C++_Strings_Quick_Reference

advertisement
C++ string class - Quick Reference Guide
#include <string>;
strings are objects in C++. The C language used char arrays/pointers and cstring literals.
C++ strings may be assigned, modified, etc. using operators or member functions.
Simplest way to create a string object:
string name; / string name = “Jack”; / string name(“Jack”);
string constructors
string class constructor prototypes:
1.
2.
3.
4.
5.
6.
7.
string str1;
string str1 ( const string& s );
string str1 ( size_type length, char ch );
string str1 ( const char* str );
string str1 ( const char* str, size_type length );
string str1 ( const string& str, size_type index, size_type length );
string str1 ( input_iterator start, input_iterator end );
The string constructors create a new string containing:
1.
2.
3.
4.
5.
6.
7.
nothing - an empty string,
a copy of the given string s – created using the “copy” constructor
length copies of ch
a duplicate of the cstring str
a duplicate of the cstring str (limited to length characters long)
a substring of str starting at index and length characters long
a string of characters denoted by the start and end iterators
Operator
Meaning
>>
extracts characters from stream up to whitespace, insert into string
<<
inserts string into stream
=
assigns string on right to string object on left
+=
appends string on right to end of contents on left
+
concatenates two strings
[ ]
dereferences character in string using array notation. No bounds checking!
>, >=, <, <=, ==, !=
relational operators for string comparison. Return true or false
C++ string class
Member Functions
Function Description - These methods use zero-based indexing
append( str )
Appends str to theString. str can be a string object or a character array.
append( str, x, n )
n number of characters from str, starting at position x, are appended to theString.
If theString is too small, the function will copy as many characters as possible.
append( str, n )
The first n characters of the character array str are appended to theString.
append( n, 'z' )
Appends n copies of 'z' to theString.
assign( str )
Assigns str to theString. str can be a string object or character array.
C++ string class
Member Functions
Function Description - These methods use zero-based indexing
assign( str, x, n )
n number of characters from str, starting at position x, are assigned to theString.
If theString is too small, the function will copy as many characters as possible.
assign( str, n )
The first n charactrers of the character array str are assigned to theString.
assign( n, 'z' )
Assigns n copies of 'z' to theString.
at( x )
Returns the character at position x in theStrings.begin( ); Does bounds checking.
begin( )
Returns an iterator pointing to the first character in the string.
capacity( )
Returns the size of the storage allocated for theString.
clear( )
Clears theString by deleting all the characters stored in it.
compare( str )
compare( str, x, n )
Performs a comparison like the strcmp function with the same return values.
str can be a string object or a character array.
Compares theString and str, starting at position x, and continuing for n
characters. The return value is like strcmp.
str can be a string object or character array.
copy( str, x, n )
Copies the character array str to theString, beginning at position x, for n
characters.
If theString is too small, the function will copy as many characters as possible.
c_str()
Returns a non-modifiable standard C character array version of the string.
data( )
Returns a pointer to the first character in the string
empty( )
Returns true if theString is empty.
end( )
Returns an iterator pointing to the last characters of the string in theString.
erase( x, n )
Erases n characters from theString, beginning at position x.
find( str, x )
Returns the first position at or beyond position x where the string str is found in
theString.
str may be either a string object or a character array.
insert( x, str )
Inserts a copy of str into theString, beginning at position x.
str may be either a string object or a character array.
insert( x, n, 'z' )
Inserts 'z' n times into theString at position x.
length( )
Returns the length of the string in theString.
replace( x, n, str )
Replaces the n characters in theString beginning at position x with the characters
in string object str.
C++ string class
Member Functions
Function Description - These methods use zero-based indexing
changes the size of the allocation in theString to n.
resize( n, 'z' )
If n is less that the current size of the string, theString is truncated to n
characters.
If n is greater, theString is expanded and 'z' is appended at the end enough times
to fill the new positions.
size( )
Returns the length of the string in theString.
substr( x, n )
Returns a copy of a substring. The substring is n characters long and begins at
position x of theString.
swap( str )
Swaps the contents of theString with str.
to_string(<num exp>)
Converts a number to a string.
The function returns a string.
string number = to_string( 123 );
Note: this is NOT a member function.
string function getline
The getline string function should be used when you want input a string from the keyboard using the cin object or
from a file using a stream object that may contain embedded whitespace characters ( spaces/tabs )
Note: This is not a member function and should not be confused with the c-string member function getline!
Syntax:


getline( <stream object>, <string variable> );
<stream object>
is
<string variable> is
cin or the name of some other input stream object
the name of a string variable to receive the input string
Examples: getline ( cin, studentName );
getline ( inFile, customerFullName );
Download