Get new post automatically.

Enter your email address:


Construct string object C++

string ( );
string ( const string& str );
string ( const string& str, size_t pos, size_t n = npos );
string ( const char * s, size_t n );
string ( const char * s );
string ( size_t n, char c );
template<class InputIterator> string (InputIterator begin, InputIterator end);

Construct string object
Constructs a standard string object and initializes its content.

The different constructor versions allow for the content to be initialized in different ways:

string ( );
Content is initialized to an empty string.
string ( const string& str );
Content is initialized to a copy of the string object str.
string ( const string& str, size_t pos, size_t n = npos );
Content is initialized to a copy of a substring of str. The substring is the portion of str that begins at the character position pos and takes up to n characters (it takes less than n if the end of str is reached before).
string ( const char * s, size_t n );
Content is initialized to a copy of the string formed by the first n characters in the array of characters pointed by s.
string ( const char * s );
Content is initialized to a copy of the string formed by the null-terminated character sequence (C string) pointed by s. The length of the character sequence is determined by the first occurrence of a null character (as determined by traits.length(s)). This version can be used to initialize a string object using a string literal constant.
string ( size_t n, char c );
Content is initialized as a string formed by a repetition of character c, n times.
template<class InputIterator> string (InputIterator begin, InputIterator end);
If InputIterator is an integral type, behaves as the sixth constructor version (the one right above this) by typecasting begin and end to call it: string(static_cast<size_t>(begin),static_cast<char>(end)); In any other case, the parameters are taken as iterators, and the content is initialized with the values of the elements that go from the element referred by iterator begin to the element right before the one referred by iterator end.


Parameters

str
Another object of class string whose content is used to initialize the content of this.
pos
Starting position of the substring of str that is used to initialize the content. Notice that the first position has a value of 0, not 1. A value for this parameter out of the content of str throws an out_of_range exception.
n
Number of characters to use for the content (i.e., its length). The default value in the third constructor version: npos, is a static member constant with the greatest possible value for a size_t element. In this case all the characters until the end of str are used.
s
Array with a sequence of characters. In the fourth constructor version, the length is determined by parameter n, even including null characters in the content. By contrast, in the fifth constructor, s is a null-terminated character, and therefore its length is determined only by the first occurrence of a null character.
c
Character value to be used to initialize the string content. This character is repeated n times.
begin
If, along with end, both are integers, it is equivalent to parameter n, otherwise it is an iterator referring to the beginning of a sequence of characters.
end
If along with begin, both are integers, it is equivalent to parameter c, otherwise it is an iterator referring to the past-the-end element of a sequence of characters.

Except for the copy constructor, an optional final parameter exists for all basic_string constructors, whose type is its Allocator template argument. This parameter influences the memory allocation model to be used for the object. To provide a better readability, and because under no known compiler implementation the memory allocation model for strings (allocator<char>) is affected by its value, it has not been included in the declarations above, but see the basic template member declaration ahead for a more complete declaration.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// string constructor
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string s0 ("Initial string");

  // constructors used in the same order as described above:
  string s1;
  string s2 (s0);
  string s3 (s0, 8, 3);
  string s4 ("A character sequence", 6);
  string s5 ("Another character sequence");
  string s6 (10, 'x');
  string s7a (10, 42);
  string s7b (s0.begin(), s0.begin()+7);

  cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3;
  cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6: " << s6;
  cout << "\ns7a: " << s7a << "\ns7b: " << s7b << endl;
  return 0;
}


Output:

s1: 
s2: Initial string
s3: str
s4: A char
s5: Another character sequence
s6: xxxxxxxxxx
s7a: **********
s7b: Initial


Basic template member declaration

( basic_string<charT,traits,Allocator> )
1
2
3
4
5
6
7
8
9
10
11
typedef typename Allocator::size_type size_type;
explicit basic_string ( const Allocator& a = Allocator() );
basic_string ( const basic_string& str);
basic_string ( const basic_string& str, size_type pos, size_type n = npos,
               const Allocator& a = Allocator() );
basic_string ( const charT* s, size_type n,
               const Allocator& a = Allocator() );
basic_string ( const charT* s, const Allocator& a = Allocator() );
basic_string ( size_type n, charT c, const Allocator& a = Allocator() );
template<class InputIterator>
   basic_string (InputIterator begin, InputIterator end); 

See also