One*_*ser 1 c++ linux iterator
我目前正在编写模板矩阵.它包含一个vector<vector<T>>名为mat和cols和rows的val,它们包含行数和列数.我试图构建一个迭代器,并发现我无法为vector向量构建迭代器函数.因为我的代码的其余部分已经写好了,所以我添加了一个matrixToVector函数,它将我vector<vector<T>>转向vector<T>(我知道这不是最佳选择,但它仅适用于大学练习).在我的Windows笔记本电脑上很好,但在Linux计算机实验室中,前两个迭代器的数量总是一个非常大的随机数,然后是0,然后其余的数字都很好.这是代码:
/**
* turns the 2d mat vecor to 1d vector.
*/
vector<T> matrixToVector()
{
vector<T> v;
for(unsigned int i = 0 ; i < rowsNum; i++)
{
for(unsigned int j = 0; j < colsNum; j++)
{
v.push_back(mat[i][j]);
}
}
return v;
}
/**
* iterator
*/
typedef typename std::vector<T>::const_iterator const_iterator;
/**
* return the end of the iterator.
*/
const_iterator end()
{
return matrixToVector().end();
}
/**
* return the begining of the iterator.
*/
const_iterator begin()
{
return matrixToVector().begin();
}
Run Code Online (Sandbox Code Playgroud)
我不知道什么是错的.我该怎么办?
编辑:当我使用常规打印功能打印矩阵时,它在Linux和Windows上运行良好.
const_iterator begin()
{
return matrixToVector().begin();
}
Run Code Online (Sandbox Code Playgroud)
你在堆栈上返回对象的引用,matrixToVector()创建临时对象的结果,从返回后将被销毁begin
| 归档时间: |
|
| 查看次数: |
123 次 |
| 最近记录: |