===6.3.2使用string对象===
string word="I love China"
*链接字符串*
string description=adjective + " " + word;
_Note_: 不能连接两个字符串字面量,下面的语句是错误的
string test= "I have" + "a dream";
===6.3.3访问字符串中的字符===
*读取字符串*
getline(cin, text);
getline(cin, text, ‘#‘)_;
word[index];
===6.3.4访问子字符串===
* substr(index0, length);
string phrase = "the higher the fewer";
string wold= phrase.substr(4,6 );
===6.3.5比较字符串===
*操作符*
> >= < !=
*compare()*
world.compare("end");
word.compare(2, 4, "end");
===6.3.6搜索字符串:find===
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");
===6.3.7修改字符串:insert、replace、erase===
*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();
C++ 常用类 string类,布布扣,bubuko.com