Vector resize | Sololearn: Learn to code for FREE!

+1

Vector resize

hello there! I'm working on a program, and if I could resize a vector in the code so I lose some elements, my life would be a lot easier. Here's the idea, we have the following vector: 5 6 7 8 8 8 9 2 3 4 1 and I want to cut that sequence off so it looks like this: 5 6 7 9 2 3 4 1 well, I can manage till here, it's just that I can't resize it afterwards. I'm using the resize function, I included the vector library, but I get this error message: request for member 'resize' in 'b', which is of non-class help

1/24/2017 2:57:10 PM

Hard To Find

5 Answers

New Answer

+4

There is a new function in c++11 called shrink_to_fit(); for vectors... You may use that.

+1

as far as I understood, you have deleted last 3 items of your vector and you want to reize it to a (bigger) size... You can try doing this: // insert 0 at the end of table 3 times. for(int i = 0; i <= 3;i++){ vecname.insert(vecname.end(), 0); }

0

type 'int [bla bla bla] had no more space back there. So, what am I doing wrong and what should I do?

0

thank you, but that's not what I was thinking. you see, I want to cut off a portion of the sequence, then replace it and cut the last part. Look: 5 7 6 1 2 8 8 8 4 3 2 4 would become 5 7 6 1 2 4 3 2 4 so I want to reduce its size I know how to get the 4, 3 and 2 to replace the 8 8 8 but resizing is the difficult part

0

You could just create a new vector (array) with the known number of elements that will be in the new array and then copy what you need from the old to the new. After that you can delete the old one to free memory.That is how resize works anyway.