Chinaunix首页 | 论坛 | 博客
  • 博客访问: 367974
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-20 08:57:10

Construct:
  1. string ( );
  2. string ( const string& str );
  3. string ( const string& str, size_t pos, size_t n = npos );
  4. string ( const char * s, size_t n );
  5. string ( const char * s );
  6. string ( size_t n, char c );
  7. 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.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. int
  5. main(void)
  6. {
  7.         string s0 ("Initial string");

  8.         string s1;
  9.         string s2 (s0);
  10.         string s3 (s0, 8, 3);
  11.         string s4 ("A character sequence", 6);
  12.         string s5 ("Another character sequence");
  13.         string s6 (10, 'x');
  14.         string s7a (10, 42);
  15.         string s7b (s0.begin(), s0.begin()+7);

  16.         cout << "s1:" << s1 << endl;
  17.         cout << "s2:" << s2 << endl;
  18.         cout << "s3:" << s3 << endl;
  19.         cout << "s4:" << s4 << endl;
  20.         cout << "s5:" << s5 << endl;
  21.         cout << "s6:" << s6 << endl;
  22.         cout << "s7a:" << s7a << endl;
  23.         cout << "s7b:" << s7b << endl;

  24.         return (0);
  25. }


  1. string& operator= ( const string& str );
  2. string& operator= ( const char* s );
  3. 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.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. int
  5. main(void)
  6. {
  7.         string str1, str2, str3;
  8.         str1 = "Test string:";
  9.         str2 = 'x';
  10.         str3 = str1 + str2;
  11.         cout << str3 << endl;

  12.         return (0);
  13. }


阅读(1219) | 评论(0) | 转发(0) |
0

上一篇:文件I/O

下一篇:归并排序

给主人留下些什么吧!~~