473,657 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2415
* 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}chao sfactory{dot}or g | 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& RemoveUnnecessa rySpaces(string & input_string)
{
// *** Remove spaces at start

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

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

while (input_string[input_string.le ngth() -1] == ' ')
input_string.er ase(input_strin g.length() -1,1);

// *** DONE remove spaces from end
// *** Remove multiples spaces
for (string::size_t ype i = 0; ; )
{
i = input_string.fi nd_first_of(' ', i);

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

++i;

while(input_str ing[i] == ' ') input_string.er ase(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& RemoveUnnecessa rySpaces(string & input_string)
{
// *** Remove spaces at start

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

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

while (input_string[input_string.le ngth() -1] == ' ')
input_string.er ase(input_strin g.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_t ype i = 0; ; )
{
i = input_string.fi nd_first_of(' ', i);

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

++i;

while(input_str ing[i] == ' ') input_string.er ase(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& RemoveUnnecessa rySpaces(string & input_string)
{
std::string::si ze_type end = input_string.fi nd_first_not_of (' ');
input_string.er ase ( 0, end );

std::string::si ze_type beg = input_string.fi nd_last_not_of( ' ');
input_string.er ase ( beg+1, std::string::np os );

std::string::si ze_type i = input_string.fi nd(" ");
while ( i != std::string::np os ) {
input_string.er ase( i, 1 );
i = input_string.fi nd( " ", 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& RemoveUnnecessa rySpaces(string & input_string)
{
// *** Remove spaces at start

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

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

while (input_string[input_string.le ngth() -1] == ' ')
input_string.er ase(input_strin g.length() -1,1);

// *** DONE remove spaces from end
// *** Remove multiples spaces
for (string::size_t ype i = 0; ; )
{
i = input_string.fi nd_first_of(' ', i);

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

++i;

while(input_str ing[i] == ' ') input_string.er ase(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.ind igo.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& RemoveUnnecessa rySpaces(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& RemoveUnnecessa rySpaces(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 = RemoveUnnecessa rySpaces (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 RemoveUnnecessa rySpaces(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;
RemoveUnnecessa rySpaces (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 RemoveUnnecessa rySpaces(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 = RemoveUnnecessa rySpaces (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*******@pres by.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& RemoveUnnecessa rySpaces(string & input_string)
{
// *** Remove spaces at start

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

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

while (input_string[input_string.le ngth() -1] == ' ')
input_string.er ase(input_strin g.length() -1,1);

// *** DONE remove spaces from end
// *** Remove multiples spaces
for (string::size_t ype i = 0; ; )
{
i = input_string.fi nd_first_of(' ', i);

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

++i;

while(input_str ing[i] == ' ') input_string.er ase(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& RemoveUnnecessa rySpaces(string & input_string)
{
// *** Remove spaces at start

while (input_string[0] == ' ') input_string.er ase(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.le ngth() -1] == ' ')
input_string.er ase(input_strin g.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_t ype i = 0; ; )
{
i = input_string.fi nd_first_of(' ', i);

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

++i;

while(input_str ing[i] == ' ') input_string.er ase(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_ty pe 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

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

Similar topics

10
8162
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
11
3645
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( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
22
13240
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; char c; std::string str;
19
6136
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 found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
8
9182
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 a namespace and another typedef to basic_string: namespace my_string {
6
11496
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 example string str1; as a class member and then add text to it. or do I have to specify it's length when defining? 2. How to convert from std::string to LPCSTR
2
5490
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
15842
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
2894
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 "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
0
8723
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
8502
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
8602
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.