【发布时间】:2013-12-09 23:13:13
【问题描述】:
我正在尝试将值分配给文本文件中的二维数组,这就是我所拥有的:
string line = "";
string temp = "";
string removechr = "{} ";
string sepchar = ",";
ifstream myfile("pt.txt", ios::in);
if(myfile.is_open()){
while( getline(myfile,line)){
//--Remove characters
size_t found = line.find_first_of(removechr);
while(found != string::npos){
line.erase(found);
}
//--Assign Values
for(int y = 0; y < YCOL; ++y){
for(int x = 0; x < XROW; ++x){
size_t found = line.find_first_of(sepchar);
while(found != string::npos){
temp.insert(line.begin(),found);
map[y][x]=stoi(temp);
temp = "";
line.erase(line.begin(),(line.begin() + found) - 1) ;
}
}
}//End of for loop
}
}
首先我删除了不必要的字符({ } 和空格),然后我运行一个循环来设置数组中的值。所以现在当它找到第一个逗号时,我想将值插入到临时字符串中,这样它就可以分配给数组。毕竟我删除了刚刚分配的部分。
这就是我想做的,但我似乎没有工作,我希望有更好的方法来做到这一点。
【问题讨论】:
-
'但我似乎没有工作' 太模糊了,无法在这里提问......
标签: c++ string multidimensional-array ifstream