The basic_string string class constructor C++ program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the basic_string constructor to construct 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

To show: How to use the C++ the basic_string to construct 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

 

// the C++ basic_string program example

// basic_string are the Standard C++ string class and are usually referred to as strings, but they should not be confused with the null-terminated C-strings

// used throughout the // Standard C++ Library. The string class is a container that enables the use of strings as normal types, such as using comparison

// and concatenation operations, iterators, and STL algorithms and copying and assigning with class allocator managed memory.

 

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// initializing with a C-string

const char *str1 = "The basic_string";

basic_string <char> str2(str1, 5);

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

cout<<"Operation: str2(str1, 5)"<<endl;

cout<<"str2 initialized by str1 is: "<<str2<<endl<<endl;

 

// initializing with a string

string str3("Initialize with a StRinG?");

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

basic_string <char> str4(str3, 6, 10);

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

cout<<"str4 initialized by part of the str3 string is: "<<str4<<endl<<endl;

 

// initializing a string with a number of characters of a specific value

basic_string <char> str5(6, '7');

cout<<"Operation: str5(6, '7')"<<endl;

cout<<"str5 initialized by six number of 7s is: "<<str5<<endl<<endl;

 

// creating an empty string and string with a specified allocator

basic_string <char> str6;

string str7;

 

// allocate a storage

basic_string <char> str8(str7.get_allocator());

 

// test the emptiness

cout<<"Operation: str8.empty()"<<endl;

if(str8.empty())

cout<<"The string str8 is empty."<<endl<<endl;

else

cout<<"The string str8 is not empty."<<endl<<endl;

 

// fill up some string

str8 = "Not empty!";

cout<<"Operation: str8 = Not empty!"<<endl;

 

// retest again

if(str8.empty())

cout<<"The string str8 is empty."<<endl;

else

cout<<"The string str8 is not empty."<<endl;

cout<<endl;

 

// initializing a string from another range of characters

string str10("Test StRiNg");

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

cout<<"Operation: str11(str10.begin()+5, str10.end())"<<endl<<endl;

 

basic_string <char> str11(str10.begin()+5, str10.end());

cout<<"str11 initialized by another range of characters is: "<<str11<<endl;

 

return 0;

}

 

Output example:

 

str1 string is: The basic_string

 

Operation: str2(str1, 5)

str2 initialized by str1 is: The b

 

str3 string is: Initialize with a StRinG?

 

Operation: str4(str3, 6, 10)

str4 initialized by part of the str3 string is: lize with

 

Operation: str5(6, '7')

str5 initialized by six number of 7s is: 777777

 

Operation: str8.empty()

The string str8 is empty.

 

Operation: str8 = Not empty!

The string str8 is not empty.

 

str10 string is: Test StRiNg

Operation: str11(str10.begin()+5, str10.end())

 

str11 initialized by another range of characters is: StRiNg

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index