use pointer to access an element in a vector

Maybe it's a stupid question,

I have a vector store quite a few objects. If I just wanna use a pointer to access one of the element, can I assign the address of that element to the pointer and then call the pointer to get the object?

Here is the example codes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>
#include <math.h>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <time.h>

using namespace std;

int main (int argc, char * const argv[])
{	
	vector<double*> list;
	int N = 6;
	
	double **b;
	
	double a[10];
	
	for(int i=0;i<N;i++)
		a[i] = 1.1*i;
	
	for(int i=0;i<N;i++)
	{
		list.push_back(&a[i]);
		if(i==2)
		{
			b = &list.back(); 
			
			cout << &list.back() << " " << b << endl;
		}
	}
	
	//for(int i=0;i<list.size();i++)
	//	cout << *list[i] << endl; 
	
	cout << **b << endl;
	
	cout << &list[2] << endl;
	cout << b << endl;
	
    return 0;
}



The output are different after the push_back(). Does it mean when we use these container it actually change the location of the elements during the resize?
std::vector reallocates the memory during adding new elements.
You could reserve initially enought memory that to escape the reallocation.

1
2
vector<double*> list;
list.reserve( N );


Also it is a bad idea to name object of type std::vector as list, because there is standard conteiner std::list. It will confuse readers of your code.
Last edited on
Use iterators to do this.
I think you need to declare a vector::iterator object. (I'm not sure)
Usage is like pointers.
thanks, i think reserve() is a solution for this problem.
a reallocation invalidates all pointers, references and iterators.
> thanks, i think reserve() is a solution for this problem.

Only if insertions and removals are constrained to be always at the end of the sequence.

Use a std::list<> instead if you want iterators/pointers/references to remain valid till the the time the actual element pointed to or referred to is erased.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>

int main()
{
    {
        std::vector<int> seq = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
        seq.reserve(1000) ;
        auto pointer = &*std::find( seq.begin(), seq.end(), 7 ) ;
        std::cout << *pointer << '\n' ; // 7
        seq.erase( std::find( seq.begin(), seq.end(), 3 ) ) ;
        std::cout << *pointer << '\n' ; // 8
        seq.insert( std::find( seq.begin(), seq.end(), 2 ), 6, -3 ) ;
        std::cout << *pointer << '\n' ; // -3
    }

    {
        std::list<int> seq = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
        auto pointer = &*std::find( seq.begin(), seq.end(), 7 ) ;
        std::cout << *pointer << '\n' ; // 7
        seq.erase( std::find( seq.begin(), seq.end(), 3 ) ) ;
        std::cout << *pointer << '\n' ; // 7
        seq.insert( std::find( seq.begin(), seq.end(), 2 ), 6, -3 ) ;
        std::cout << *pointer << '\n' ; // 7
    }
}
Last edited on
Topic archived. No new replies allowed.