Vector question
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
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.
However, if you want a sorted collection of integers, you may want to consider a std::set instead.
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
And to answer your question: Yes
Toolmaker
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?
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?
To answer your question: No.
If the vector is already sorted you don't search linearly you use upper_bound
sample:
O( log2(N)) instead of O(N) to locate insertion location, sure the insert is still O(N) but every bit helps right?
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?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement