In order for your vector to be sorted all the time, you should always insert new elements in the correct position. Since you want to post elements in ascending order, and the vector provides only the pop_back () method, you must sort the elements in descending order. so first you need to find the correct position and then paste in there:
typedef std::vector<int> ints;
void insert( ints &cont, int value ) {
ints::iterator it = std::lower_bound( cont.begin(), cont.end(), value, std::greater<int>() );
cont.insert( it, value );
}
source
share