|
My Training Period: xx hours
Program examples in this Module compiled using Visual C++ 6.0 with SP6 and Visual C++ .Net. g++ (GNU C++ run on my Fedora 3 machine) example is given at the end of this Module. Take note also for the codes that span more than one line, which they are not supposed to. The source code for this tutorial is available at C++ Characters & Strings source codes.
The C++ character and string programming abilities that supposed to be mastered:
What do we have in this session?
25.1 Introduction
25.2 The <string> C++ Standard Library
#include <string>
|
The following table is a list of the <string> typedefs giving a string and wstring new names to basic_string.
Type |
Description |
string
typedef basic_string<char> string; e.g. const basic_string <char> str1("TEST"); or string str2("TEST");
|
A type that describes a specialization of the template class basic_string with elements of type char as a string. This means basic_string<char> is synonym to string. |
wstring
typedef basic_string<wchar_t> wstring; e.g. const basic_string <wchar_t> str1(L"EST"); or wstring str2(L"EST");
|
A type that describes a specialization of the template class basic_string with elements of type wchar_t as a wstring. This means basic_string<wchar_t> is synonym to wstring. This is wide char, normally 2 byte such as Unicode character set (more information on this is discussed HERE and the implementation is HERE). |
Table 25.1 string and wstring typedefs |
// char and wchar_t types
#include <string>
#include <iostream>
using namespace std;
int main( )
{
const basic_string <char> str1("TEST");
// uses the typedef for string word synonym to basic_string <char>
string str2("TEST");
// simple comparison between two objects of type basic_string
cout<<"String str1: "<<str1<<"\nString str2: "<<str2<<endl;
cout<<"Operation: (str1 == str2)"<<endl;
if(str1 == str2)
cout<<"Strings str1 & str2 are equal."<<endl;
else
cout<<"Strings str1 & str2 are not equal."<<endl;
// L - literal qualifier, long
const basic_string <wchar_t> str3(L"TESTING");
// uses the typedef for wstring word synonym to basic_string <wchar_t>
wstring str4(L"JUMPING");
// simple comparison between two objects of type basic_string <wchar_t>
cout<<"\nString str3: TESTING \nString str4: JUMPING"<<endl;
cout<<"Operation: (str3 == str4)"<<endl;
if(str3 == str4)
cout<<"Strings str3 & str4 are equal."<<endl;
else
cout<<"Strings str3 & str4 are not equal."<<endl;
return 0;
}
Output:
String operators are overloaded operators.
The comparison between string objects is based on what is called a pair wise lexicographical comparison of their characters. Two strings are equal if they have the same number of characters and their respective character values are the same. Otherwise, they are unequal.
A lexicographical comparison between strings compares them character by character until:
It finds two corresponding characters unequal, and the result of their comparison is taken as the result of the comparison between the strings.
It finds no inequalities, but one string has more characters than the other, and the shorter string is considered less than the longer string.
It finds no inequalities and finds that the strings have the same number of characters, and so the strings are equal.
The following table is a list of string operators.
Operator |
Brief Description |
operator!= str1 != str2 |
Tests if the string object on the left side of the operator is not equal to the string object on the right side. |
operator== str1 == str2 |
Tests if the string object on the left side of the operator is equal to the string object on the right side. |
operator< str1 < str2 |
Tests if the string object on the left side of the operator is less than to the string object on the right side. |
operator<< same as in iostream, e.g. cout<< |
A template function that inserts a string into the output stream. |
operator<= str1 <= str2 |
Tests if the string object on the left side of the operator is less than or equal to the string object on the right side. |
operator> str1 > str2 |
Tests if the string object on the left side of the operator is greater than to the string object on the right side. |
operator>= str1 >= str2 |
Tests if the string object on the left side of the operator is greater than or equal to the string object on the right side. |
operator>> same as in iostream, e.g. cin>> |
A template function that extracts a string from the input stream. |
operator+ string str13 = str1 + str3 |
Concatenates two string objects. |
Table 25.2: string operators |
Some of the program examples using string operators.
// the == and != operators
#include <string>
#include <iostream>
using namespace std;
int main( )
{
// declaring an objects of type basic_string<char>
string str1("testingone");
string str2("testingtwo");
cout<<"str1 string is = "<<str1<<endl;
cout<<"str2 string is = "<<str2<<endl;
// declaring a C-style string
char *str3 = "testingone";
cout<<"C-style str3 string is = "<<str3<<endl;
// comparison between left-side object of type basic_string & right-side object of type basic_string
cout<<"\nOperation: (str1 != str2)"<<endl;
if(str1 != str2)
cout<<"str1 & str2 are not equal."<<endl;
else
cout<<"str1 & str2 are equal."<<endl;
// comparison between left-side object of C-style string type & right-side object of type basic_string
cout<<"\nOperation: (str3 != str2)"<<endl;
if(str3 != str2)
cout<<"str3 & str2 are not equal."<<endl;
else
cout<<"str3 & str2 are equal."<<endl;
// comparison between left-side object of type basic_string & right-side object of C-style string type
cout<<"\nOperation: (str1 != str3)"<<endl;
if(str1 != str3)
cout<<"str1 & str3 are not equal."<<endl;
else
cout<<"str1 & str3 are equal."<<endl;
// comparison between left-side object of type basic_string & right-side object of type basic_string
cout<<"\nOperation: (str1 == str2)"<<endl;
if(str1 == str2)
cout<<"str1 & str2 are equal."<<endl;
else
cout<<"str1 & str2 are not equal."<<endl;
// comparison between left-hand object of C-style string type & right-hand object of type basic_string
cout<<"\nOperation: (str3 == str2)"<<endl;
if(str3 == str2)
cout<<"str3 & str2 are equal."<<endl;
else
cout<<"str3 & str2 are not equal."<<endl;
// comparison between left-hand object of type basic_string & right-hand object of C-style string type
cout<<"\nOperation: (str1 == str3)"<<endl;
if(str1 == str3)
cout<<"str1 & str3 are equal."<<endl;
else
cout<<"str1 & str3 are not equal."<<endl;
return 0;
}
![]() |
// the < and > operators
#include <string>
#include <iostream>
using namespace std;
int main()
{
// declaring objects of type basic_string<char>
string str1("testingthree");
string str2("testingtwo");
cout<<"str1 is = "<<str1<<endl;
cout<<"str2 is = "<<str2<<endl;
// declaring a C-style string
char *str3 = "testingone";
cout<<"str3 C-style string is = "<<str3<<endl;
// a comparison between left-side object of type basic_string & right-side object of type basic_string
cout<<"\nOperation: (str1 < str2)"<<endl;
if(str1 < str2)
cout<<"str1 is less then string str2."<<endl;
else
cout<<"str1 is not less then string str2."<<endl;
// comparison between left-side object of C-style string type & right-side object of type basic_string
cout<<"\nOperation: (str3 < str2)"<<endl;
if(str3 < str2)
cout<<"str3 is less then string str2."<<endl;
else
cout<<"str3 is not less then string str2."<<endl;
// comparison between left-side object of type basic_string & right-side object of C-style string type
cout<<"\nOperation: (str1 < str3)"<<endl;
if(str1 < str3)
cout<<"str1 is less then string str3."<<endl;
else
cout<<"str1 is not less then string str3."<<endl;
// comparison between left-side object of type basic_string & right-side object of type basic_string
cout<<"\nOperation: (str1 > str2)"<<endl;
if(str1 > str2)
cout<<"str1 is greater then string str2."<<endl;
else
cout<<"str1 is not greater then string str2."<<endl;
// comparison between left-hand object of C-style string type & right-hand object of type basic_string
cout<<"\nOperation: (str3 > str2)"<<endl;
if(str3 > str2)
cout<<"str3 is greater then string str2."<<endl;
else
cout<<"str3 is not greater then string str2."<<endl;
// comparison between left-hand object of type basic_string & right-hand object of C-style string type
cout<<"\nOperation: (str1 > str3)"<<endl;
if(str1 > str3)
cout<<"str1 is greater then string str3."<<endl;
else
cout<<"str1 is not greater then string str3."<<endl;
return 0;
}
// the >= and <= operators
#include <string>
#include <iostream>
using namespace std;
int main( )
{
// declaring an objects of type basic_string<char>
string str1("testingone");
string str2("testingtwo");
cout<<"str1 string is = "<<str1<<endl;
cout<<"str2 string is = "<<str2<<endl;
// declaring a C-style string
char *str3 = "testingone";
cout<<"str3 C-style string is = "<<str3<<endl;
// comparison between left-side object of type basic_string & right-side object of type basic_string
cout<<"\nOperation: (str1 <= str2)"<<endl;
if(str1 <= str2)
cout<<"str1 is less than or equal to str2."<<endl;
else
cout<<"str1 is not less than or equal to str2."<<endl;
// a comparison between left-side object of C-style string type & right-side object of type basic_string
cout<<"\nOperation: (str3 <= str2)"<<endl;
if(str3 <= str2)
cout<<"str3 is less than or equal to str2."<<endl;
else
cout<<"str3 is not less than or equal to str2."<<endl;
// comparison between left-side object of type basic_string & right-side object of C-style string type
cout<<"\nOperation: (str1 <= str3)"<<endl;
if(str1 <= str3)
cout<<"str1 is less than or equal to str3."<<endl;
else
cout<<"str1 is not less than or equal to str3."<<endl;
// comparison between left-side object of type basic_string & right-side object of type basic_string
cout<<"\nOperation: (str1 >= str2)"<<endl;
if(str1 >= str2)
cout<<"str1 is greater than or equal to str2."<<endl;
else
cout<<"str1 is not greater than or equal to str2."<<endl;
// comparison between left-hand object of C-style string type & right-hand object of type basic_string
cout<<"\nOperation: (str3 >= str2)"<<endl;
if(str3 >= str2)
cout<<"str3 is greater than or equal to str2."<<endl;
else
cout<<"str3 is not greater than or equal to str2."<<endl;
// comparison between left-hand object of type basic_string & right-hand object of C-style string type
cout<<"\nOperation: (str1 >= str3)"<<endl;
if(str1 >= str3)
cout<<"str1 is greater than or equal to str3."<<endl;
else
cout<<"str1 is not greater than or equal to str3."<<endl;
return 0;
}
The operator skips the leading white spaces unless the skipws flag is set. It reads all the following characters until the next character is a white space or the end of the file is reached.
// the << and >> operators
#include <string>
#include <iostream>
using namespace std;
int main()
{
string Sample = "Testing the << and >> operators.";
string Var1, Var2;
cout<<Sample<<endl;
cout<<"Enter a string or a word: ";
cin>>Var1;
cout<<"Enter another string or a word: ";
cin>>Var2;
cout<<"The strings entered are: "<<Var1<<" and "<<Var2<<endl;
return 0;
}
// concatenating using ‘+’ operator
#include <string>
#include <iostream>
using namespace std;
int main()
{
// declaring an object of type basic_string<char>
string str1("StringOne");
string str2("StringTwo");
// declaring a C-style string
char *str3 = "StringThree";
// declaring a character constant
char chr = '?';
cout<<"str1 string is = "<<str1<<endl;
cout<<"str2 string is = "<<str2<<endl;
cout<<"str3 C-style string is = "<<str3<<endl;
cout<<"A character constant chr is = "<<chr<<endl;
// concatenates an object of type basic_string with an object of type basic_string
cout<<"\nOperation: str12 = str1 + str2"<<endl;
string str12 = str1 + str2;
cout<<"str12 = "<<str12<<endl;
// concatenates an object of type basic_string with an object of C-style string type
cout<<"\nOperation: str13 = str1 + str3"<<endl;
string str13 = str1 + str3;
cout<<"str13 = "<<str13<<endl;
// concatenates an object of type basic_string with a character constant
cout<<"\nOperation: str13chr = str13 + chr"<<endl;
string str13chr = str13 + chr;
cout<<"str13chr = "<<str13chr<<endl;
return 0;
}
tenouk C++ STL programming tutorial
The source code for this tutorial is available at C++ Characters & Strings source codes.
A complete C & C++ Standard Library documentation that includes STL.
Check the best selling C / C++, STL and UML books at Amazon.com.