C++提高编程---3.1 常用容器-string 容器【P189~P196】
3.1 string 容器
3.1.1 string 基本概念
本质:
- string是C++风格的字符串,而string本质上是一个类
string和char*区别:
- char*是一个指针
- string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。
特点:
string 类内部封装了很多成员方法
例如︰查找 find,拷贝 copy,删除 delete 替换 replace,插入 insert
string 管理 char* 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
3.1.2 string 构造函数
构造函数原型:
string();
// 创建一个空的字符串 例如:string strstring(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 test01()
{
string s1; // 默认构造
// C语言风格的字符串
const char * str = "hello world";
string s2(str);
cout << "s2 = " << s2 << endl;
string s3(s2);
cout << "s3 = " << s3 << endl;
string s4(10, 'a');
cout << "s4 = " << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.3 string 赋值操作
构造函数是对对象进行初始化操作,那么初始化过后可以对其进行新的赋值操作
功能描述:
- 给 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);
//把字符串的前 n 个字符赋值给当前的字符串string& assign(const string &s);
//把字符串 s 赋值给当前字符串string& assign(int n, char c);
//用 n 个字符c 赋值给当前字符串
# include<iostream>
# include<string>
using namespace std;
void test01()
{
// 1
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
// 2
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
// 3
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
// 4
string str4;
str4.assign("hello C++");
cout << "str4 = " << str4 << endl;
// 5
string str5;
str5.assign("hello C++", 5);
cout << "str5 = " << str5 << endl;
// 6
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
// 7
string str7;
str7.assign(10, 'b');
cout << "str7 = " << str7 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.4 string 字符串拼接
# include<iostream>
# include<string>
using namespace std;
void test01()
{
// 1
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1 = " << str1 << endl;
// 2
str1 += ':';
cout << "str1 = " << str1 << endl;
// 3
string str2 = " LOL NaN";
str1 += str2;
cout << "str1 = " << str1 << endl;
// 4
string str3 = "I";
str3.append(" love");
cout << "str3 = " << str3 << endl;
// 5
str3.append(" game aaaaaaa", 5);
cout << "str3 = " << str3 << endl;
// 6
str3.append(str2);
cout << "str3 = " << str3 << endl;
// 7
string str4 = "I love game:";
str4.append(str2, 0, 4);
cout << "str4 = " << str4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.5 string 查找和替换
查找
find 和 rfind 区别:
find 是从左往右找,rfind 是从右往左找
# include<iostream>
using namespace std;
// 字符串查找和替换
// 1、查找
void test01()
{
// find
string str1 = "abcdefghijklmnopqrstuvwxyzde";
int pos1 = str1.find("ed", 0);
cout << "如果字符串中没有所要查找的相应字符,那么返回的 pos 值为:" << pos1 << endl;
int pos = str1.find("de", 0);
if (pos == -1)
{
cout << "未找到该字符串" << endl;
}
else
{
cout << "字符串 'de' 所在位置的下标 pos 为:" << pos << endl;
}
// rfind
int pos2 = str1.rfind("de");
cout << "pos2 = " << pos2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
替换
# include<iostream>
# include<string>
using namespace std;
// 字符串查找和替换
// 替换
void test02()
{
string str4 = "abcdefgh";
str4.replace(1, 3, "1111"); // 从1号位置起3个字符替换为"1111"
cout << "str4 = " << str4 << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
3.1.6 string 字符串比较
# include<iostream>
using namespace std;
void test01()
{
string str1 = "hello";
string str2 = "xello";
if(str1.compare(str2) == 0)
{
cout << "str1 等于 str2" << endl;
}
else if (str1.compare(str2) == 1)
{
cout << "str1 大于 str2" << endl;
}
else
{
cout << "str1 小于 str2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不大
3.1.7 string 字符存取
string 中单个字符存取方式有两种
char& operator[](int n);
// 通过[]方式取字符char& at(int n);
//通过 at 方式获取字符
# include<iostream>
# include<string>
using namespace std;
void test01()
{
string str1 = "hello";
cout << "str1 = " << str1 << endl;
// 1、通过[]访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] << " ";
}
cout << endl;
//2、通过 at 方式访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " ";
}
cout << endl;
// 不仅可以读,还可以写入单个字符
str1[0] = 'x';
cout << "str1 = " << str1 << endl;
str1.at(1) = 'x';
cout << "str1 = " << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.1.8 string 字符插入和删除
# include<iostream>
# include<string>
using namespace std;
void test01()
{
string str1 = "hello";
// 插入
str1.insert(1, "2222222");
cout << "str1 = " << str1 << endl;
// 删除
str1.erase(1, 7);
cout << "str1 = " << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:插入和删除的起始下标都是从零开始
3.1.9 string 子串
功能描述
- 从字符串中获取想要的子串
函数原型
string substr(int pos = 0, int n = npos) const;
//返回由 pos 开始的 n 个字符组成的字符串
# include<iostream>
# include<string>
using namespace std;
void test01()
{
string str = "abcdefgh";
cout << "str = " << str << endl;
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl;
}
// 实用操作
void test02()
{
string email = "zhangsan@sina.com";
int pos = email.find('@');
string username = email.substr(0, pos);
cout << "username = " << username << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}