|< C++ STL Templates 2 | Main | C++ String Class 2 >| Site Index | Download |


 

 

 

 

 

MODULE 25

C++ STRING CLASS PROGRAM EXAMPLES 1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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:

 

  • Able to understand and appreciate how the templates are used.

  • Able to understand and use the template based C++ headers.

  • Able to understand and use string template classes in manipulating character and string in C++.

  • Able to understand and use the basic_string template class.

  • Able to recognize the Containers, Iterators and Algorithms.

 

What do we have in this session?

  1. Introduction

  2. The <string> C++ Standard Library

  3. The <string> Typedefs

  4. char and wchar_t types example

  5. <string> Operators

  6. == and != operators example

  7. < and > operators example

  8. >= and <= operators example

  9. << and >> operators example

  10. concatenating using ‘+’ operator example

 

25.1  Introduction

  • Before we ‘jump’ into the real STL ‘bandwagon’, let try the <string> class from the C++ Standard Library.  This library is constructed using template classes and functions.

  • In this Module take note for these words: container, iterator and algorithm.  See how these things used in the program examples.

  • We will discuss these ‘creatures’ in more detail later on.  May be after completing this Module, you may develop your own simple search routine.

  • Your main task here is ‘learn how to use’.

25.2  The <string> C++ Standard Library

  • <string> defines the container template class basic_string and various supporting templates.  You must include the <string> header in your program as shown below.

#include <string>
  • The C++ language supports two types of strings:

  1. Null-terminated character arrays often referred to as C-strings and,

  2. Template class objects, of type basic_string that handles all char-like template arguments.

25.3  The <string> Typedefs

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:

 

C++ STL character and string manipulation wchar_t char

 

25.4  <string> Operators

  1. It finds two corresponding characters unequal, and the result of their comparison is taken as the result of the comparison between the strings.

  2. It finds no inequalities, but one string has more characters than the other, and the shorter string is considered less than the longer string.

  3. It finds no inequalities and finds that the strings have the same number of characters, and so the strings are equal.

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

// 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;

}

 

Output:

C++ STL character and string manipulation operators  

 

// 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;

}

 

Output:

 

C++ STL character and string manipulation 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<<"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;

}

 

Output:

 

C++ STL character and string manipulation operators

// 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;

}

 

Output:

 

C++ STL character and string manipulation operators

 

// 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;

}

 

Output:

 

C++ STL character and string manipulation concat operators

 

tenouk C++ STL programming tutorial

 

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ related reading:

 

  1. The source code for this tutorial is available at C++ Characters & Strings source codes.

  2. A complete C & C++ Standard Library documentation that includes STL.

  3. Check the best selling C / C++, STL and UML books at Amazon.com.

 

 

 

 

 

 

|< C++ STL Templates 2 | Main | C++ String Class 2 >| Site Index | Download |


C++ String Class:  Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7