|< C++ String Class 1 | Main | C++ String Class 3 >| Site Index | Download |


 

 

 

 

 

MODULE 25_1

C++ STRING CLASS TYPES AND CODE SAMPLES 2

 

 

 

 

 

What do we have in this session?

  1. The <string> Specialized Template Functions

  2. swap() example

  3. The <string> Functions

  4. getline() example

  5. The <string> Template Classes

  6. basic_string Template Class

  7. basic_string Template Class Typedef

  8. basic_string Template Class Member Functions

  9. basic_string Template Class Operators

  10. basic_string Program Examples

  11. append() example

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

My Training Period: xx hours

 

25.5  <string> Specialized Template Functions

 

Specialized Template Function

Description

swap()

Exchanges the arrays of characters of two strings.

 

Table 25.3: swap() function

  • If the strings being swapped have the same allocator object, the swap() member function:

  1. Occurs in constant time.

  2. Throws no exceptions.

  3. Invalidates no references, pointers, or iterators that designate elements in the two strings.

  • Otherwise, it performs a number of element assignments and constructor calls proportional to the number of elements in the two controlled sequences.

// swap()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // declaring an object of type basic_string<char>

    string str1("StringOne");

    string str2("StringTwo");

    cout<<"Before swapping string str1 and str2:"<<endl;

    cout<<"str1 string is = "<<str1<<endl;

    cout<<"str2 string is = "<<str2<<endl;

    swap(str1, str2);

    cout<<"\nOperation: swap(str1, str2)"<<endl;

    cout<<"After swapping string str1 and str2:"<<endl;

    cout<<"str1 string is = "<<str1<<endl;

    cout<<"str2 string is = "<<str2<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation swap()

 

25.6  <string> Functions

 

Function

Description

getline()

The getline() function creates a string containing all of the characters from the input stream until one of the following situations occurs:

  1. End of file.

  2. The delimiter is encountered.

  3. is.max_str() elements have been extracted.

 

Table 25.4:  getline() function

// getline()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

   string str;

   string str1;

   string str2;

   cout<<"Enter a line of text: ";

   getline(cin, str);

   cout<<"You entered: "<<str<<endl;

   cout<<"Enter a line of text, <space> as the delimiter: "<<endl;

   getline(cin, str1, ' ');

   cout<<"You entered: "<<str1<<endl;

   return 0;

}

 

Output:

 

C++ STL character and string manipulation getline()

 

25.7  <string> Template Classes

 

Class

Description

basic_string

A template class that describes objects that can store a sequence of arbitrary character-like objects.

char_traits

A template class that describes attributes associated with a character of type CharType.

 

Table 25.5:  <string> classes

 

25.8  basic_string Template Class

template < class CharType, class Traits = char_traits<CharType>, class Allocator = allocator<CharType> >

Entity

Description

CharType

The data type of a single character to be stored in the string. The Standard C++ Library provides two specializations of this template class:

1.  string, for elements of type char,

2.  wstring, for elements of type wchar_t.

Traits

Various important properties of the CharType elements in a basic_string specialization are described by the class Traits.

Allocator

The type that represents the stored allocator object that encapsulates details about the string's allocation and de-allocation of memory. The default value is allocator<Type>.

 

Table 25.6:  basic_string template parameters

 

25.9  basic_string Template Class Typedef

Typedef

Brief Description

allocator_type

A type that represents the allocator class for a string object.  The type is a synonym for the template parameter Allocator.

const_iterator

A type that provides a random-access iterator that can access and read a const element in the string.  A type const_iterator cannot be used to modify the value of a character and is used to iterate through a string in a forward direction.

const_pointer

A type that provides a pointer to a const element in a string.  The type is a synonym for allocator_type::const_pointer.  For type string, it is equivalent to char*.  Pointers that are declared const must be initialized when they are declared. const pointers always point to the same memory location and may point to constant or non constant data.

const_reference

A type that provides a reference to a const element stored in a string for reading and performing const operations.  A type const_reference cannot be used to modify the value of an element.  The type is a synonym for  allocator_type::const_reference. For string type, it is equivalent to const char&.

const_reverse_iterator

A type that provides a random-access iterator that can read any const element in the string.  A type const_reverse_iterator cannot modify the value of a character and is used to iterate through a string in reverse.

difference_type

A type that provides the difference between two iterators those refer to elements within the same string.  The signed integer type describes an object that can represent the difference between the addresses of any two elements in the controlled sequence.  For type string, it is equivalent to ptrdiff_t.

iterator

A type that provides a random-access iterator that can read or modify any element in a string.  A type iterator can be used to modify the value of a character and is used to iterate through a string in a forward direction.

npos

An unsigned integral value initialized to –1 that indicates either "not found" or "all remaining characters" when a search function fails.  When the return value is to be checked for the npos value, it might not work unless the return value is of type size_type and not either int or unsigned.

pointer

A type that provides a pointer to a character element in a string or character array.  The type is a synonym for allocator_type::pointer.  For type string, it is equivalent to char*.

reference

A type that provides a reference to an element stored in a string.  A type reference can be used to modify the value of an element. The type is a synonym for allocator_type::reference.

For type string, it is equivalent to chr&.

reverse_iterator

A type that provides a random-access iterator that can read or modify an element in a reversed string.  A type reverse_iterator can be used to modify the value of a character and is used to iterate through a string in reverse.

size_type

An unsigned integral type for the number of elements in a string.  It is equivalent to allocator_type::size_type.  For type string, it is equivalent to size_t.

traits_type

A type for the character traits of the elements stored in a string.  The type is a synonym for the second template parameter Traits. For type string, it is equivalent to char_traits<char>.

value_type

