473,394 Members | 1,699 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.

Info on std::string

Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Thanks.
-JKop
Jul 22 '05 #1
18 2386
* JKop <NU**@NULL.NULL> wrote:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.


http://www.cppreference.com/

Martin

--
Are you Anonymous? Where? ... I don't think so ...

[ devnull{at}chaosfactory{dot}org | http://www.chaosfactory.org/ ]
Jul 22 '05 #2
JKop wrote:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Thanks.
-JKop


You might want to try the C++ Library Reference from Dinkumware:

http://www.dinkumware.com/refxcpp.html

Best

Kai-Uwe Bux
Jul 22 '05 #3
JKop posted:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Thanks.
-JKop


Just to get one thing straight: When you do:

string blah("Hello!!");

Is new memory allocated and is "Hello!!" copied, yes?

Anyway, I'm brand-new to std::string, so can some-one please comment on my
following code. At the moment it compiles but it causes run-time access
violations:
string& RemoveUnnecessarySpaces(string& input_string)
{
// *** Remove spaces at start

while (input_string[0] == ' ') input_string.erase(0,1);

// *** DONE remove spaces at start
// *** Remove spaces from end

while (input_string[input_string.length() -1] == ' ')
input_string.erase(input_string.length() -1,1);

// *** DONE remove spaces from end
// *** Remove multiples spaces
for (string::size_type i = 0; ; )
{
i = input_string.find_first_of(' ', i);

if (i == string::npos) break;

++i;

while(input_string[i] == ' ') input_string.erase(i,1);

++i;
}
// *** DONE remove multiple spaces

}
-JKop
Jul 22 '05 #4
"JKop" <NU**@NULL.NULL> wrote in message
news:TQ******************@news.indigo.ie...
JKop posted:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Thanks.
-JKop
Just to get one thing straight: When you do:

string blah("Hello!!");

Is new memory allocated and is "Hello!!" copied, yes?


Yes, the string will copy "Hello!!" into its internal storage.
Anyway, I'm brand-new to std::string, so can some-one please comment on my
following code. At the moment it compiles but it causes run-time access
violations:
string& RemoveUnnecessarySpaces(string& input_string)
{
// *** Remove spaces at start

while (input_string[0] == ' ') input_string.erase(0,1);

// *** DONE remove spaces at start
// *** Remove spaces from end

while (input_string[input_string.length() -1] == ' ')
input_string.erase(input_string.length() -1,1);

// *** DONE remove spaces from end
Both of these "space removing" loops cannot handle cases where the string
consists entirely of spaces.
// *** Remove multiples spaces
for (string::size_type i = 0; ; )
{
i = input_string.find_first_of(' ', i);

if (i == string::npos) break;

++i;

while(input_string[i] == ' ') input_string.erase(i,1);

++i;
}
// *** DONE remove multiple spaces

}


You didn't return a value? Is the code complete?

Instead of doing the manual check for duplicate spaces, I'd take an easier
route (well, it seems easier to me, anyway) and repeatedly search for
double-spaces. Something like this:

string& RemoveUnnecessarySpaces(string& input_string)
{
std::string::size_type end = input_string.find_first_not_of(' ');
input_string.erase ( 0, end );

std::string::size_type beg = input_string.find_last_not_of(' ');
input_string.erase ( beg+1, std::string::npos );

std::string::size_type i = input_string.find(" ");
while ( i != std::string::npos ) {
input_string.erase( i, 1 );
i = input_string.find( " ", i );
}

return input_string;
}

--
David Hilsee
Jul 22 '05 #5

"JKop" <NU**@NULL.NULL> wrote in message
news:TQ******************@news.indigo.ie...
JKop posted:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Thanks.
-JKop


Just to get one thing straight: When you do:

string blah("Hello!!");

Is new memory allocated and is "Hello!!" copied, yes?

Anyway, I'm brand-new to std::string, so can some-one please comment on my
following code. At the moment it compiles but it causes run-time access
violations:
string& RemoveUnnecessarySpaces(string& input_string)
{
// *** Remove spaces at start

while (input_string[0] == ' ') input_string.erase(0,1);

// *** DONE remove spaces at start
// *** Remove spaces from end

while (input_string[input_string.length() -1] == ' ')
input_string.erase(input_string.length() -1,1);

// *** DONE remove spaces from end
// *** Remove multiples spaces
for (string::size_type i = 0; ; )
{
i = input_string.find_first_of(' ', i);

if (i == string::npos) break;

++i;

while(input_string[i] == ' ') input_string.erase(i,1);

++i;
}
// *** DONE remove multiple spaces

}
-JKop


This wouldn't compile on MSVC++ 6, your function didn't return anything.
I fixed that
problem and your code seems to work ok on the single example I fed it.

Free functions that return references usually make me queezy. In this case
I would prefer
either to use a void function and return the processed string as the
reference argument (which
this does already), or make input_string a const ref arg and return a fresh
string (preferred).
I prefer the latter since if you screw up the processing ( like an exception
happens), the original string
is unchanged.

Jul 22 '05 #6
In article <TQ******************@news.indigo.ie>, JKop <NU**@NULL.NULL> wrote:

Just to get one thing straight: When you do:

string blah("Hello!!");

Is new memory allocated and is "Hello!!" copied, yes?
Yes. "Hello!" is a literal string, so the program allocates and
initializes it in "read only" memory somewhere. When the program reaches
the declaration of 'blah', it invokes the constructor for class 'string'
and passes a pointer to the literal string as an argument. The
constructor then allocates memory to hold the string data, and copies the
literal string into it.
string& RemoveUnnecessarySpaces(string& input_string)
{ [snip code]}


Your function works fine for me when I use it as below, after inserting a
'return' statement.

#include <iostream>
#include <string>

using namespace std;

string& RemoveUnnecessarySpaces(string& input_string)
{
// snip your code
return input_string;
}

int main ()
{
cout << "gimme a string:" << endl;
string line;
getline (cin, line);
cout << "You entered '" << line << "'." << endl;
cout << "Stripping extra spaces..." << endl;
string stripped = RemoveUnnecessarySpaces (line);
cout << "Result is '" << stripped << "'." << endl;
return 0;
}

Sample output:

gimme a string:
fee fie foe fum
You entered ' fee fie foe fum '.
Stripping extra spaces...
Result is 'fee fie foe fum'.
Original is now 'fee fie foe fum'.

Note that your function also leaves the stripped string in the input
parameter, as you can see if you output the contents of 'line' after the
function call. In fact, your function is returning a reference to 'line',
in this example; then the main function copies the (modified) contents of
'line' into 'stripped'. Is this really the way you want it?

I think most programmers would either make this a void function which
returns the "stripped" string via a modified reference parameter only:

#include <iostream>
#include <string>

using namespace std;

void RemoveUnnecessarySpaces(string& input_string)
{
// snip; no 'return' statement
}

int main ()
{
cout << "gimme a string:" << endl;
string line;
getline (cin, line);
cout << "You entered '" << line << "'." << endl;
cout << "Stripping extra spaces..." << endl;
RemoveUnnecessarySpaces (line);
cout << "Result is '" << line << "'." << endl;
return 0;
}

Or pass the string by value, which leaves the original version unmodified,
and return the new string by value (can't use a reference here because the
new string is a local variable inside the function):

#include <iostream>
#include <string>

using namespace std;

string RemoveUnnecessarySpaces(string input_string)
{
// snip
return input_string;

}

int main ()
{
cout << "gimme a string:" << endl;
string line;
getline (cin, line);
cout << "You entered '" << line << "'." << endl;
cout << "Stripping extra spaces..." << endl;
string stripped = RemoveUnnecessarySpaces (line);
cout << "Result is '" << stripped << "'." << endl;
cout << "Original is still '" << line << "'." << endl;
return 0;
}

Of course, this version does two string copies, which may affect your
program's performance.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #7
JKop wrote:
JKop posted:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Thanks.
-JKop


Just to get one thing straight: When you do:

string blah("Hello!!");

Is new memory allocated and is "Hello!!" copied, yes?

Anyway, I'm brand-new to std::string, so can some-one please comment on my
following code. At the moment it compiles but it causes run-time access
violations:
string& RemoveUnnecessarySpaces(string& input_string)
{
// *** Remove spaces at start

while (input_string[0] == ' ') input_string.erase(0,1);

// *** DONE remove spaces at start
// *** Remove spaces from end

while (input_string[input_string.length() -1] == ' ')
input_string.erase(input_string.length() -1,1);

// *** DONE remove spaces from end
// *** Remove multiples spaces
for (string::size_type i = 0; ; )
{
i = input_string.find_first_of(' ', i);

if (i == string::npos) break;

++i;

while(input_string[i] == ' ') input_string.erase(i,1);

++i;
}
// *** DONE remove multiple spaces

}
-JKop


Your function does not return any value. You might fix that by inserting
return( input_string );
right before the end.
Best

Kai-Uwe Bux
Jul 22 '05 #8
JKop <NU**@NULL.NULL> wrote in news:TQ******************@news.indigo.ie:
Just to get one thing straight: When you do:

string blah("Hello!!");

Is new memory allocated and is "Hello!!" copied, yes?
Yes.
Anyway, I'm brand-new to std::string, so can some-one please comment
on my following code. At the moment it compiles but it causes run-time
access violations:
string& RemoveUnnecessarySpaces(string& input_string)
{
// *** Remove spaces at start

while (input_string[0] == ' ') input_string.erase(0,1);
If you pass it an empty string, this may crash (input_string[0] isn't
valid).

If you pass it a string of only spaces, this may crash (same reason as
above, after the last space is removed).
// *** DONE remove spaces at start
// *** Remove spaces from end

while (input_string[input_string.length() -1] == ' ')
input_string.erase(input_string.length() -1,1);
Personally I'd use .size() instead of .length(), but that's a style
thing.

Same crash cases as above. You'll cause the index to go past the
beginning of the string.
// *** DONE remove spaces from end
// *** Remove multiples spaces
for (string::size_type i = 0; ; )
{
i = input_string.find_first_of(' ', i);

if (i == string::npos) break;

++i;

while(input_string[i] == ' ') input_string.erase(i,1);

++i;
}
// *** DONE remove multiple spaces
Stylisticly I wouldn't use a for loop here.... this logic just doesn't
suggest a for loop for me. Somehow a while makes more sense to me... I'd
replace the for with: string::size_type i = 0 ; while (i !=
string::npos) { /* mostly the same body */ }
}
-JKop


Jul 22 '05 #9
The code I posted wasn't perfected.

I want to introduce a new term here, I'm going call it
"half-baked code". I'm going use this term to indicate the
code I post hasn't been compile tested, that arrays bounds
may be wrong, there may be typos, stuff like that.

So my original code was half-baked. I was just looking for
opinions on the general methods I was using.
-JKop
Jul 22 '05 #10
JKop wrote:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Some info on std::string is that it is a typedef.
typedef basic_string<char> string;
So essentially you are looking for the members of template basic_string.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #11
JKop wrote:

string& RemoveUnnecessarySpaces(string& input_string)
{
// *** Remove spaces at start

while (input_string[0] == ' ') input_string.erase(0,1);

// *** DONE remove spaces at start
// *** Remove spaces from end

while (input_string[input_string.length() -1] == ' ')
input_string.erase(input_string.length() -1,1);

// *** DONE remove spaces from end
// *** Remove multiples spaces
for (string::size_type i = 0; ; )
{
i = input_string.find_first_of(' ', i);

if (i == string::npos) break;

++i;

while(input_string[i] == ' ') input_string.erase(i,1);

++i;
}
// *** DONE remove multiple spaces

}


This is what you want to do:
#include <string>
#include <algorithm>

void RemoveUnnecessarySpaces(std::string &inputString)
{
using namespace std;

if(inputString.empty())
return;

remove(inputString.begin(), inputString.end(), ' ');
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #12
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:ci**********@ulysses.noc.ntua.gr...
<snip>
This is what you want to do:
#include <string>
#include <algorithm>

void RemoveUnnecessarySpaces(std::string &inputString)
{
using namespace std;

if(inputString.empty())
return;

remove(inputString.begin(), inputString.end(), ' ');
}


My understand was that all spaces at the beginning or end of the string were
to be removed, and all other character sequences consisting of two or more
consecutive spaces were to be replaced with a sequence of one space.

If JKop wanted to remove all spaces, the code would look like this
(untested):

void RemoveUnnecessarySpaces(std::string &inputString)
{
inputString.erase(
std::remove( inputString.begin(), inputString.end(), ' ' ),
inputString.end()
);
}

--
David Hilsee
Jul 22 '05 #13
JKop wrote:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Thanks.
-JKop

MSDN is the de facto reference I'm surprised hasn't been mentioned yet.
Also, for $17, you could download the C++ ISO standard and get some
really complete documentation on all the standard classes, as well as
everything else that's standard C++.
Jul 22 '05 #14
David Hilsee wrote:
If JKop wanted to remove all spaces, the code would look like this
(untested):

void RemoveUnnecessarySpaces(std::string &inputString)
{
inputString.erase(
std::remove( inputString.begin(), inputString.end(), ' ' ),
inputString.end()
);
}


Yes you are right.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #15
In message <Wp******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Is there something wrong with section 21.3 of that copy of the Standard
you didn't pay for?
--
Richard Herring
Jul 22 '05 #16
Skyler York <sk****@seas.upenn.edu> wrote in news:ci2req$7m3c$2
@netnews.upenn.edu:
JKop wrote:
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Thanks.
-JKop

MSDN is the de facto reference I'm surprised hasn't been mentioned yet.
Also, for $17, you could download the C++ ISO standard and get some
really complete documentation on all the standard classes, as well as
everything else that's standard C++.


'fraid that I certainly don't use the MSDN as a reference. Physical books
are what I use... The Standard C++ Library by Josuttis for example..
Jul 22 '05 #17
Richard Herring wrote:

In message <Wp******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Can some-one please point me to a nice site that gives an
exhaustive list of all the memberfunctions,
membervariables, operators, etc. of the std::string class,
along with an informative description of how each works.

I've been trying Google for the last 20 minutes but can't
get anything decent.

Is there something wrong with section 21.3 of that copy of the Standard
you didn't pay for?
--
Richard Herring


He said "informative description"...
Jul 22 '05 #18
In message <41***************@nospam.com>, Julie <ju***@nospam.com>
writes
Richard Herring wrote:

In message <Wp******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
>Can some-one please point me to a nice site that gives an
>exhaustive list of all the memberfunctions,
>membervariables, operators, etc. of the std::string class,
>along with an informative description of how each works.
>
>I've been trying Google for the last 20 minutes but can't
>get anything decent.
>

Is there something wrong with section 21.3 of that copy of the Standard
you didn't pay for?


He said "informative description"...


Hmmm.
Each function listed in turn.
Requires...
Throws...
Effects...
Returns...

It looks both informative and descriptive to me.

--
Richard Herring
Jul 22 '05 #19

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

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
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; ...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
8
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
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,...
0
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...
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...

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.