迭代二维 STL 向量 c++
我目前正在尝试打印我正在开发的游戏中玩家的动作历史记录。在每轮结束时,每个玩家都在正向或负向移动了一定的量,这会被记录为移动向量中的 int 。最终我想绘制每个玩家的移动方向与时间的关系,但我在从二维向量中提取数据时遇到了困难。
所以我尝试的第一件事就是迭代并打印所有元素,但这不会编译:
void output_movement(const std::vector< std::vector<int> > & movement){
std::vector< std::vector<int> >::iterator row;
std::vector<int>::iterator col;
for (row = movement.begin(); row != movement.end(); ++row) {
for (col = row->begin(); col != row->end(); ++col) {
std::cout << **col;
}
}
}
编译器给出了我不太理解的错误消息:
hg_competition.cpp:45: error: no match for ‘operator=’ in ‘row = ((const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >*)money_movement)->std::vector<_Tp, _Alloc>::begin [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]()’
/usr/include/c++/4.4/bits/stl_iterator.h:669: note: candidates are: __gnu_cxx::__normal_iterator<std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >& __gnu_cxx::__normal_iterator<std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >::operator=(const __gnu_cxx::__normal_iterator<std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >&)
非常感谢任何帮助!
I'm currently trying to print out a history of movements for players in a game I am working on. At the end of each round every player has moved some amount in the positive or negative direction and this gets recorded as an int in the movement vector. Eventually I'm wanting to plot the directions moved vs time for each player but I'm having trouble extracting the data out of the 2d vector.
So the first thing I tried was to just iterate and print all the elements, however this doesn't compile:
void output_movement(const std::vector< std::vector<int> > & movement){
std::vector< std::vector<int> >::iterator row;
std::vector<int>::iterator col;
for (row = movement.begin(); row != movement.end(); ++row) {
for (col = row->begin(); col != row->end(); ++col) {
std::cout << **col;
}
}
}
The compiler gives this error message which I don't really understand:
hg_competition.cpp:45: error: no match for ‘operator=’ in ‘row = ((const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >*)money_movement)->std::vector<_Tp, _Alloc>::begin [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]()’
/usr/include/c++/4.4/bits/stl_iterator.h:669: note: candidates are: __gnu_cxx::__normal_iterator<std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >& __gnu_cxx::__normal_iterator<std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >::operator=(const __gnu_cxx::__normal_iterator<std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >&)
Any help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果
vector
是 const 引用,则需要使用const_iterator
。另外,要输出col
您只需要取消引用它一次。编辑:使用 typedef 将使您的代码更具可读性
You need to use a
const_iterator
if thevector
is a const reference. Also, to outputcol
you only need to dereference it once.Edit: using typedefs will make your code more readable
2D
vector
被声明为const
,因此您需要使用const_iterator
而不是iterator
。您也不应该双重取消引用
col
。它是一个迭代器,因此您只需要取消引用一次。The 2D
vector
is declaredconst
, so you need to useconst_iterator
instead ofiterator
.You also shouldn't doubly dereference
col
. It is an iterator, so you only need to dereference once.const
对象返回const_iterators
,因此只需将所有地方的iterator
替换为const_iterator
即可。这也可以防止对载体进行不必要的修改。这是 Sam 和 Mathieu 的建议的结合:
const
objects returnconst_iterators
, so simply replaceiterator
byconst_iterator
everywhere. This also prevents unwanted modifications of the vectors.This is the combination of Sam's and Mathieu's suggestions:
John,您建议使用 lambda,但如果 C++11 可用,我更喜欢
John, you suggested using lambdas, but if C++11 is available, I'd prefer
天哪,任何东西都比那些混乱的
for
循环要好。这里有一些替代方案。选择你喜欢的。或者,
或者,我个人的偏好(boost/foreach.hpp),
Omg, anything is better than that mess of
for
loops. Here some alternatives. Choose whichever you like.or,
or, my personal preference (boost/foreach.hpp),