我这段代码运行时得到一个内存问题解析:与查找和SUBSTR功能
#include <iomanip>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<string> tokens;
int x,y;
string toParse = "word,more words,an int 5,some more,this is the 5th one,thisll be 6,seven,eight,nine,tenth,extra";
for(y=0; y<10; y++){
cout << "in for loop " << y << endl;
x = toParse.find(",");
tokens[y]= toParse.substr(0,x);
toParse = toParse.substr(x+1);
}
for(y=0; y<9; y++)
cout << tokens[y] << endl;
}
从本质上讲,我只是想存储的值,取决于逗号分隔。
x应该设置为逗号的位置,我将这些信息存储在逗号之前。然后,我使用substr来获取一个新的字符串来获取位,从逗号后面取整行。我遇到了分段错误。