c++ - Insert multiple values into vector -
i have std::vector<t>
variable. have 2 variables of type t, first of represents value in vector after insert, while second represents value insert.
so lets have container: 1,2,1,1,2,2
and 2 values 2 , 3 respect definitions above. wish write function update container instead contain:
1,2,3,1,1,2,3,2,3
i using c++98 , boost. std or boost functions might use implement function?
iterating on vector , using std::insert 1 way, gets messy when 1 realizes need remember hop on value inserted.
this do:
vector<t> copy; (vector<t>::iterator i=original.begin(); i!=original.end(); ++i) { copy.push_back(*i); if (*i == first) copy.push_back(second); } original.swap(copy);
put call reserve in there if want. know need room @ least original.size()
elements. initial iteraton on vector (or use std::count
) determine exact amount of elements reserve, without testing, don't know whether improve performance.
Comments
Post a Comment