vector it->和*it

时间:2023-03-08 17:20:16
vector it->和*it
//每次写代码总是被迭代器的iter->和*iter弄晕,主要是被protobuf弄晕了

#include <vector>

struct test{
test(){
memset(this, , sizeof(test));
}
int a;
int b;
}; int main()
{
test a, b;
a.a = a.b = ;
b.a = b.b = ; //std::vector<test> vecT;
//vecT.push_back(a);
//vecT.push_back(b);
//for (std::vector<test>::iterator it = vecT.begin(); it != vecT.end(); ++it)
//{
// int v_a = it->a;//直接访问元素中的成员
// int v_b = it->b; // v_a = (*it).a;//(*it)直接就是vector中的保存的元素
// v_b = (*it).b;
//} std::vector<test*> vecT;
vecT.push_back(&a);
vecT.push_back(&b);
for (std::vector<test*>::iterator it = vecT.begin(); it != vecT.end(); ++it)
{
int v_a = (*it)->a;//必须用(*it)获取到指针,然后访问到a
int v_b = (*it)->b;
} return ;
}