简体   繁体   English

如何访问(例如,cout)多维 STL 向量中的迭代器的当前值(C++)

[英]how do I access (e.g., cout) the current value of iterators in multi-dimensional STL vectors (C++)

I'm still very much a neophyte in using STL.在使用 STL 方面,我仍然是个新手。 The following code fragment populates a 3D vector (S) and compiles (g++) fine.以下代码片段填充 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;
}

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.但是,我想在迭代期间打印出外部迭代器(Slayer,Srow)的值。 How do I properly dereference the current value of Slayer, ie如何正确取消引用 Slayer 的当前值,即

 cout << "Layer # " << Slayer->??? << " contains " << Slayer->size() << " rows" << endl;

If what you need is the current vector "index", i think that the standard way to do this is to increment another value.如果您需要的是当前向量“索引”,我认为执行此操作的标准方法是增加另一个值。

unsigned int countSlayer = 0;

for (vector<vector<vector<int> > >::iterator Slayer = S.begin(); Slayer != S.end(); Slayer++, ++countSlayer) {

    cout << "Layer # " << countSlayer << " 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;
}

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).您也可以尝试从迭代器中减去 begin(),但它不适用于所有迭代器类型(我没有时间进行一些测试)。

You can't do this directly.你不能直接这样做。

The way I'd do it - define an function with a loop to do this for you.我这样做的方式 - 定义一个带有循环的 function 来为您执行此操作。


Something like:就像是:

void Print2dIntVector( const vector<vector<int> >& v )
{
    // ..
}
void Print3dIntVector( const vector< vector< vector<int> > >& v )
{
    // ..
}

Advice - use typedef , instead of writing all these long, nested vectors.建议 - 使用typedef ,而不是编写所有这些长的嵌套向量。

EDIT : Of cource, you can always overload operator<< for these types (vectors)编辑:当然,您始终可以为这些类型(向量)重载operator<<

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.所以*Scol会得到最后一个向量中的 int, *Srow会得到行的向量, *Slayer会得到包含行向量的层的向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM