===6.3.2使用string对象===
string test= "I have" + "a dream";
word[index];
string wold= phrase.substr(4,6 );
word.compare(2, 4, "end");
sentence.rfind("man");
删除从index开始的num个字符:sentence.erase(index, num);
===6.3.8注意事项===
1) append函数与char 和char*
append函数的原型如下:
string& append (const string& str); string& append (const string& str, size_t subpos, size_t sublen); string& append (const char* s); string& append (const char* s, size_t n); string& append (size_t n, char c);注意,char* 和char用作append的参数时候,例如,想在string尾部增加一个字符,需要用mystring.append(1, ch) 而不是mystring.apend(ch). 同样,想将chars的其中一部分插入string中,需要用mystring.append(s+a, b-a)
注意,如果仅仅想增加一个字符,使用push_back即可
string 详解
1.*string constructor*
default (1) string();
copy (2) string (const string& str);
substring (3) string (const string& str, size_t pos, size_t len = npos);
from c-string (4) string (const char* s);
from buffer (5) string (const char* s, size_t n);
fill (6) string (size_t n, char c);
range (7) template <class InputIterator>
string (InputIterator first, InputIterator last);
// string constructor #include <iostream> #include <string> int main () { std::string s0 ("Initial string"); // constructors used in the same order as described above: std::string s1; std::string s2 (s0); std::string s3 (s0, 8, 3); std::string s4 ("A character sequence", 6); std::string s5 ("Another character sequence"); std::string s6 (10, 'x'); std::string s7a (10, 42); // 42 is the ASCII code for '*' std::string s7b (s0.begin(), s0.begin()+7); std::cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3; std::cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6: " << s6; std::cout << "\ns7a: " << s7a << "\ns7b: " << s7b << '\n'; return 0; } **output**: s1: s2: Initial string s3: str s4: A char s5: Another character sequence s6: xxxxxxxxxx s7a: ********** s7b: Initial
C++ 常用类 string类,布布扣,bubuko.com
原文地址:http://blog.csdn.net/trochiluses/article/details/30221317