Construct:
- 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);
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 caracter 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
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(begin),static_cast(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.
str
Another object of class 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 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.
- #include <iostream>
-
#include <string>
-
using namespace std;
-
-
int
-
main(void)
-
{
-
string s0 ("Initial string");
-
-
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 << endl;
-
cout << "s2:" << s2 << endl;
-
cout << "s3:" << s3 << endl;
-
cout << "s4:" << s4 << endl;
-
cout << "s5:" << s5 << endl;
-
cout << "s6:" << s6 << endl;
-
cout << "s7a:" << s7a << endl;
-
cout << "s7b:" << s7b << endl;
-
-
return (0);
-
}
- string& operator= ( const string& str );
-
string& operator= ( const char* s );
-
string& operator= ( char c );
str
object. A copy of the content of this object is used as the new content for the object.
s
A pointer to an array containing a null-terminated character sequence (C string), which is copied as the new content for the object.
c
Character. The content is set to a single character.
- #include <iostream>
-
#include <string>
-
using namespace std;
-
-
int
-
main(void)
-
{
-
string str1, str2, str3;
-
str1 = "Test string:";
-
str2 = 'x';
-
str3 = str1 + str2;
-
cout << str3 << endl;
-
-
return (0);
-
}
阅读(1227) | 评论(0) | 转发(0) |