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

发布于 2024-11-05 02:58:18 字数 1184 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巴黎夜雨 2024-11-12 02:58:18

如果您需要的是当前向量“索引”,我认为执行此操作的标准方法是增加另一个值。

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;
}

您还可以尝试从迭代器中减去 begin() ,但它不适用于所有迭代器类型(我没有时间进行一些测试)。

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).

疾风者 2024-11-12 02:58:18

因此,*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.

超可爱的懒熊 2024-11-12 02:58:18

你不能直接这样做。

我这样做的方式是定义一个带有循环的函数来为您执行此操作。


类似于:

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

建议 - 使用 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:

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.

EDIT: Of cource, you can always overload operator<< for these types (vectors)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文