1、c++如何按空格拆分字符串
std::vector<std:: string> split(std:: string str,std:: string pattern)
{
std:: string::size_type pos;
std::vector<std:: string> result;
str+=pattern; // 扩展字符串以方便操作
int size=str.size();
for( int i= 0; i<size; i++)
{
pos=str.find(pattern,i);
if(pos<size)
{
std:: string s=str.substr(i,pos-i);
result.push_back(s);
i=pos+pattern.size()- 1;
}
}
return result;
}
2、txt如何读取txt文件中的字符串
ifstream in("C:\\Users\\Administrator\\Desktop\\resultofkrige.txt"); \\
string str;//注意这里symmetry.txt为你当前工程目录下的文件内容
for(string s;getline(in,s);)
{
pointvalue=split(s," ");\\split 为上面代码的函数
float value=atof(pointvalue[5].c_str());
third.push_back(value);\\装入vector的后面
}
3、如何为灰度图像的像素赋值
IplImage *image=cvCreateImage(size,IPL_DEPTH_8U,1);\\创建单通道的灰度图
for(int i=0;i<third.size()/1499;i++)
{
for (int j=0;j<1499;j++)
{
CvScalar scal;\\赋值
scal.val[0]=third[counter];
cvSet2D(image,i,j,scal);
counter++;
}
}
4、在c++中数组定义过大后会内存爆掉:
search结果:
Project->Setting->Link,在Category下选Output,在Stack allocations的Reserve填入
想要的堆栈大小就可以了!
另外可以把数组定义在全局变量中。以后应该学习如何动态分配内存空间。