writing to any position in a vector
hi
I'm trying to write structs at a random position in a vector ...my problem is that these strcuts should also be written at a position that is bigger than the actual size of the vector...i mean if the vector contains 3 elements i want for example to write a struct to position 5... when i inserted all elements i have a complete list without gaps, so i could search the position for every element that fits best. But that would mean i would need to iterate through my whole vector and look if the should be inserted after or before the elemnt in the list. I just want to know if there's simplier solution for that.
regards,
m4gnus
Better description please, you appear to be somewhat rambling, using... lots of... elipsis (...), and trying to describe what's happening at the low level. One does not say "I need these chemical equasions (*scribble scribble scribble*) to start functioning" when one is choking, one says "I can't breathe, I need to breathe" :-).
A few things:
If you know ahead of time what index you want your structure to have, simply check and resize your vector as necessary:
This dosn't try to deal with multiple indexes of the same number.
If however you don't know the index ahead of time, you'll need to sort your results somehow. For this, you can either use the sorting functions of the standard library, or use a sorting container such as map, multimap, set, or multiset.
Here's an example of set:
I posted an example which used std::list's sort function here, too.
Explaining on a higher level what you're trying to do will mean less stabbing in the dark for us as well as faster useful replies for you :-).
A few things:
If you know ahead of time what index you want your structure to have, simply check and resize your vector as necessary:
struct my_struct {};std::vector< my_struct > stuff;while ( ... ) { ... unsigned int index = ...; my_struct thing_to_insert; if ( stuff.size() <= index ) stuff.resize( index + 1 ); stuff[ index ] = thing_to_insert;}
This dosn't try to deal with multiple indexes of the same number.
If however you don't know the index ahead of time, you'll need to sort your results somehow. For this, you can either use the sorting functions of the standard library, or use a sorting container such as map, multimap, set, or multiset.
Here's an example of set:
int main () { std::set< int > stuff; stuff.push_back( 3 ); stuff.push_back( 5 ); stuff.push_back( 1 ); stuff.push_back( 3 ); std::copy( stuff.begin() , stuff.end() , std::ostream_iterator< int >( std::cout , " " ) ); //prints: "1 3 5 " (in that order - note that set eliminates duplicates. If you wanted two threes, you should have used std::multiset)}
I posted an example which used std::list's sort function here, too.
Explaining on a higher level what you're trying to do will mean less stabbing in the dark for us as well as faster useful replies for you :-).
Assuming that vector is the only choice of container...
The simple and straightforward way is this:
The simple and straightforward way is this:
void insert_element_into_vector( std::vector< foo > & v, int index, foo const & value ) { if ( std::vector< foo >::size_type( index ) >= v.size() ) { v.resize( index + 1 ); } v[index] = foo; }
Quote:Original post by ZahlmanI'm inclined to agree with that suggestion.
I think std::map<int, (whateverStruct)> sounds like what he wants.
sorry for the bad description. I'm not really good at writing texts and as i read my post now i'm quite suprised that somebody seems to understand my problem.
I only knew std:.vector and std::list so far so i'll take a closer look at std::map and hope that it will help me solve my problem...
thx for the replies.
regards,
m4gnus
I only knew std:.vector and std::list so far so i'll take a closer look at std::map and hope that it will help me solve my problem...
thx for the replies.
regards,
m4gnus
Quote:Original post by iMalcQuote:Original post by ZahlmanI'm inclined to agree with that suggestion.
I think std::map<int, (whateverStruct)> sounds like what he wants.
It is not possible to pick the best container from his description. He only describes how he fills the container, and not how he plans to use it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement