Reinitialization of iterator in case of vector

Hi All,

I am using insert operation in vector. After doing first insert operation iterator needs to be reinitialized. but in case of set it is not required. Reason please?

std::vector<int> myvector;

int main()
{

vector<int>::iterator it;
it = myvector.begin();
myvector.insert(it,10);
it = myvector.begin(); // if I comment it will not work in case of vector but in case of SET it is working...
myvector.insert(it,10);
cout<<"size"<<myvector.size()<<myvector[1];
std::cin.get();
return 0;
}
Try this simple program and you will understand why vector iterator is not valid after the insert operation.

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
47
48
49
50
51
52
53
54
55
#include	<iostream>
#include	<vector>


int main()
{
	{
		std::vector<int> v;

		std::cout << "\nvector size is " << v.size() << std::endl;
		std::cout << "data = " << v.data() << std::endl;

		std::vector<int>::iterator it;
		
		it = v.begin();
		
		v.insert( it, 1 );

		std::cout << "vector size is " << v.size() << std::endl;
		std::cout << "data = " << v.data() << std::endl;

		it = v.begin();
		
		v.insert( it, 2 );

		std::cout << "vector size is " << v.size() << std::endl;
		std::cout << "data = " << v.data() << std::endl;
	}

	{
		std::vector<int> v;
		v.reserve( 2 );

		std::cout << "\nvector size is " << v.size() << std::endl;
		std::cout << "data = " << v.data() << std::endl;

		std::vector<int>::iterator it;
		
		it = v.begin();
		
		v.insert( it, 1 );

		std::cout << "vector size is " << v.size() << std::endl;
		std::cout << "data = " << v.data() << std::endl;

		it = v.begin();
		
		v.insert( it, 2 );

		std::cout << "vector size is " << v.size() << std::endl;
		std::cout << "data = " << v.data() << std::endl;
	}

	return 0;
}

In the first code block memory is ( usuually ) allocated in each insert operation.
In the second code block memory is not allocated and the iterator is valid. But in general case it is better do not think that the iterator is valid. The correct way of using insert is the following

it = myvector.insert( it, value );
Also you can try the folllowing example

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
#include	<iostream>
#include	<vector>


int main()
{
	{
		std::vector<int> v;

		std::cout << "\nvector size is " << v.size() << std::endl;
		std::cout << "data = " << v.data() << std::endl;

		std::vector<int>::iterator it;
		
		it = v.begin();
		
		it = v.insert( it, 1 );

		std::cout << "vector size is " << v.size() << std::endl;
		std::cout << "data = " << v.data() << std::endl;
		std::cout << "&( *it ) = " << &( *it )  << std::endl;

		it = v.insert( it, 2 );

		std::cout << "vector size is " << v.size() << std::endl;
		std::cout << "data = " << v.data() << std::endl;
		std::cout << "&( *it ) = " << &( *it )  << std::endl;
	}

	return 0;
}
Hi,

In the case of first code block itself,
for C++ STL Set the second time

it = v.begin(); which is not required.....

can u please clarify ???

Eg:

int main()
{
std::set<int> myset;
set<int>::iterator it;
it=myset.begin();
myset.insert(it,2);
myset.insert(it,9);
return 0;
}
Here only once we initialised iterator. my doubt is why only once in set and twice in vector?
Because vectors shall have one memory block for all its elements as built-in arrays have. In fact, return value of data() is the address of this dynamically allocated array.
sets does not required to have one single memory block to store their elements.

All depends on the internal organization of a container.
Topic archived. No new replies allowed.