How to access the contents of a vector from a pointer to the vector in C++?

后端 未结 8 835
不思量自难忘°
不思量自难忘° 2020-11-30 17:26

I have a pointer to a vector. Now, how can I read the contents of the vector through pointer?

相关标签:
8条回答
  • 2020-11-30 17:49

    You can access the iterator methods directly:

    std::vector<int> *intVec;
    std::vector<int>::iterator it;
    
    for( it = intVec->begin(); it != intVec->end(); ++it )
    {
    }
    

    If you want the array-access operator, you'd have to de-reference the pointer. For example:

    std::vector<int> *intVec;
    
    int val = (*intVec)[0];
    
    0 讨论(0)
  • 2020-11-30 17:52

    Do you have a pointer to a vector because that's how you've coded it? You may want to reconsider this and use a (possibly const) reference. For example:

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void foo(vector<int>* a)
    {
        cout << a->at(0) << a->at(1) << a->at(2) << endl;
        // expected result is "123"
    }
    
    int main()
    {
        vector<int> a;
        a.push_back(1);
        a.push_back(2);
        a.push_back(3);
    
        foo(&a);
    }
    

    While this is a valid program, the general C++ style is to pass a vector by reference rather than by pointer. This will be just as efficient, but then you don't have to deal with possibly null pointers and memory allocation/cleanup, etc. Use a const reference if you aren't going to modify the vector, and a non-const reference if you do need to make modifications.

    Here's the references version of the above program:

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void foo(const vector<int>& a)
    {
        cout << a[0] << a[1] << a[2] << endl;
        // expected result is "123"
    }
    
    int main()
    {
        vector<int> a;
        a.push_back(1);
        a.push_back(2);
        a.push_back(3);
    
        foo(a);
    }
    

    As you can see, all of the information contained within a will be passed to the function foo, but it will not copy an entirely new value, since it is being passed by reference. It is therefore just as efficient as passing by pointer, and you can use it as a normal value rather than having to figure out how to use it as a pointer or having to dereference it.

    0 讨论(0)
  • 2020-11-30 17:54

    The easiest way use it as array is use vector::data() member.

    0 讨论(0)
  • 2020-11-30 17:56

    Access it like any other pointer value:

    std::vector<int>* v = new std::vector<int>();
    
    v->push_back(0);
    v->push_back(12);
    v->push_back(1);
    
    int twelve = v->at(1);
    int one = (*v)[2];
    
    // iterate it
    for(std::vector<int>::const_iterator cit = v->begin(), e = v->end; 
        cit != e;  ++cit)
    {
        int value = *cit;
    }
    
    // or, more perversely
    for(int x = 0; x < v->size(); ++x)
    {
        int value = (*v)[x];
    }
    
    // Or -- with C++ 11 support
    for(auto i : *v)
    {
       int value = i;
    }
    
    0 讨论(0)
  • 2020-11-30 18:05
    vector <int> numbers {10,20,30,40};
    vector <int> *ptr {nullptr};
    
    ptr = &numbers;
    
    for(auto num: *ptr){
     cout << num << endl;
    }
    
    
    cout << (*ptr).at(2) << endl; // 20
    
    cout << "-------" << endl;
    
    cout << ptr -> at(2) << endl; // 20
    
    0 讨论(0)
  • 2020-11-30 18:06

    There are a lot of solutions. For example you can use at() method.

    *I assumed that you a looking for equivalent to [] operator.

    0 讨论(0)
提交回复
热议问题