473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Shifted" insert in std::string

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

Jul 19 '07 #1
5 1933
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
Jul 19 '07 #2
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
Jul 19 '07 #3
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 );
}
Jul 19 '07 #4
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
Jul 19 '07 #5
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

Jul 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3350
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 application in a few hours, I'm very happy with its productivity. But the performance is not satisfactory. I decide to optimized it in Python before trying C/C++ extensions. But I don't know Python much and have no clu to tune my program. Also, I...
2
2728
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>
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6679
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
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 we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.