A type that represents the type of characters stored in a string.  It is equivalent to traits_type::char_type and is equivalent to char for objects of type string.

 

Table 25.7:  basic_string typedefs

 

25.10  basic_string Template Class Member Functions

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Member Function

Brief Description

append()

Adds characters to the end of a string.

assign()

Assigns new character values to the contents of a string.

at()

Returns a reference to the element at a specified location in the string.

basic_string()

Constructs a string that is empty or initialized by specific characters or that is a copy of all or part of some other string object or C-string.

begin()

Returns an iterator addressing the first element in the string.

c_str()

Converts the contents of a string as a C-style, null-terminated, string.

capacity()

Returns the largest number of elements that could be stored in a string without increasing the memory allocation of the string.

clear()

Erases all elements of a string.

compare()

Compares a string with a specified string to determine if the two strings are equal or if one is lexicographically less than the other.

copy()

Copies at most a specified number of characters from an indexed position in a source string to a target character array.

data()

Converts the contents of a string into an array of characters.

empty()

Tests whether the string contains characters or not.

end()

Returns an iterator that addresses the location succeeding the last element in a string.

erase()

Removes an element or a range of elements in a string from specified positions.  Notice the different with clear().

find()

Searches a string in a forward direction for the first occurrence of a substring that matches a specified sequence of characters.

find_first_not_of()

Searches through a string for the first character that is not any element of a specified string.

find_first_of()

Searches through a string for the first character that matches any element of a specified string.

find_last_not_of()

Searches through a string for the last character that is not any element of a specified string.

find_last_of()

Searches through a string for the last character that is an element of a specified string.

get_allocator()

Returns a copy of the allocator object used to construct the string.

insert()

Inserts an element or a number of elements or a range of elements into the string at a specified position.

length()

Returns the current number of elements in a string.

max_size()

Returns the maximum number of characters a string could contain.

push_back()

Adds an element to the end of the string.

rbegin()

Returns an iterator to the first element in a reversed string.

rend()

Returns an iterator that point just beyond the last element in a reversed string.

replace()

Replaces elements in a string at a specified position with specified characters or characters copied from other ranges or strings or C-strings.

reserve()

Sets the capacity of the string to a number at least as great as a specified number.

resize()

Specifies a new size for a string, appending or erasing elements as required.

rfind()

Searches a string in a backward direction for the first occurrence of a substring that matches a specified sequence of characters.

size()

Returns the current number of elements in a string.

substr()

Copies a substring of at most some number of characters from a string beginning from a specified position.

swap()

Exchange the contents of two strings.

 

Table 25.8:  basic_string member functions

 

25.11  basic_string Template Class Operators

Operator

Brief Description

operator+=

Appends characters to a string.

operator=

Assigns new character values to the contents of a string.

operator[ ]

Provides a reference to the character with a specified index in a string.

 

Table 25.9:  basic_string operator

 

25.12  basic_string Program Examples

 

append()

// append()

#include <string>

#include <iostream>

using namespace std;

 

int main()

{

    // appending a C-string to a string

    string str1("Playing ");

    const char *str2 = "with a string";

    cout<<"str1 is: "<<str1<<endl;

    cout<<"str2, C string is: "<<str2<<endl;

    str1.append(str2);

    cout<<"Operation: str1.append(str2)"<<endl;

    cout<<"Appending str2 to str1: "<<str1<<endl;

    // appending part of a C-string to a string

    string str3 ("Replaying ");

    const char *str4 = "the string ";

    cout<<"\nstr3 string is: "<<str3<<endl;

    cout<<"str4 C-string is: "<<str4<<endl;

    str3.append(str4, 6);

    cout<<"Operation: str3.append(str4, 6)"<<endl;

    cout<<"Appending part of the str4 to string str3: \n"<<str3<<endl;

    // appending part of one string to another

    string str5("Again "), str6("string manipulation");

    cout<<"\nstr5 is: "<<str5<<endl;

    cout<<"str6 is: "<<str6<<endl;

    str5.append(str6, 4, 6);

    cout<<"Operation: str5.append(str6, 4, 6)"<<endl;

    cout<<"The appended string is: "<<str5<<endl;

    // appending one string to another in two ways, comparing append and operator [ ]

    string str7("First "), str8("Second "), str9("Third ");

    cout<<"\nstr7 is: "<<str7<<"\nstr8 is: "<<str8<<"\nstr9 is: "<<str9<<endl;

    str7.append(str8);

    cout<<"Operation: str7.append(str8)"<<endl;

    cout<<"The appended string str7 is: "<<str7<<endl;

    str7 += str9;

    cout<<"Operation: str7 += str9"<<endl;

    cout<<"The re appended string is: "<<str7<<endl;

    // appending characters to a string

    string str10("What string");

    cout<<"\nstr10 string is: "<<str10<<endl;

    str10.append(3, '?');

    cout<<"Operation: str10.append(3, '?')"<<endl;

    cout<<"str10 string appended with ? is: "<<str10<<endl;

    // appending a range of one string to another

    string str11("Finally "), str12("comes the END ");

    cout<<"\nstr11 is: "<<str11<<" str12 is: "<<str12<<endl;

    str11.append(str12.begin() + 6, str12.end() - 1);

    cout<<"Operation:\nstr11.append(str12.begin() + 6, str12.end() - 1)"<<endl;

    cout<<"The appended str11 String is: "<<str11<<endl;

    return 0;

}

 

Output:

 

C++ STL character and string manipulation append()

 

tenouk C++ STL string and character programming

 

 

 

 

 

 

 

 

 

 

 

 

 

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++ String Class 1 | Main | C++ String Class 3 >| Site Index | Download |


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