C++ std::String














































C++ std::String



 The string header file in C++ allows us to use string objects. Previously we didn't have
 these objects, we then used the strings as an array of characters but now strings are more 
 easier to use. There are 9 ways by which the strings can be initialized in C++11:

 (1) empty string constructor (default constructor)
       Constructs an empty string, with a length of zero characters.
            string();
(2) copy constructor Constructs a copy of str.
            string (const string& str);
(3) substring constructor Copies the portion of str that begins at the character position pos and spans len
          characters (or until the end of str, if either str is too short or if len is
              string::npos).
            string (const string& str, size_t pos, size_t len = npos);

 (4) from c-string
       Copies the null-terminated character sequence (C-string) pointed by s.
            string (const char* s);
(5) from buffer Copies the first n characters from the array of characters pointed by s.
            string (const char* s, size_t n);
(6) fill constructor Fills the string with n consecutive copies of character c.
            string (size_t n, char c);
             
 (7) range constructor
       Copies the sequence of characters in the range [first,last), in the same order.
            template <class InputIterator>
            string  (InputIterator first, InputIterator last); 
 (8) initializer list
       Copies each of the characters in il, in the same order.
            string (initializer_list<char> il);
(9) move constructor Acquires the contents of str. str is left in an unspecified but valid state.
            string (string&& str) noexcept;

 Parameters used are: 

 str: It is another string object, whose value is either copied or acquired.
 pos: Position of the first character in str that is copied to the object as a substring. If
      this is greater than str's length, it throws out_of_range.
      Note: The first character in str is denoted by a value of 0 (not 1).
 len: Length of the substring to be copied (if the string is shorter, as many characters as
      possible are copied). A value of string::npos indicates all characters until the end
      of str.
 s: Pointer to an array of characters (such as a c-string).
 n: Number of characters to copy.
 c: Character to fill the string with. Each of the n characters in the string will be
    initialized to a copy of this value.
 first, last: Input iterators to the initial and final positions in a range. The range used
              is [first,last), which includes all the characters between first and last,
              including the character pointed by first but not the character pointed by last.
              The function template argument InputIterator shall be an input iterator type
              that points to elements of a type convertible to char.
              If InputIterator is an integral type, the arguments are casted to the proper
              types so that signature (5) is used instead.
 il: An initializer_list object. These objects are automatically constructed from initializer
     list declarators.

 Code:

#include <iostream>
#include <string>

using namespace std;
int main ()
{
string s0 ("CppSecrets");

// constructors used in the same order as described above:
string s1;
string s2 (s0);
string s3 (s0, 0, 3);
string s4 ("An array of characters");
string s5 ("Another character sequence", 12);
string s6a (10, 'v');
string s6b (10, 42); // 42 is the ASCII code for '*'
string s7 (s0.begin(), s0.begin()+9);

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

Comments