===6.3.2使用string对象===
*链接字符串*
string description=adjective + " " + word;
_Note_: 不能连接两个字符串字面量,以下的语句是错误的
string test= "I have" + "a dream";
*读取字符串*
getline(cin, text);
getline(cin, text, '#')_;
word[index];
* substr(index0, length);
string phrase = "the higher the fewer";
string wold= phrase.substr(4,6 );
*操作符*
> >= < !=
*compare()*
world.compare("end");
word.compare(2, 4, "end");
string sentence = "Manners maketh man";
int a= sentence.find("man");
int b= sentence.find("man", index);//从index開始搜索
假设没有找到。返回~~string:npos~~
*搜索字符集*
string separeators= " ,.\"";
sentence.find_first_of(separeators);
sentence.find_last_of(separeators);
sentence.find_last_not_of(separeators);
sentence.find_first_not_of(separeators);
*逆向搜索*
sentence.rfind("man");
*1.insert*
_`mystring.insert(index, anotherstring)`
sentence.insert(13, world, 8, 5);//从index=8開始之后的5个字符
把几个同样字符串插入到string对象中:sentence.insert(index,times, string);
*2.replace*
将从index開始的num个字符,替换成新的string:sentence.replace(index, num, string);
*3.erase*
删除从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
2.迭代器:
begin();
end();
3.capacity:
size();
length();
clear();
empty();
4.Element Access:
operator[]
at()
back();
front();
5.Modifier
operator+=
append()
push_back()
assign()
insert()
erase()
replcace();
swap();
pop_back();
6.String Operations:
copy();
find();
find_first_of();
find_last_of();
find_first_not_of();
find_last_not_of();
substr();
compare();
7.Member constants:
npos
8.Non-member function overloads:
getline();
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5183956.html,如需转载请自行联系原作者