码迷,mamicode.com
首页 > 其他好文 > 详细

string函数

时间:2017-11-04 00:26:39      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:log   false   com   use   for   points   inter   assign   hang   

A string is like an array of char, but it also supports copying, assignment, and comparison, and its size may be set or changed at run time. ‘\0‘ has no special meaning. There is implicit conversion from char* to string in mixed type expressions.
  string()           // Empty string
  string(cp)         // Convert char* cp to string
  string(n, c)       // string of n copies of char c
  s=s2               // Assign char* or string s2 to string s
  s1<s2              // Also ==, !=, >, <=, >=, either s1 or s2 may be char*
  s.size()           // Length of string s
  string::size_type  // Type of s.size(), usually unsigned int
  s.empty()          // True if s.size() == 0
  s[i]               // i‘th char, 0 <= i < s.size() (unchecked), may be assigned to
  s.at(i)            // s[i] with bounds check, throws out_of_range
  s1+s2              // Concatenate strings, either s1 or s2 may be char or char*
  s+=s2              // Append string, char, or char* s2 to string s
  s.c_str()          // string s as a const char* with trailing ‘\0‘
  s.substr(i, j)     // Substring of string s of length j starting at s[i]
  s.substr(i)        // Substring from s[i] to the end
  s.find(s2)         // Index of char, char*, or string s2 in s, or string::npos if not found
  s.rfind(s2)        // Index of last occurrence of s2 in s
  s.find_first_of(s2)     // Index of first char in s that occurs in s2
  s.find_last_of(s2)      // Index of last char in s that occurs in s2
  s.find_first_not_of(s2) // Index of first char in s not found in s2
  s.find_last_not_of(s2)  // Index of last char in s not found in s2
  s.replace(i, j, s2)     // Replace s.substr(i, j) with s2
s.size() should be converted to int to avoid unsigned comparison.
  string s(3,‘a‘);   // "aaa"
  s += "b"+s;        // "aaabaaa"
  for (int i=0; i!=int(s.size()); ++i) {  // print s one char at a time
    cout << s[i];
  s.size() > -1;     // false!  -1 is converted to unsigned
string supports standard container operations with regard to iterators. string iterators are random, supporting all the pointer operators of char*. The notation [b,e) means the sequence such that pointer or iterator b points to the first element and e points one past the last element.
  s.begin()          // Iterator pointing to s[0]
  s.end()            // Iterator pointing 1 past last char
  string::iterator   // Iterator type, like char*
  string::const_iterator  // Type if s is const, like const char*
  string(b, e)       // string initialized from sequence [b,e)
  s.erase(b)         // Remove char in s pointed to by b
  s.erase(b, e)      // Remove substring [b,e) from s
  s.replace(b, e, s2)  // Replace substring [b,e) with string s2
Conversion from iterator to const_iterator is allowed, but not the other way. const_iterator should be used if the string is not going to be modified.
  char* cp="ABCDE";
  string s(cp, cp+5); // "ABCDE"
  string s2(s.begin()+1, s.end()-1);  // "BCD"
  for (string::const_iterator p=s.begin(); p!=s.end(); ++p)  // Print s one char at a time
    cout << *p;       // or p[0] 
As with arrays and pointers, indexing and iterator dereferencing are not checked at run time. Creating a string with a negative or very large size is also trouble.
  string s(-1, ‘x‘);              // Crash, negative size
  string s2(s.end(), s.begin());  // Crash, negative size
  s[-1]=‘x‘;                      // Crash, out of bounds
  *s.end()=‘x‘;                   // Crash, out of bounds
  string::iterator p; *p=‘x‘;     // Crash, dereferencing uninitialized iterator

 

string函数

标签:log   false   com   use   for   points   inter   assign   hang   

原文地址:http://www.cnblogs.com/enyala/p/7780515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!