string容器
string是C++中用于管理字符的一个类
本质上字符在string类中是char *类型的变量,只不过被封装成了一个类,这个类中重载了很多运算符,使其像个数组一样。下面总结了一些string类的函数和重载的运算符
string的构造函数
string()
默认构造
string(const char* s)
字符串构造
string(const string& s)
拷贝构造
string(int num, char c)
数值*字符构造
#include <iostream>
using namespace std;
void test01()
{
const char* str = "Hello World";
string s1;//默认构造
string s2(str);//使用字符常量构造
string s3("hello World");//同上
string s4(s2);//拷贝构造
string s5(10,'a');//数量*字符
cout<<"s1 = "<<s1<<endl;
cout<<"s2 = "<<s2<<endl;
cout<<"s3 = "<<s3<<endl;
cout<<"s4 = "<<s4<<endl;
cout<<"s5 = "<<s5<<endl;
}
int main()
{
test01();
}
string的赋值操作
string& operator=(const char* s)
string& operator=(const string& s)
string& operator=(const char c)
string& assign(const char* s)
把字符串赋值给string对象
string& assign(const char* s, int n)
把字符串前n个字符赋值给string对象
string& assign(string& s)
另一个string给这个string
string& assign(int n,char c)
n个字符
string字符串拼接
string& operator+=(const char* s)
string& operator+=(const string& s)
string& operator+=(const char c)
string& append(const char* s)
string& append(const string& s)
string& append(const char c)
string& append(const string& s, int pos, int n)
string查找和替换
函数原型
在类里面的函数后面加const使函数变为调用时不可修改类内部数据的函数
-
int find(const string& str, int pos=0) const
//查找str第一次出现的位置,从pos开始查找,找不到返回-1 -
int find(const char* str,int pos=0) const
//同上 -
int find(const char* str, int pos=0, int n) const
//从pos位置查找str的前n个元素的位置 -
int find(const char c, int pos=0) const
//从pos位置查找字符c -
int rfind(const string& str, int pos=npos) const
//查找str的最后出现的位置,从pos开始查找 -
int rfind(const char* str,int pos=npos) const
//同上 -
int rfind(const char* str, int pos, int n) const
//从pos位置查找str的前n个元素的位置 -
int rfind(const char c, int pos=0) const
//从pos位置查找字符c -
string& replace(int pos, int n, const string& str)
从pos开始n个字符为字符串str -
string& replace(int pos, int n, const char* s)
示例:
void test01()
{
string str = "abcdefef";
int pos = str.find("ef");
cout<<pos<<endl;//4
int pos2 = str.rfind("ef");
cout<<pos2<<endl;//6
str.replace(1,3,"1111");
cout<<str<<endl;//a1111efef,从位置1开始后面的三个字符变为1111
}
总结:
find从左往右,rfind从右往左
find返回查找的第一个字符,找不到返回-1
replace在替换时将从哪个位置起,多少个字符,替换为 什么
string的字符串比较
按ASCII码进行比较
=返回0
>返回1
<返回-1
test01()
{
string str1 = "hello";
string str2 = "world";
if(str1.compare(str2)==0)
{
cout<<"str1=str2"<<endl;
}
}
string字符存取
char& operator[](int n)
[ ]方式char& at(int)
//at方式
test01()
{
string str = "abcdefg";
//第一种方式
for(int i=0;i<str.size();i++)
{
cout<<str[i]<<endl;
}
//第二种方式
for(int i=0;i<str.size();i++)
{
cout<<str.at(i)<<endl;
}
}
string插入和删除
函数原型
string& insert(int pos, const char* s);
//插入字符串string& insert(int pos, const string& str);
插入字符串string& insert(int pos, int n, char c);
在指定位置插入n个字符string& erase(int pos, int = npos);
删除从pos开始的n个字符
void test01()
{
string str1 = "hello";
string str2 = "world";
str1.insert(5," ");
cout<<str1<<endl;//hello空格
str1.insert(6, str2);
cout<<str1<<endl;//hello world
}
string子串
函数原型:
string substr(int pos=0, int n=npos) const;
//返回由pos开始的n个字符串组成的字符串
示例:
void test01()
{
string str1 = "hello";
string str2 = str1.substr(0,2);
cout<<str2<<endl;
}