Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Vector question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

And to answer your question: Yes

Toolmaker

Share this post


Link to post
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 this post


Link to post
Share on other sites
To answer your question: No.

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 this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
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?


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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!