访问向量中的类成员 [英] Accessing class member inside a vector

查看:126
本文介绍了访问向量中的类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是C ++的新手

我有一个矢量迭代器它,里面有一个A类,类有双变量b。

我需要访问变量所以我写了这样的东西:

It.A-> b

但它给我带来了不同的错误。

有人能告诉我正确的格式吗?

Hi I am new to C++
I have a vector iterator It, which has a Class A inside it and class has double variable b.
I need to access the variable so I wrote something like this:
It.A->b
but its throwing me different errors.
can someone tell me the correct format?

推荐答案

你需要取消引用迭代器。你想要的是



You need to dereference the iterator. What you want is

*It->b





[rev.1 - 对不起,语法错误,希望修复...]



[rev.1 - sorry, syntax error, hopefully fixed...]


作为一个评论,我不会使用带向量的迭代器,但是只需使用索引。例如:

Just as a remark, I would not use an iterator with a vector, but just use the index. For example:
    std::vector<double> vec;
    ...
    double d = vec[i];
</double>



如果你坚持使用迭代器,那么把它想象成一种指针(语法方面),因此你将其取消引用为:


If you insist in using an iterator, then think of it as being kind of a pointer (syntax wise), and hence you would dereference it as:

    std::vector<mystruct> vec;
    ...
    MyStruct* p = it;
    ... or if age is a member of MyStruct
    int age = it->age;
</mystruct>


这篇关于访问向量中的类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