473,394 Members | 1,567 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,394 software developers and data experts.

efficient approach to reverse a std::string object

Hello all

Recentlly, I was asked to write a program in which there is a function
that need to reverse a std::string object.
I wrote like this:
string ReverseString(const string& srcStr)
{
string str;
for(size_t i = 0; i < srcStr.length(); ++i)
str += srcStr[srcStr.length() - i - 1];

return str;
}

Is there any potential error in the ReverseString function?
And if there is any more efficient approach to do this?

Jul 23 '05 #1
2 5424

"July" <ju**********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello all

Recentlly, I was asked to write a program in which there is a function
that need to reverse a std::string object.
I wrote like this:
string ReverseString(const string& srcStr)
{
string str;
for(size_t i = 0; i < srcStr.length(); ++i)
str += srcStr[srcStr.length() - i - 1];

return str;
}

Is there any potential error in the ReverseString function?
That you can debug...
And if there is any more efficient approach to do this?


std::reverse does exactly that.
Jul 23 '05 #2


July schreef:
Hello all

Recentlly, I was asked to write a program in which there is a function
that need to reverse a std::string object.
I wrote like this:
string ReverseString(const string& srcStr)
{
string str;
for(size_t i = 0; i < srcStr.length(); ++i)
str += srcStr[srcStr.length() - i - 1];

return str;
}

Is there any potential error in the ReverseString function? Seems Ok.
And if there is any more efficient approach to do this?


Reverse iterators:

string ReverseString(const string& srcStr) {
return string( srcStr.rbegin(), srcStr.rend() );
}

You might not even need the function, if you see how simple it
really is.

HTH,
Michiel Salters.

Jul 23 '05 #3

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

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
10
by: Alex | last post by:
I'm looking for a fast way to split a string into individual tokens. The typical format of the string is token1|token2|token3|...|tokenN| and the number of tokens varies (which is why i use a...
2
by: Cheng Mo | last post by:
Below code cannot be compiled. try { throw std::string; } catch(...) { } While below is correct.
3
by: Scott Brady Drummonds | last post by:
Hi, all, I've a fairly small piece of code that is causing me problems. I'm using std::string and am building a string of several dozen characters using several of std::string's functions: a...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
10
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use...
1
by: mathieu | last post by:
Hi C++ gurus, Why in the following code the std::string destructor is being called before the 'catch' ? Thanks, -Mathieu #include <iostream> #include <sstream>
144
by: dominantubergeek | last post by:
Hello, I'm a highly experienced expert C programmer and I've written this code to reverse a string in place. I think you could all learn something from it! int reverse(char* reverseme){ int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
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...
0
tracyyun
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 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.