Jak uzyskać dostęp do zawartości wektora ze wskaźnika do wektora w C++?

Mam wskaźnik do wektora. Jak mogę odczytać zawartość wektora przez wskaźnik?

8 answers

Jest wiele rozwiązań, oto kilka, które wymyśliłem:

int main(int nArgs, char ** vArgs)
{
    vector<int> *v = new vector<int>(10);
    v->at(2); //Retrieve using pointer to member
    v->operator[](2); //Retrieve using pointer to operator member
    v->size(); //Retrieve size
    vector<int> &vr = *v; //Create a reference
    vr[2]; //Normal access through reference
    delete &vr; //Delete the reference. You could do the same with
                //a pointer (but not both!)
}
 102
Author: Seb Holzapfel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-04 17:59:02

Dostęp do niego jak do każdej innej wartości wskaźnika:

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;
}
 25
Author: Chad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-19 14:43:15

Czy masz wskaźnik do wektora, bo tak go zakodowałeś? Możesz to przemyśleć i użyć (ewentualnie const) referencji. Na przykład:

#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);
}

Chociaż jest to poprawny program, ogólny styl C++ polega na przekazaniu wektora przez odniesienie, a nie przez wskaźnik. Będzie to równie efektywne, ale wtedy nie będziesz musiał zajmować się wskaźnikami null i alokacją/czyszczeniem pamięci itp. Użyj referencji const, jeśli nie zamierzasz modyfikować wektora, a nie-const odniesienie, jeśli musisz dokonać modyfikacji.

Oto wersja referencyjna powyższego programu:

#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);
}

Jak widzisz, wszystkie informacje zawarte w a zostaną przekazane do funkcji foo, ale nie skopiuje ona całkowicie nowej wartości, ponieważ jest przekazywana przez odniesienie. Jest więc tak samo wydajny jak przechodzenie przez wskaźnik i można go używać jako wartości normalnej, zamiast wymyślać, jak go używać jako wskaźnika lub konieczności dereferencji go.

 18
Author: Andrew Rasmussen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-04 18:20:42
vector<int> v;
v.push_back(906);
vector<int> * p = &v;
cout << (*p)[0] << endl;
 11
Author: Mark Ransom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-04 17:58:17

Możesz uzyskać bezpośredni dostęp do metod iteratora:

std::vector<int> *intVec;
std::vector<int>::iterator it;

for( it = intVec->begin(); it != intVec->end(); ++it )
{
}

Jeśli chcesz operatora dostępu do tablicy, musisz usunąć odniesienie do wskaźnika. Na przykład:

std::vector<int> *intVec;

int val = (*intVec)[0];
 6
Author: Jon Benedicto,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-04 17:58:43

Istnieje wiele rozwiązań. Na przykład można użyć metody at().

*założyłem, że szukasz odpowiednika [] operatora.

 2
Author: eugene_che,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-04 17:56:49
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
 1
Author: kutukmu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-11-13 15:05:21

Najprostszym sposobem użycia tablicy jest użycie vector::data() member.

 -3
Author: sarat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-04 18:50:21