C++ String Library - constructor


Advertisements

Description

It is used to construct a string object.and initializing its value depending on the constructor version used.

Declaration

Following is the declaration for std::string::string.

string();

Parameters

  • str − It is an another string object.

  • pos − It contains the position of first string character.

  • len − It contains the lenght of sub string.

  • s − Pointer to an array of characters.

  • n − It contains the information about number of characters to copy.

  • c − Character to fill the string.

  • first, last − It is an nput iterator to find the initial and final positions in a range.

  • il − It is an initializer_list object.

Return Value

none

Exceptions

Never throw any exceptiions.

Example

In below example for std::string::string.

#include <iostream>
#include <string>

int main () {
   std::string s0 ("initial string");
   std::string s1;
   std::string s2 (s0);
   std::string s3 (s0, 8, 3);
   std::string s4 ("A character sequence", 6);
   std::string s5 ("Another character sequence");
   std::string s6a (10, 'x');
   std::string s6b (10, 42);
   std::string s7 (s0.begin(), s0.begin()+7);

   std::cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3;
   std::cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6a: " << s6a;
   std::cout << "\ns6b: " << s6b << "\ns7: " << s7 << '\n';
   return 0;
}

The sample output should be like this −

s1:
s2: initial string
s3: str
s4: A char
s5: Another character sequence
s6a: xxxxxxxxxx
s6b: **********
s7: initial
string.htm
Advertisements