vector it->和*it

//每次写代码总是被迭代器的iter->和*iter弄晕,主要是被protobuf弄晕了

#include <vector>

struct test{
    test(){
        memset(this, 0, sizeof(test));
    }
    int a;
    int b;
};

int main()
{
    test a, b;
    a.a = a.b = 0;
    b.a = b.b = 1;

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

 

posted @ 2016-10-15 11:49  zzyoucan  阅读(452)  评论(0编辑  收藏  举报