Monday, January 24, 2011

std::string assignment and swapping

String assignment
The easiest way to assign a value to a string is to the use the overloaded operator= function. There is also an assign() member function that duplicates some of this functionality.
string& string::operator= (const string& str)
string& string::assign (const string& str)
string& string::operator= (const char* str)
string& string::assign (const char* str)
string& string::operator= (char c)
  • These functions assign values of various types to the string.
  • These functions return *this so they can be “chained”.
  • Note that there is no assign() function that takes a single char.
Sample code:
01string sString;
02 
03// Assign a string value
04sString = string("One");
05cout << sString << endl;
06 
07const string sTwo("Two");
08sString.assign(sTwo);
09cout << sString << endl;
10 
11// Assign a C-style string
12sString = "Three";
13cout << sString << endl;
14 
15sString.assign("Four");
16cout << sString << endl;
17 
18// Assign a char
19sString = '5';
20cout << sString << endl;
21 
22// Chain assignment
23string sOther;
24sString = sOther = "Six";
25cout << sString << " " << sOther << endl;
Output:
One
Two
Three
Four
5
Six Six
The assign() member function also comes in a few other flavors:
string& string::assign (const string& str, size_type index, size_type len)
  • Assigns a substring of str, starting from index, and of length len
  • Throws an out_of_range exception if the index is out of bounds
  • Returns *this so it can be “chained”.
Sample code:
1const string sSource("abcdefg");
2string sDest;
3 
4sDest.assign(sSource, 2, 4); // assign a substring of source from index 2 of length 4
5cout << sDest << endl;
Output:
cdef
string& string::assign (const char* chars, size_type len)
  • Assigns len characters from the C-style array chars
  • Ignores special characters (including ‘\0′)
  • Throws an length_error exception if the result exceeds the maximum number of characters
  • Returns *this so it can be “chained”.
Sample code:
1string sDest;
2 
3sDest.assign("abcdefg", 4);
4cout << sDest << endl;
Output:
abcd
This function is potentially dangerous and it’s use is not recommended.
string& string::assign (size_type len, char c)
  • Assigns len occurrences of the character c
  • Throws an length_error exception if the result exceeds the maximum number of characters
  • Returns *this so it can be “chained”.
Sample code:
1string sDest;
2 
3sDest.assign(4, 'g');
4cout << sDest << endl;
Output:
gggg
Swapping
If you have two strings and want to swap their values, there are two functions both named swap() that you can use.
void string::swap (string &str)
void swap (string &str1, string &str2)
  • Both functions swap the value of the two strings. The member function swaps *this and str, the global function swaps str1 and str2.
  • These functions are efficient and should be used instead of assignments to perform a string swap.
Sample code:
1string sStr1("red");
2string sStr2("blue);
3 
4cout << sStr1 << " " << sStr2 << endl;
5swap(sStr1, sStr2);
6cout << sStr1 << " " << sStr2 << endl;
7sStr1.swap(sStr2);
8cout << sStr1 << " " << sStr2 << endl;
Output:
red blue
blue red
red blue

No comments: