String容器
前言
string是C++风格的字符串,而string本质是一个类。char* 是一个指针,string是一个类,类内部封装了char* ,管理这个字符串,是一个char*型的容器。
一、构造函数
#include<iostream>
#include<string>
using namespace std;
void test01()
{
//1.构造方法
string();
string s2 = "hello";//最常用
string s3(s2);//拷贝构造函数 s3=hello
string s4 (10, 'a'); //s4=aaaaaaaaaa
}
int main()
{
test01();
system("pause");
return 0;
}
二、String类的赋值操作(assign)
1.函数原型
2.代码说明
string str = "hello";//创建直接赋值 重载赋值运算符 operator=
string str1;
str1.assign("hello");//str1=hello
str1.assign("aaa", 2);//str1=aa 把字符串的前几个字符赋值给str1
str1.assign(3,'a');//str1=aaa 把3个字符'a'赋值给str1
二、String类的拼接操作(append)
1.函数原型
2.代码说明
string str = "hello";
str += " world";
cout << str << endl; //hello world
string str = "hello";
str.append(" world");
cout << str << endl; //hello world
str.append("aaa",2);
cout << str << endl; //hello worldaa 把字符串aaa的前两个字符追加给str
str.append(2, 'b');
cout << str << endl;//hello worldaabb 追加字符'b'两个
三、String类的查找和替换操作(find、replace)
1.函数原型
2.代码说明
int main()
{
string str = "hello";
int set = str.find('c');//find返回值为int set变量来接收
cout << set << endl; //打印结果为:-1 未找到返回-1
set = str.find("el");
cout << set << endl;//打印结果为:1 从0开始找(从左向右找)
set = str.rfind("el");
cout << set << endl;//打印结果为:1 从末尾开始找(从右向左找)
system("pause");
return 0;
}
2.1替换
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "hello";
int first = 1;
int end = 3;
str.replace(first,end,"aaa");
cout << str.replace(first, end, "aaaa") << endl; //haaaao
//replace函数返回自身字符串
//把first=1,end=3 之间位置的字符串替换为"aaaa"
system("pause");
return 0;
}
四、字符串的比较操作(compare)
1.函数原型
2.代码说明
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "hello";
string str2 = "hello";
int set = str1.compare(str2);
cout << "set= " << set << endl; //两个字符串相等返回0
string str11 = "xello";
string str22 = "hello";
int set1 = str11.compare(str22);
cout << "set1= " << set1 << endl; //str11>str22 返回1
string str111 = "xello";
string str222 = "zello";
int set11 = str111.compare(str222);
cout << "set11= " << set11 << endl; //str11>str22 返回-1
system("pause");
return 0;
}
五、字符串的存取操作(at)
1.函数原型
2.代码说明
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "hello";
for (int i = 0; i < str.size(); i++)//size为字符串的长度
{
cout << str[i];//通过重载的[]去访问字符串
//输出为 hello
}
str[1] = 'x';//修改下标为1的字符
cout << str << endl;//输出结果为:hxll
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "hello";
for (int i = 0; i < str.size(); i++)//size为字符串的长度
{
cout << str.at(i);//通过at()函数
//输出为 hello
}
cout << endl;
str.at(1) = 'x';//修改下标为1的字符
cout << str << endl;//输出结果为:hxll
system("pause");
return 0;
}
六、字符串的插入和删除(insert、erase)
1.函数原型
2.代码说明
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "aaaaa";
str.insert(1,"bbbb");//在第一个位置后插入字符串"bbbb"
cout << str << endl; //abbbbaaaa
str.erase(1,4); //删除字符串str 1-4位置的字符串
cout << str << endl;//aaaaa
system("pause");
return 0;
}
七、字符串的获取(substr)
1.函数原型
2.代码说明
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "abcdefghijkl";
string substr = str.substr(1,3);
cout << substr << endl; //输出结果为:bcd
system("pause");
return 0;
}
八、总结
本次对C++/string类部分函数进行演示,希望可以帮助到大家。函数原型图片参考(黑马课程)。