473,221 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

"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 1884
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
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...
2
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>
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.