文章目录
相关笔记链接:
[C++提高编程笔记] 一.模板
[C++提高编程笔记] 二.STL初识
[C++提高编程笔记] 三.(一).STL常用容器之string容器
[C++提高编程笔记] 三.(二).STL常用容器之vector容器
[C++提高编程笔记] 三.(三).STL常用容器之deque容器
[C++提高编程笔记] 三.(四).STL常用容器之案例-评委打分
[C++提高编程笔记] 三.(五).STL常用容器之stack容器
[C++提高编程笔记] 三.(六).STL常用容器之queue容器
[C++提高编程笔记] 三.(七).STL常用容器之list容器
[C++提高编程笔记] 三.(八).STL常用容器之set/multiset容器
[C++提高编程笔记] 三.(九).STL常用容器之map/multimap容器
[C++提高编程笔记] 三.(十).STL常用容器之案例-员工分组
[C++提高编程笔记] 四.STL函数对象
[C++提高编程笔记] 五.STL常用算法
导读:C++是一种计算机高级程序设计语言,由C语言扩展升级而产生,最早于1979年由本贾尼·斯特劳斯特卢普在AT&T贝尔工作室研发。C++既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行以继承和多态为特点的面向对象的程序设计。C++擅长面向对象程序设计的同时,还可以进行基于过程的程序设计。C++拥有计算机运行的实用性特征,同时还致力于提高大规模程序的编程质量与程序设计语言的问题描述能力。
1.string基本概念
本质: string是C++风格的字符串,而string本质上是一个类。
string和char *区别:
1)char *是一个指针
2)string 是一个类,类内部封装了char *,管理这个字符串,是一个char *类型的容器。
特点:
1)string类内部封装了很多成员方法,例如:查找find,拷贝copy,删除delete,替换replace,插入insert……
2)string管理char *所分配的内存,不用担心复制越界和取值越界等。
2.string构造函数
函数原型:
1)string();
创建一个空的字符串,例如string str;。
2)string(const char * s);
使用字符串s初始化。
3)string(const string& str);
拷贝构造,使用一个string对象初始化另一个string对象。
4)string(int n, char c);
使用n个字符c初始化。
示例代码:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
// 第一种:默认构造
string s1;
// 第二种:
const char * str = "hello world"; // c语言风格的字符串
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()
{
test01();
return 0;
}
3.string赋值操作
功能描述: 给string字符串进行赋值。
赋值的函数原型:
1)string& operator=(const char* s;)
C语言风格的char *类型字符串赋值给当前字符串(理解:字符串赋值给string变量)
2)string& operator=(const string &s);
把字符串s赋值给当前的字符串(理解:string变量赋值给string变量)
3)string& operator=(char c);
字符赋值给当前的字符串(理解:字符赋值给string变量)
4)string& assign(const char *s);
把字符串s赋给当前的字符串(理解:字符赋值给string变量)
5)string& assign(const char *s, it n);
用字符串s的前几个字符赋给当前的字符串
6)string& assign(const string &s);
用字符串s赋给当前字符串
7)string& assign(int n, char c);
用n个字符c赋给当前字符串
示例代码:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
// 第一种方式:
string str1;
str1 = "hello world";
cout << str1 << endl;
// 第二种方式:
string str2;
str2 = str1;
cout << str2 << endl;
// 第三种方式:
string str3;
str3 = 'a';
cout << str3 << endl;
// 第四种方式:
string str4;
str4.assign("hello C++");
cout << str4 << endl;
// 第五种方式:
string str5;
str5.assign("hello,C++", 5); //字符串的前5个字符赋值给str5
cout << str5 << endl;
// 第六种方式:
string str6;
str6.assign(str5);
cout << str6 << endl;
// 第七种方式:
string str7;
str7.assign(3, 'w');
cout << str7 << endl;
}
int main()
{
test01();
return 0;
}
总结: string赋值方式很多,一般用operator=(等号赋值)比较多。
4.string字符串拼接
功能描述: 实现在字符串末尾拼接字符串。
函数原型:
1)string& operator+=(const char* str);
使用+=拼接字符串
2)string& operator+=(const char c);
使用+=拼接单个字符
3)string& operator+=(const string& str);
使用+=拼接字符串变量
4)string& append(const char *s);
使用append将字符串s追加到原字符串上
5)string& append(const char *s, int n);
使用append将字符串s的前n个字符追加到原字符串上
6)string& append(const string &s);
使用append将字符串变量拼接到原字符串上
7)string& append(const char *s, int pos, int n);
使用append将字符s从pos开始的n个字符追加到原字符串
示例代码:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
// 第一种方式: 拼接字符串
string str1 = "我";
str1 += "爱玩游戏";
cout << str1 << endl;
// 第二种方式:拼接单个字符
str1 += ',';
cout << str1 << endl;
// 第三种方式: 拼接已知的字符串变量
string str2 = " example:FIFAonline和2Konline。";
str1 += str2;
cout << str1 << endl;
// 第四种方式:append 拼接字符串
string str3 = "I";
str3.append(" love game ");
cout << str3 << endl;
// 第五种方式:append 拼接字符串的前n个字符
str3.append("hahahaha",4);
cout << str3 << endl;
// 第六种方式:append 拼接字符串
str3.append(str2);
cout << str3 << endl;
// 第七种方式:append 拼接部分字符串
str3.append(str2,0,8);
cout << str3 << endl;
}
int main()
{
test01();
return 0;
}
5.string查找和替换
功能描述:
1)查找:查找指定字符串是否存在
2)替换:在指定的位置替换字符串
函数原型:
1)int find(const string& str, int pos = 0) const;
查找字符串str第一次出现的位置,从pos开始查找
2)int find(const char* s, int pos = 0) const;
查找字符s第一次出现的位置,从pos开始查找
3)int find(const char* s, int pos, int n) const;
从pos位置查找s的前n个字符第一次出现的位置
4)int find(const char c, int pos = 0) const;
查找字符c第一次出现的位置
5)int rfind(const string& str, int pos = npos) const;
查找str最后一次出现的位置,从pos开始查找
6)int rfind(const char* s, int pos = npos) const;
从pos查找s的前n个字符最后一次出现的位置
7)int rfind(const char* s, int pos, int n) const;
从pos查找s的前n个字符最后一次出现的位置
8)int rfind(const char c, int pos = 0) const;
查找字符c最后一次出现的位置
9)string& replace(int pos, int n, const string& str);
替换从pos开始n个字符为字符串str
10)string& replace(int pos, int n, const char* s);
替换从pos开始的n个字符为字符串s
示例代码:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
// 使用find查找字符串
string str1 = "abcdefgde";
int pos1 = str1.find("de");
if(pos1 == -1)
{
cout << "未找到字符串" << endl;
}else
{
cout << "pos1 = " << pos1 << endl;
}
// 使用rfind查找字符串
int pos2 = str1.rfind("de");
if(pos2 == -1)
{
cout << "未找到字符串" << endl;
}else
{
cout << "pos2 = " << pos2 << endl;
}
}
// 替换
void test02()
{
string str1 = "abcdefg";
str1.replace(1,3,"1111"); // 指定4个1,全部替换
cout << "str1 = " << str1 << endl;
}
int main()
{
// 字符串查找函数
test01();
// 字符串替换函数
test02();
return 0;
}
6.string字符串比较
功能描述: 字符串之间的比较
比较方式: 字符串比较是按字符的ASCII码进行对比
1)= 返回 0
2)> 返回 1
3)< 返回 -1
函数原型:
1)int compare(const string &s) const;
与字符串s比较
2)int compare(const char *s) const;
与字符串s比较
示例代码:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
string str1 = "hello";
string str2 = "hello";
if(str1.compare(str2) == 0)
{
cout << "str1 等于 str2" << endl;
}
string str3 = "xello";
if(str2.compare(str3) > 0)
{
cout << "str2 大于 str3" << endl;
}
else
{
cout << "str2 小于 str3" << endl;
}
}
int main()
{
test01();
return 0;
}
总结: 字符串比较主要是判断两个字符串相等还是不相等,比较大小的意义不大。
7.string字符存取
string中单个字符存取有两种方式:
1)char& operator[](int n);
通过[]方式取字符串
2)char& at(int n);
通过at方法获取字符
示例代码:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
string str = "hello";
cout << "str = " << str << endl;
// 方式1:通过[]访问单个字符
for(int i = 0; i < str.size(); i++)
{
cout << str[i] << endl;
}
// 方式2:通过at访问单个字符
for(int i = 0; i < str.size(); i++)
{
cout << str.at(i) << endl;
}
// 修改方式1:使用[]
str[0] = 'x';
cout << "str = " << str << endl;
// 修改方式2:使用at
str.at(1) = 'y';
cout << "str = " << str << endl;
}
int main()
{
test01();
return 0;
}
8.string插入和删除
功能描述: 对string字符串进行插入和删除字符操作
函数原型:
1)string& insert(int pos, const char* s);
插入字符串
2)string& insert(int pos, const string& str);
插入字符串
3)string& insert(int pos, int n, char c);
在指定位置插入n个字符c
4)string& erase(int pos, int n = npos);
从pos开始删除n个字符
示例代码:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
string str = "hello";
// 插入
str.insert(1, "111");
cout << "str = " << str << endl;
// 删除
str.erase(1,3);
cout << "str = " << str << endl;
}
int main()
{
test01();
return 0;
}
9.string子串获取
功能描述: 从字符串中获取想要的子串
函数原型:
string substr(int pos = 0, int n = npos) const;
返回从pos开始的n个字符组成的字符串
示例代码:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
string str = "abcdefgh";
// 获取子串
string strSub = str.substr(1,3);
cout << "strSub = " << strSub << endl;
}
// 实际操作
void test02()
{
string email = "zhangsan@sina.com";
int pos = email.find("@");
// 截取用户名
string userName = email.substr(0,pos);
cout << userName << endl;
// 截取域名
string domain = email.substr(pos+1);
cout << domain << endl;
}
int main()
{
// test01();
test02();
return 0;
}