如何访问(例如,cout)多维 STL 向量中迭代器的当前值 (C++)
我在使用 STL 方面还是个新手。以下代码片段填充 3D 矢量 (S) 并编译 (g++) 良好。
const int maxBonds = 6;
vector< vector< vector<int> > > S;
S.resize(maxBonds);
populate(S); // function that returns S with various layers filled with int data in rows and columns.
for (vector<vector<vector<int> > >::iterator Slayer = S.begin(); Slayer != S.end(); Slayer++) {
cout << "Layer contains " << Slayer->size() << " rows" << endl;
for (vector<vector<int> >::iterator Srow = Slayer->begin(); Srow != Slayer->end(); Srow++) {
for (vector<int>::iterator Scol = Srow->begin(); Scol != Srow->end(); Scol++) {
cout << *Scol;
}
cout << endl;
}
cout << endl;
}
这运行良好:
Layer contains 0 rows
Layer contains 5 rows
000
200
020
220
002
Layer contains 12 rows
100
010
210
... // etc.
但是,我想在迭代期间打印出外部迭代器(Slayer、Srow)的值。如何正确取消引用 Slayer 的当前值,即
cout << "Layer # " << Slayer->??? << " contains " << Slayer->size() << " rows" << endl;
I'm still very much a neophyte in using STL. The following code fragment populates a 3D vector (S) and compiles (g++) fine.
const int maxBonds = 6;
vector< vector< vector<int> > > S;
S.resize(maxBonds);
populate(S); // function that returns S with various layers filled with int data in rows and columns.
for (vector<vector<vector<int> > >::iterator Slayer = S.begin(); Slayer != S.end(); Slayer++) {
cout << "Layer contains " << Slayer->size() << " rows" << endl;
for (vector<vector<int> >::iterator Srow = Slayer->begin(); Srow != Slayer->end(); Srow++) {
for (vector<int>::iterator Scol = Srow->begin(); Scol != Srow->end(); Scol++) {
cout << *Scol;
}
cout << endl;
}
cout << endl;
}
This runs fine:
Layer contains 0 rows
Layer contains 5 rows
000
200
020
220
002
Layer contains 12 rows
100
010
210
... // etc.
However, I'd like to print out the value of the outer iterators (Slayer, Srow) during iteration. How do I properly dereference the current value of Slayer, i.e.
cout << "Layer # " << Slayer->??? << " contains " << Slayer->size() << " rows" << endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要的是当前向量“索引”,我认为执行此操作的标准方法是增加另一个值。
您还可以尝试从迭代器中减去 begin() ,但它不适用于所有迭代器类型(我没有时间进行一些测试)。
If what you need is the current vector "index", i think that the standard way to do this is to increment another value.
You can also try to subtract begin() from the iterator but it doesn't work well with all iterators types (I lack time for making some tests).
因此,
*Scol
将为您提供最后一个向量中的 int,*Srow
将为您提供该行的向量,*Slayer
将为您提供包含行向量的层向量。So
*Scol
gets you the int within the last vector,*Srow
would get you the vector of the row,*Slayer
would get you the vector of the layers containing the vector of rows.你不能直接这样做。
我这样做的方式是定义一个带有循环的函数来为您执行此操作。
类似于:
建议 - 使用
typedef
,而不是编写所有这些长的嵌套向量。编辑:当然,您始终可以为这些类型(向量)重载
operator<<
You can't do this directly.
The way I'd do it - define an function with a loop to do this for you.
Something like:
Advice - use
typedef
, instead of writing all these long, nested vectors.EDIT: Of cource, you can always overload
operator<<
for these types (vectors)