[try Beta version]
Not logged in

 
 
STL vectors

Mar 31, 2021 at 10:49am
I am trying to write 3 different types of algorithms which create arrays using vectors STL( 1-st lesson).
When creating vector a and c, size() seems to be correct, however for vector b size() is doubled. Why is that happening ?

Any advice will be appreciated!

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

using namespace std;

int main()
{
	vector <int> a;
	vector <int> b;
	vector <int> c;
	int n;
		
	cout<<" Enter number of elements in vector a "; cin>>n;
	
	for(int i=0,m; i<n; i++)
	{
		cout<<"vector <int> a - element ["<<i<<"] ="; cin>>m;
		
		a.push_back(m);
		
		cout<<"a"<<a[i]<<endl;
	}
	cout<<"a size "<<a.size()<<endl;
	
	
	cout<<" Enter number of elements in vector b "; cin>>n;
	
		for(int i=0,m; i<n; i++)
	{
		cout<<"vector <int> b - element ["<<i<<"] ="; cin>>m;
		
	//	b.insert(b.begin(),m);
		b.insert(b.begin()+i,m);
		
		b.insert(b.end(),m);
		
	cout<<"b"<<b[i]<<endl;
	}
	cout<<"b size "<<b.size()<<endl;
	
	cout<<" Enter number of elements in vector c "; cin>>n;
	
	for(int i=1,m; i<=n; i++)
	{
		c.resize(i);
		
		cout<<"enter element["<<i<<"]"; cin>>m;
		
		c[i]=m;
		
		cout<<"c"<<c[i]<<endl;
	}
	cout<<"c size "<<c.size()<<endl;
	
	return 0;
	
}
Last edited on Mar 31, 2021 at 10:54am
Mar 31, 2021 at 11:01am
Hello MaxGreen,

you wrote:

When creating vector a and c, size() seems to be correct, however for vector b size() is doubled.


At what point is "b" found to be double?

Lines 8 - 10 define 3 empty vectors that have (0)zero size.

Show the input you give when you run the program and the output.

Andy
Mar 31, 2021 at 11:02am
closed account (z05DSL3A)
however for vector b size() is doubled. Why is that happening ?

Look at lines 33 and 35?
Mar 31, 2021 at 11:05am
@Handy Andy,

line 39 cout<<"b size "<<b.size()<<endl;

the output is the doubled size() of

line 26 cout<<" Enter number of elements in vector b "; cin>>n;

Enter number of elements in the vector b 6
vector <int> b - element [0] =12
b12
vector <int> b - element [1] =223
b223
vector <int> b - element [2] =212
b212
vector <int> b - element [3] =32
b32
vector <int> b - element [4] =12
b12
vector <int> b - element [5] =1
b1
b size 12, obviously size should be 6, something is wrong in my code :S
Last edited on Mar 31, 2021 at 11:06am
Mar 31, 2021 at 11:13am
Hello MaxGreen,

As Grey Wolf said. Those lines are adding to the vector twice. You only need 1 of those lines. I think I would just use line 35.

As your code is put a break point on line 39 and check the contents of vector "b".

Andy
Mar 31, 2021 at 11:14am
@Grey Wolf, thanks, now I see that for each i, there is double assignment , 1 for begin() and 1 for end();
Mar 31, 2021 at 11:15am
@Handy Andy, thanks again!
Mar 31, 2021 at 11:16am
Hello MaxGreen,

Just had a thought. The ".insert" is better used to put something in an already populated vector. Although it does work the way that you are using it. It does seem like the hard way to do it.

Andy
Mar 31, 2021 at 11:30am
Andy - what you said seems to be so appropriate, even the keyword "insert" means to put smthg into smtgh else, I guess that was just a practical experience for my HW (creating vectors using 3 different methods) .
Last edited on Mar 31, 2021 at 11:30am
Mar 31, 2021 at 3:34pm
@Handy Andy
@Grey Wolf
another question occurs,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	vector <int> c;
	cout<<" Enter number of elements in the vector c "; cin>>n;
	
	for(int i=1, m; i<=n; i++)
	{
		c.resize(i);
		
		cout<<"enter element["<<i<<"]"; cin>>m;
		
		c[i]=m;
		
	}
	cout<<"c size "<<c.size()<<endl;
	
	for(int i=1; i<=c.size(); i++)
	{	
		cout<<c[i]<<' ';
	}
	cout<<endl;


when I am displaying values it doesn't show the array itself, still for push_back(), insert() it displays easily and correct, what could be the issue ?
Mar 31, 2021 at 4:04pm
vector elements, like array elements, start at 0, not 1. There is also no need to resize inside the loop. Just set the size once when the size is known:

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

int main()
{
	size_t n {};

	std::cout << "Enter number of elements in the vector c ";
	std::cin >> n;

	std::vector<int> c(n);

	for (int i = 0; i < n; ++i) {
		std::cout << "Enter element [" << i << "] ";
		std::cin >> c[i];
	}

	std::cout << "c size " << c.size() << '\n';

	for (int i = 0; i < c.size(); ++i)
		std::cout << c[i] << ' ';

	std::cout << '\n';
}


Or using a range-for with initialiser (C++20):

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

int main()
{
	size_t n {};

	std::cout << "Enter number of elements in the vector c ";
	std::cin >> n;

	std::vector<int> c(n);

	for (size_t i {};  auto & e : c) {
		std::cout << "Enter element [" << i++ << "] ";
		std::cin >> e;
	}

	std::cout << "c size " << c.size() << '\n';

	for (const auto& e : c)
		std::cout << e << ' ';

	std::cout << '\n';
}

Last edited on Mar 31, 2021 at 4:08pm
Mar 31, 2021 at 4:10pm
@seeplus, thank you;
I guess the issue occurs because I was trying to use specifically resize inside the loop.
Mar 31, 2021 at 4:13pm
specifically resize inside the loop


Can be done that way and would work - but the overhead of re-allocations could be severe. Resizing should be done as few times as possible to avoid the overheads.
Mar 31, 2021 at 4:43pm
I guess the issue occurs because I was trying to use specifically resize inside the loop.


I can't think of any reason why that would cause an issue (apart from poor performance, as seeplus noted)? What are you thinking of when you say this?

To me, the problem you're seeing is far more likely to be that you're writing past the end of the vector on each iteration of the loop - for reasons seeplus has already explained.
Topic archived. No new replies allowed.