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.
Thanks for any replies.
- Eric 5 1911
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.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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.
Nope, it's not there. But, as you said, it's easy to build it on top of what
is there:
#include <algorithm>
template < typename Cont, typename Value >
Cont shifted_insert( Cont the_cont, Value const & val ) {
std::rotate( the_cont.begin(), --the_cont.end(), the_cont.end() );
*the_cont.begin() = val;
return ( the_cont );
}
#include <string>
#include <iostream>
int main ( void ) {
std::string foo = "abc";
std::cout << shifted_insert( foo, 'd' ) << '\n';
}
Best
Kai-Uwe Bux
Kai-Uwe Bux 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.
Nope, it's not there. But, as you said, it's easy to build it on top of what
is there:
#include <algorithm>
template < typename Cont, typename Value >
Cont shifted_insert( Cont the_cont, Value const & val ) {
std::rotate( the_cont.begin(), --the_cont.end(), the_cont.end() );
Would this be better?
std:;rotate( the_cont.begin(), the_cont.rbegin().base(),
the_cont.end());
I just don't like the predecrement on a function return. I know,
personal taste.
*the_cont.begin() = val;
return ( the_cont );
}
red floyd wrote:
Kai-Uwe Bux 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.
Nope, it's not there. But, as you said, it's easy to build it on top of what is there:
#include <algorithm>
template < typename Cont, typename Value > Cont shifted_insert( Cont the_cont, Value const & val ) { std::rotate( the_cont.begin(), --the_cont.end(), the_cont.end() );
Would this be better?
std:;rotate( the_cont.begin(), the_cont.rbegin().base(),
the_cont.end());
I just don't like the predecrement on a function return. I know,
personal taste.
Thanks, you are right. And it's probably not just taste. The way you wrote
it will guarantee the template to work even with vector containers that
have naked pointers for iterators. In that way, your version has smaller
conceptual requirements than mine.
> *the_cont.begin() = val; return ( the_cont ); }
Best
Kai-Uwe Bux
On 19 Juli, 23:11, Kai-Uwe Bux <jkherci...@gmx.netwrote:
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.
Nope, it's not there. But, as you said, it's easy to build it on top of what
is there:
#include <algorithm>
template < typename Cont, typename Value >
Cont shifted_insert( Cont the_cont, Value const & val ) {
std::rotate( the_cont.begin(), --the_cont.end(), the_cont.end() );
*the_cont.begin() = val;
return ( the_cont );
}
#include <string>
#include <iostream>
int main ( void ) {
std::string foo = "abc";
std::cout << shifted_insert( foo, 'd' ) << '\n';
}
Best
Kai-Uwe Bux
Thanks for the code, Kai, and also Victor for his reply. I agree that
it's rare that you need this so I'm not surprised there's no function
in standard library for performing this. I've made some design changes
in my program and no I no longer need to do this but still I've learnt
something.
- Eric This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mike Meng |
last post by:
Hi all,
I just finished reading Learning Python 3rd ed, and am doing my
first Python application, which retrieves and process text and XML
documents from Web. Python helped me to write the...
|
by: arnuld |
last post by:
i am confused on some aspects of bitset class:
/* C++ Primer 4/e
* chapter 3
*
* exercise 3.23
*
*/
#include <iostream>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |