读取 2D 矢量
Reading into a 2D vector
>我从文件读取到 2D 矢量时遇到问题。我是 c++ 的新手,我没有看到我的代码有问题。
该文件遵循此模式。
5
0 0 0 0 1
0 0 0 1 0
0 0 0 1 1
0 0 1 0 0
...
我的算法来阅读它...
void Similarity::readData(Scanner& inStream){
dataLength = inStream.nextInt();
while(inStream.hasNext()){
vector<int> temp;
int tempInt = 0;
for(int i = 0; i < dataLength; ++i){
tempInt = inStream.nextInt();
temp.push_back(tempInt);
temp.clear();
}
theData.push_back(temp);
theData.clear();
}
}
我的算法来打印它。
string Similarity::toString(){
string result = "";
for(int i = 0; i < theData.size(); ++i){
for(int j = 0; j < dataLength; ++j){
result += convertInt(theData[i][j]);
}
result += "n";
}
return result;
}
string Similarity::convertInt(int number){
stringstream s;
s << number;
return s.str();
}
toString 没有输出,是我需要处理的 readData 还是 toString?
谢谢。
这部分代码毫无意义(至少从流中消耗数字):
tempInt = inStream.nextInt();
temp.push_back(tempInt);
temp.clear();
因为temp.clear()
会立即删除用 push_back() 插入的对象。
这同样适用于
theData.push_back(temp);
theData.clear();
我认为你确实需要一个temp.clear()
,你现在有theData.clear()
相关文章:
- 打印第二列时的2d字符矢量打印空间
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- opengl glBegin(GL_LINES) 和 glBegin(GL_POINT) 在 2D 中不可视化点矢量
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 如何在 c++ 中格式化和访问 2D 矢量数组中的数据
- 读取字符串文本输入以创建 2D 矢量
- 识别 2D 矢量C++中的位置
- Cython:无法分配给 2D 矢量
- 按列随机播放 2D 矢量
- 初始化空的 2D 矢量?
- 如何在 2d 矢量 c++ 中复制元素并将其放在原始元素旁边
- 如何访问具有地图的2D矢量?
- 通过功能进行2D矢量输入和显示
- 在C++中将对象添加到 2D 矢量
- 太空入侵者 – 2D 矢量运动算法
- 从 2 个 1D 矢量创建 2D 矢量
- C++ 默认参数的重新定义:参数 1 (矢量 2D)
- 矢量2D擦除元素并保持字段为空
- C STL库矢量2D数组不超出边界
- 在c++ Qt中显示2d矢量/2d数组到简单的图形