迭代向量的所有成员

发布于 2024-11-27 22:15:02 字数 618 浏览 0 评论 0原文

我有两个 struct 定义如下:

struct vertex
{
  double x;
  double y;
  double z;
};

struct finalVertex
{
  int n;
  vertex v;
};

我使用以下代码迭代列表并打印所有成员:

  vector<finalVertex> finalVertices;
  vector<finalVertex>::iterator ve;

  for ( ve = finalVertices.begin(); ve < finalVertices.end(); ve++ )
    {
      out << *(ve).v.x << *(ve).v.y << *(ve).v.z << endl;
    }

我收到以下错误代码:

main.cpp:651: 错误:'class __gnu_cxx::__normal_iterator > >'没有 名为“v”的成员

访问集合元素的语法上正确的方法是什么?

I have two structs defined as in the following:

struct vertex
{
  double x;
  double y;
  double z;
};

struct finalVertex
{
  int n;
  vertex v;
};

I use the following code to iterate through the list and print all the members:

  vector<finalVertex> finalVertices;
  vector<finalVertex>::iterator ve;

  for ( ve = finalVertices.begin(); ve < finalVertices.end(); ve++ )
    {
      out << *(ve).v.x << *(ve).v.y << *(ve).v.z << endl;
    }

I receive the following code of error:

main.cpp:651: error: 'class __gnu_cxx::__normal_iterator > >' has no
member named 'v'

What is the syntactically correct way of accessing the elements of the set?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

王权女流氓 2024-12-04 22:15:02

问题在于运算符优先级:编写 (*ve).vx 或更简单的 ve->vx

除此之外,我建议您重写 vertex 结构的 operator << ,以使代码更具可读性:

std::ostream& operator <<(std::ostream& out, vertex const& value) {
    return out << value.x << " " << value.y << " " << value.z;
}

然后像这样使用它:

for ( ve = finalVertices.begin(); ve != finalVertices.end(); ve++ )
    out << ve->v << endl;

The problem is operator precedence: write (*ve).v.x or simpler, ve->v.x.

Apart from that, I would advise you to override operator << for your vertex structure to make your code vastly more readable:

std::ostream& operator <<(std::ostream& out, vertex const& value) {
    return out << value.x << " " << value.y << " " << value.z;
}

and then use it like this:

for ( ve = finalVertices.begin(); ve != finalVertices.end(); ve++ )
    out << ve->v << endl;
暗喜 2024-12-04 22:15:02

你应该做的是:

ve->v.x

你也可以做的是:

(*ve).v.x

但这很糟糕。 :)

What you should do is:

ve->v.x

What you can also do is:

(*ve).v.x

but it sucks. :)

谈场末日恋爱 2024-12-04 22:15:02
out << *(ve).v.x << *(ve).v.y << *(ve).v.z << endl;

您的 *(ve).vx 相当于 *((ve).vx)。您可能想要:

out << (*ve).v.x << (*ve).v.y << (*ve).v.z << endl;

或者:

out << ve->v.x << ve->v.y << ve->v.z << endl;

另外,您的循环效率并不高。不需要每次迭代都调用 end() ,并且迭代器的后增量可能比普通指针/整数重得多,因此您应该尽可能习惯使用预增量:

for ( ve = finalVertices.begin(), end = finalVertices.end(); ve != end; ++ve )
out << *(ve).v.x << *(ve).v.y << *(ve).v.z << endl;

Your *(ve).v.x is equivalent to *((ve).v.x). You probably want:

out << (*ve).v.x << (*ve).v.y << (*ve).v.z << endl;

Or:

out << ve->v.x << ve->v.y << ve->v.z << endl;

Also, your loop isn't as efficient as it could be. Calling end() every iteration isn't needed, and post-increment can be a lot heavier with iterators than plain pointers/integers so you should get used to using pre-increment when possible:

for ( ve = finalVertices.begin(), end = finalVertices.end(); ve != end; ++ve )
云淡月浅 2024-12-04 22:15:02

将取消引用移到括号内,如下所示:

out << (*ve).v.x << (*ve).v.y << (*ve).v.z << endl;

我还建议更改 ve ve < FinalVertices.end();ve != FinalVertices.end();

Move your dereference to inside the parens, like so:

out << (*ve).v.x << (*ve).v.y << (*ve).v.z << endl;

I'd also suggest changing ve < finalVertices.end(); to ve != finalVertices.end();.

随梦而飞# 2024-12-04 22:15:02
        for ( ve = finalVertices.begin(); ve != finalVertices.end(); ++ve )
        {
            ve->v.x;
        }
        for ( ve = finalVertices.begin(); ve != finalVertices.end(); ++ve )
        {
            ve->v.x;
        }
非要怀念 2024-12-04 22:15:02

你不应该写
已经< FinalVertices.end()
你必须写
ve != FinalVertices.end()

You should not write
ve < finalVertices.end()
you must write
ve != finalVertices.end()

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