目录
参考视频:
第2章:string容器
2.1 string容器基本概念
C++标准库定义了一种string类,定义在头文件<string>。
String和c风格字符串对比:
- Char*是一个指针,string是一个类:string封装了char*,管理这个字符串,是一个char*型的容器。
- String封装了很多实用的成员方法:查找find,拷贝copy,删除delete 替换replace,插入insert
- 不用考虑内存释放和越界:string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。
2.2 string构造函数
string();//创建一个空的字符串 例如: string str;
string(const string& str);//使用一个string对象初始化另一个string对象
string(const char* s);//使用字符串s初始化
string(int n, char c);//使用n个字符c初始化
示例:
/*
string(); //创建一个空的字符串 例如: string str;
string(const char* s); //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, char c); //使用n个字符c初始化
*/
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s1;//创建一个空的字符串s1,调用无参构造函数
const char* str = "hello world";
//使用字符串str初始化s2
string s2(str);
cout << "s2 = " << s2 << endl;
//调用拷贝构造函数
string s3(s2);
cout << "s3 = " << s3 << endl;
//使用10个字符a初始化
string s4(10, 'a');
cout << "s4 = " << s4 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2.3 string赋值操作
string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
string& operator=(const string &s); //把字符串s赋给当前的字符串
string& operator=(char c); //字符赋值给当前的字符串
string& assign(const char *s); //把字符串s赋给当前的字符串
string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s); //把字符串s赋给当前字符串
string& assign(int n, char c); //用n个字符c赋给当前字符串
string& assign(const string &s, int start, int n); //将s从start开始n个字符赋值给字符串
示例:
/*string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
string& operator=(const string &s); //把字符串s赋给当前的字符串
string& operator=(char c); //字符赋值给当前的字符串
string& assign(const char *s); //把字符串s赋给当前的字符串
string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s); //把字符串s赋给当前字符串
string& assign(int n, char c); //用n个字符c赋给当前字符串*/
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s1;//创建一个空的字符串s1,调用无参构造函数
const char* str = "hello world";
string s2(str);//使用字符串str初始化s2
cout << "s2 = " << s2 << endl;
string s3;
s3.assign("hello c++", 5);
cout << "s3 = " << s3 << endl;
string s4;
s5.assign(s3);
cout << "s4 = " << s4 << endl;
string s5;
s5.assign(5, 'x');
cout << "s5 = " << s5 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2.4 string存取字符
char& operator[](int n); //通过[]方式取字符
char& at(int n); //通过at方法获取字符
示例:
#include <iostream>
#include <string>
using namespace std;
//字符存取
void test()
{
string str = "hello world";
//字符修改
str[0] = 'x';
str.at(1) = 'x';
cout << str << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2.5 string拼接操作
string& operator+=(const string& str); //重载+=操作符
string& operator+=(const char* str); //重载+=操作符
string& operator+=(const char c); //重载+=操作符
string& append(const char *s); //把字符串s连接到当前字符串结尾
string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s); //同operator+=()
string& append(const string &s, int pos, int n); //把字符串s中从pos开始的n个字符连接到当前字符串结尾
string& append(int n, char c); //在当前字符串结尾添加n个字符c
示例:
#include <iostream>
#include <string>
using namespace std;
//字符串拼接
void test()
{
string s1 = "我";
s1 += "爱学习";
cout << "s1 = " << s1 << endl;
s1 += ":";
cout << "s1 = " << s1 << endl;
string s2 = "C++ STL";
s1 += s2;
cout << "s1 = " << s1 << endl;
string s3 = "I";
s3.append(" love ");
s3.append("C++ STL", 4);
cout << "s3 = " << s3 << endl;
//从下标4开始,截取3个字符,拼接到字符串末尾
s3.append(s2, 4, 3);
cout << "s3 = " << s3 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2.6 string查找和替换
int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找
int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置
int find(const char c, int pos = 0) const; //查找字符c第一次出现位置
int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找
int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找
int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置
int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s
示例:
#include <iostream>
#include <string>
using namespace std;
//string查找与替换
void test()
{
//查找
//code1 查找第一次出现的位置
string s1 = "abcdefgde";
int pos1 = s1.find("de");
if (pos1 == -1)
{
cout << "未找到" << endl;
}
else
{
cout << "pos1 = " << pos1 << endl;
}
//code2 查找最后一次出现的位置
int pos2 = s1.rfind("de");
if (pos2 == -1)
{
cout << "未找到" << endl;
}
else
{
cout << "pos2 = " << pos2 << endl;
}
//code3 替换
string s2 = "abcdefde";
s2.replace(1, 3, "1111");
cout << "s2 = " << s2 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2.7 string比较操作
// compare函数在>时返回 1,<时返回 -1,==时返回 0。
// 比较区分大小写,比较时参考字典顺序,排越前面的越小,大写的A比小写的a小。
int compare(const string &s) const; //与字符串s比较
int compare(const char *s) const; //与字符串s比较
示例:
#include <iostream>
#include <string>
using namespace std;
//字符串比较
void test()
{
string s1 = "hello";
string s2 = "string";
int ret = s1.compare(s2);
if (ret == 0)
{
cout << "s1 = s2" << endl;
}
else if (ret > 0)
{
cout << "s1 > s2" << endl;
}
else
{
cout << "s1 < s2" << endl;
}
}
int main()
{
test();
system("pause");
return 0;
}
2.8 string子串
string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
示例:
#include <iostream>
#include <string>
using namespace std;
//获取子串
void test()
{
string s1 = "abcdefg";
string subStr = s1.substr(1, 3);
cout << "subStr = " << subStr << endl;
//应用
string email = "zhangsan@qq.com";
int pos = email.find("@");
string username = email.substr(0, pos);
cout << "username = " << username << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2.9 string插入和删除操作
string& insert(int pos, const char* s); //插入字符串
string& insert(int pos, const string& str); //插入字符串
string& insert(int pos, int n, char c); //在指定位置插入n个字符c
string& erase(int pos, int n = npos); //删除从Pos开始的n个字符
示例:
#include <iostream>
#include <string>
using namespace std;
//字符串插入和删除
void test()
{
string str = "hello";
str.insert(1, "111");
cout << str << endl;
str.erase(1, 3);//从下标1位置开始删除三个字符
cout << str << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2.10 string和c-style字符串转换
//string 转 char*
string str = "itcast";
const char* cstr = str.c_str();
//char* 转 string
char* s = "itcast";
string str(s);