一、
string 是C++ 标准库提供的字符串方法
string 构造
string 控制台输入
string 运算
使用类的关键是要知道它的公有接口(私有部分无法访问),以下代码以及文本表述都是针对string 的公有接口(public) 部分
string 调试接口
ostream& operator << (ostream & lft, string& that);
size_t string::length(void);
1、string 的构造函数 丰富的重载形式
string 构造函数的重载版本有如下:
#1 string(char const* s)
#2 string(size_type n, char c)
#3 string(string const& that)
#4 string()
#5 string(char const *s, size_type n)
#6
template < typename Iter>
string(Iter begain, Iter end)
#7 string(string const &str, size_type pos = 0, size_type n = npos)
C++ 11 标准之后支持下面两个接口
#8 string(string && str) noexcept
#9 string(initializer_list il)
#include <iostream>
#include <string>
int main(void)
{
using namespace std;
string str1("Hello world"); //#1-num = 11
string str2(10,'A'); //#2 num=10
string str3(str1); //#3 mum=11
string str4();//#4
string str5("That is ok", 5); //#5 num=5
char allc[] = "All is well that ends well";
string str6(allc + 1, allc +10); //#6
string str7_1(str1, 0); //#7 npos/size= string::npos num=11
string str7_2(str1,0,2);//#7 npos/size= 2 num=2
//Use debug
cout << str1.length() << endl;
cout << str2.length() << endl;
cout << str3.length() << endl;
cout << "str4 can not use this function,Why speciallize?" << endl;
//str4.length();
cout << str5.length() << endl;
cout << str6.length() << endl;
cout << str7_1.length() << endl;
cout << str7_2.length() << endl;
cout << "string::npos = " << string::npos << endl;
return 0;
}
1.2 string 输入
Eg1 标准io流对象 获取输入
#include <string>
#include <iostream>
using namespace std;
int main(void){
string stuff1,stuff2;
/*#1 input from cin, read a word keep \n in cin*/
cin >> stuff1 >> stuff2;
cout << "#1 " << stuff1 << " " << stuff2 << endl;
/*#2 input from cin, read a line discard \n in cin*/
getline(cin,stuff1);
cout << stuff1 << endl;
getline(cin,stuff2);
cout << stuff2 << endl;
return 0;
}
Eg2 从标准文件流 获取输入
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
int main() {
using namespace std;
ifstream fin;
fin.open("tobuy.txt");
if(fin.is_open() == false) {
cerr << "can not open file" << endl;
exit(EXIT_FAILURE);
}
string item;
int count = 0;
getline(fin, item,':');
/*if input if good after getline*/
while(fin) {
cout << ++count << ": " << item <<endl;
getline(fin, item,':');
}
cout << "Done" << endl;
return 0;
}
1.3 string 使用方法
字符串比较
#include <string>
#include <iostream>
using namespace std;
int main(void) {
string snake1("cobra");
string snake2("cora1");
if (snake1 < snake2) //operator< (string const&, const string&);
cout << "case 1" <<endl;
if (snake1 != snake2) //operator != (string const&, char const *)
cout << "case 2" << endl;
if (snake1 == snake2) //operator== (char const*, string const&)
cout <<"case 3" << endl;
return 0;
}
1.4 字符串检索
find 查找首个出现的子字符串或者字符
size_type find(string const& str, size_type pos = 0) const;
size_type find(char const *, size_type pos = 0) const;
size_type find(char const *, size_type pos, size_type n) const;
size_type find (char ch, size_type pos = 0) const;
rfind 查找字符或者字符串最后出现的位置,重载参数表与find一致
find_first_of 查找子字符串任意字符首次出现的位置
find_last_of 查找子字符串任意字符最后出现的位置
与之对应
find_first_not_of 查找非子符串任意字符首次出现的位置
find_last_not_of 查找非子符串任意字符最后出现的位置