Info on std::string

J

JKop

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
 
K

Kai-Uwe Bux

JKop said:
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
 
J

JKop

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 == ' ') input_string.erase(i,1);

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

}


-JKop
 
K

Kai-Uwe Bux

JKop said:
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 == ' ') 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
 
D

David Hilsee

JKop said:
JKop posted:


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 == ' ') 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;
}
 
D

Dave Townsend

JKop said:
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 == ' ') 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.
 
A

Andre Kostur

JKop said:
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 == ' ') 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 */ }
 
J

Jon Bell

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.
 
J

JKop

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
 
I

Ioannis Vranos

JKop said:
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.
 
I

Ioannis Vranos

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 == ' ') 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(), ' ');
}
 
D

David Hilsee

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()
);
}
 
S

Skyler York

JKop said:
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++.
 
I

Ioannis Vranos

David said:
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.
 
R

Richard Herring

JKop <[email protected]> said:
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?
 
A

Andre Kostur

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..
 
R

Richard Herring

[QUOTE="Julie said:
Is there something wrong with section 21.3 of that copy of the Standard
you didn't pay for?

He said "informative description"...[/QUOTE]

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

It looks both informative and descriptive to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top