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填入
想要的堆棧大小就可以了!
另外可以把數組定義在全局變量中。以後應該學習如何動態分配內存空間。