std::vector中的意外更改
Unexpected changes in std::vector
代码如下:
typedef struct Triplet
{
double value;
int row;
int column;
};
class Matrix
{
public:
//Some methods
double specialMethod(/*params*/);
private:
std::vector<Triplet> elements;
int nRows;
int nColumns;
};
specialMethod
在矩阵中得到调用值后。元素被破坏了。但是除了像这样迭代之外,对它们没有做任何事情:
std::vector<Triplet>::iterator it;
std::vector<Pair> buff;
for (it = this->elements.begin(); it != this->elements.end(); ++it)
{
if (it->column = n)
{
Pair p;
p.key = it->row;
p.value = it->value;
buff.push_back(p);
}
}
不知道从哪里开始找错误
if (it->column = n)
应: if (it->column == n)
如果可能的话,将n定义为const值,并在If指令中进行比较时颠倒顺序-就像这样:If (n == it->column)。编译器将帮助您找到类似的错误。
请注意,如果打开了更高的警告级别,编译器无论如何都会对这种错误发出警告。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中用vector填充一个简单的动态数组
- vector.resize()中的分配错误
- 使用std::vector的OpenCL矩阵乘法
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 在某些循环内使用vector.push_back时出现分段错误
- 当vector是tje全局变量时,c++中vector的内存管理
- std::vector的包装器,使数组的结构看起来像结构的数组
- 在C++中对T*类型执行std::move的意外行为
- 为什么(-1)%vector::size()总是返回0
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 在C++中将类(带有Vector成员)保存为二进制文件
- 处理除以零会导致<csignal>意外行为
- 编译器如何区分std::vector的构造函数
- 将 int 数组转换为 std::vector<int*>
- vector.size() 在比较中意外工作
- 从函数的返回值将元素C++存储到 std::vector 中时出现意外结果
- EXC_BAD_ACCESS 在 std::vector <double>的静态声明意外破坏时
- std::vector中的意外更改
- Std::vector::insert给出意外的结果