Re: "Shifted" insert in std::string
On Jul 19, 10:58 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Eric Lilja wrote:
Is there something "elegant" in the standard library I can use to
perform a "shifted insert" in a std::string? Let me examplify what I
mean with shifted insert.
Say I have:
std::string foo = "abc";
std::string shifted_foo = shifted_insert(foo, 'd');
shifted_foo should after the shifted_insert() equal "dab".
The char 'd' is inserted first, pushing everything up one index but
the size of the string should remain the same so in effect the last
char 'c' is pushed off the edge.
Doing this manually is relatively easy but I wanted to check if I'm
missing out on something in the standard library.
No, you're not missing anything. This functionality is not something
that is often in demand (at least in my POV), there is no place for
it in the standard library.
It's also extremely easy to do with the existing functionality:
size_t oldSize = foo.size() ;
foo.insert( 0, 1, 'd' ) ;
foo.resize( oldSize ) ;
In practice, the only times I've needed something anything like
this is to maintain a sliding window in e.g. a file, in which
case, the goal is to keep the window a fixed size. In that
case, I'll use deque (which was designed expressedly for this
sort of thing); the design of the STL generally allows
substituting one container for another if you're careful about
what you do with the iterators.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34