Public Group

# Vector question

This topic is 4901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

if i have a vector such as: 1, 6, 10, 50 and i want to insert '7' do i iterate to 6 and do vector(index+1, 7)? //where index is the value 6 is so that way i have 1, 6, 7, 10, 50

##### Share on other sites
Assuming you are talking C++ and referring to std::vector, if you want to maintain a sorted vector, you could use vector::insert(), though it would be more robust to use std::upper_bound() to find an iterator that pointed to an element greater than the value you are inserting.

However, if you want a sorted collection of integers, you may want to consider a std::set instead.

##### Share on other sites
You could use std::sort on yuor vector to sort it. Just insert it at the back, set a flag the vector is "dirty" and when you need the data, have it sorted by std::sort.

Toolmaker

##### Share on other sites
this one about iterators.

C++ and STL Vectors.

i have two iterators

now i want to have my vector look like this:

BBBBBRRRRRR //b is black and r is red.

now if both iterators point at the first element and the vector is empty.
and i add a B both iterators move.
if i add a R only one iterator moves.
so now i have a iterator over B and one over R. BR
now if i have add another B i want to add it so the vector is:
BBR where one iterator is over the second and the other iterator is over the R.

is that possible?

##### Share on other sites

If the vector is already sorted you don't search linearly you use upper_bound
sample:
template <typename Cont, typename T>void InsertSorted(Cont &c, const T &val){	c.insert( std::upper_bound( c.begin(), c.end(), val), val);}

O( log2(N)) instead of O(N) to locate insertion location, sure the insert is still O(N) but every bit helps right?

##### Share on other sites
Quote:
 Original post by Alpha_ProgDesthis one about iterators.C++ and STL Vectors.i have two iteratorsnow i want to have my vector look like this:BBBBBRRRRRR //b is black and r is red.now if both iterators point at the first element and the vector is empty.and i add a B both iterators move.if i add a R only one iterator moves. so now i have a iterator over B and one over R. BRnow if i have add another B i want to add it so the vector is:BBR where one iterator is over the second and the other iterator is over the R.is that possible?

Modifying a vector have the side effect of (in most cases (and you'll know when it doesnt...)) invalidating all iterators into the vector so no.

Sounds like maybe you could solve this with a list (that has the nice property that insertion/deletion doesn't invalidate iterators unless you just removed the element that the iterator was refering to.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634058
• Total Posts
3015289
×