从文件中读取两个向量
reading two vectors from file
问题在这里描述。我试图使用以下代码来解决它,但它不起作用。
const char* filename = "test.txt";
ifstream file1(filename);
vector<int> v1;
vector<int> v2;
vector<int> res;
int number;
char c;
while(1){
while(1){
v1.push_back(number);
file1.get(c);
if (c==';') break;
}
while(1){
v2.push_back(number);
file1.get(c);
if (c=='n') break;
}
for (vector<int>::iterator it = v2.begin(); it!=v2.end(); it++)
cout << *it << ',';
cout << endl;
file1.get(c);
if (c==EOF) break;
file1.unget();
}
阅读末端存在问题。c=='n'
对吗?
要阅读一行,您应该使用:
istream& getline (istream& is, string& str, char delim);
在您的情况下,使用';'
然后,您可以使用','
这样:
std::string line, temp;
std::getline(file1,line,';'); //get a line. (till ';')
std::istringstream s1 (line); //init stream with the whole line
while(std::getline(s1,temp,',')){//get a number as string from the line. (till ',')
int n;
std::istringstream s2(temp);
s2>>n; //convert string number to numeric value
//now you can push it into the vector...
}
相关文章:
- 在C++中查找两个向量之间最相似的值
- 如何在C++中从两个向量生成所有可能的对?
- 返回两个向量 – 使用引用还是元组?
- 检查两个向量是否并行的最有效方法
- 如何获得比较两个向量对的子集
- 如何在 c++ 中对两个向量进行线性搜索?
- 如何在 c++ 中从两个向量创建 JSON 对象?
- C++如何同时删除位于两个向量中的 2 个指针?
- 如何通过 stl 容器和算法库计算两个向量的内积?
- 删除两个向量中的重复项
- 两个向量在犰狳中相等?
- 为什么我无法比较自定义类类型的两个向量?
- 通过块比较两个向量时,如何避免重复
- Boost.Test - 如何在交叉点上检查两个向量
- 通过移动从两个向量创建元组向量
- 如何在C 中合并两个向量
- R- armadillo c :用其他两个向量对向量进行排序
- 使用嵌套循环搜索两个向量并查看其属性
- 如何以精度换取速度来评估C++中两个向量的点积符号?(不特定于硬件)
- 我该如何XOR这两个向量的内容