C++基础:string使用详解

string使用详解
0.目录
1.准备
2.初始化
3.赋值
4.访问
5.连接
6.比较
7.子串
8.交换
9.查找
10.修改
11.插入
12.删除
13.特性描述
14.字符串流处理
1.准备
首先,需要做这样的事情
#include <iostream>
#include <string>
using namespace std;
//之后就可以体验string内置的各种强大功能了—
string相关迭代器
//返回string的起始位置
const_iterator begin()const; iterator begin(); 
//返回string的最后一个字符后面的位置
const_iterator end()const; iterator end(); 
//返回string的最后一个字符的位置
const_iterator rbegin()const; iterator rbegin(); 
//返回string第一个字符位置的前面
const_iterator rend()const; iterator rend(); 
2.初始化
初始化肯定是必须的,以下为几种初始化方法:

什么都不做

代码示例
string str;
cout << "---" << str << "---" << endl;
程序输出
------
使用=运算符

函数声明
//这是通过`字符数组`来赋值
string& operator=(const char* s); 
//这是通过其他的`string`来赋值
string& operator=(const string &s);
代码示例
//字符串
string a = "djdljwcj";
string b = a;
cout << a << endl << b << endl;
//字符数组
char c[7] = "wzdkjl";
string d = c;
cout << c << endl << d << endl;
程序输出
djdljwcj
djdljwcj
wzdkjl
wzdkjl
使用string和字符数组初始化

函数声明
//字符数组
string(const char* s); 
//string字符串
string(const string& str); 
代码示例
string a = "whdltql";
string b(a);
cout << a << endl << b << endl;

char c[10] = "limtql";
string d(c);
cout << c << endl << d << endl;
//直接""也是可以的
string e = "gydbtql";
cout << e << endl;
程序输出
whdltql
whdltql
limtql
limtql
gydbtql
使用n个字符c初始化

函数声明
string(int n, char c);
代码示例
string str(10, 'h');
cout << str << endl;
程序输出
hhhhhhhhhh
3.赋值
1.重载=运算符

函数声明
//这是通过`字符数组`来赋值
string& operator=(const char* s); 
//这是通过其他的`string`来赋值
string& operator=(const string &s);
//这是使用`单个字符`来赋值
string& operator=(char c);
代码示例
string a, b, c;
char str1[10] = "yznb";
string str2 = "bnzy";
char str3 = 'a';
a = str1;
b = str2;
c = str3;
cout << a << endl << b << endl << c << endl;
程序输出
yznb
bnzy
a
assign方式赋值

函数声明
//使用字符数组赋值
string &assign(const char *s);
//使用字符数组的前n个字符赋值
string &assign(const char *s, int n);
//使用string赋值
string &assign(const string &s);
//使用string中从start开始的n个字符赋值
string &assign(const string &s, int start, int n);
//使用两个迭代器赋值
string &assign(const_iterator first, const_itertor last);
//使用n个字符c赋值
string &assign(int n, char c);
代码示例
string a, b, c, d, e, f, g;
char str1[10] = "gydbtql";
string str2 = "cfzbllnz";

//使用字符数组赋值
a.assign(str1);
cout << a << endl;

//str1的前4个字符,即gydb
b.assign(str1, 4);
cout << b << endl;

//使用string赋值
c.assign(str2);
cout << c << endl;

//从下标2开始的4个字符,即zbll
//注意如果最后一个参数不填的话就直接默认为把`从2开始的后面全部字符`赋值
d.assign(str2, 2, 4);
cout << d << endl;

//迭代器赋值
e.assign(str2.begin(), str2.end());
cout << e << endl;

//10个字符h赋值
g.assign(10, 'h');
cout << g << endl;
程序输出
gydbtql
gydb
cfzbllnz
zbll
cfzbllnz
hhhhhhhhhh
4.访问
重载[]和at函数

代码示例
string a = "0123456789";
cout << a[0] << endl;
cout << a.at(1) << endl;
程序输出
0
1
5.连接
重载+=运算符

函数声明
//把字符串s连接到当前字符串的结尾
string &operator+=(const string &s);
//把字符c连接到当前字符串的结尾
string &operator+=(char c);
代码示例
string a;
string str1 = "0123456789";
char temp = 'x';

//结尾加上str
a += str1;
cout << a << endl;
//结尾加上temp
a += temp;
cout << a << endl;
//直接使用""
a += "abcdefg";
cout << a << endl;
//直接使用''
a += '$';
cout << a << endl;
程序输出
0123456789
0123456789x
0123456789xabcdefg
0123456789xabcdefg$
append方式

函数声明
//把字符数组连接到当前字符串结尾
string &append(const char *s); 
//把字符数组的前n个字符连接到当前字符串结尾
string &append(const char *s, int n);
//同operator+=()
string &append(const string &s);
//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(const string &s, int pos, int n);
//把迭代器first和last之间的部分连接到当前字符串的结尾
string &append(const_iterator first, const_iterator last);
//在当前字符串结尾添加n个字符c
string &append(int n, char c); 
程序示例
string a, b, c, d, e, f, g;
char str1[] = "0123456789";
string str2 = "abcdefghij";

a.append(str1);
cout << a << endl;
//前5个字符
a.append(str1, 5);
cout << a << endl;

b.append(str2);
cout << b << endl;
//从下标3开始五个字符
//这里注意如果最后一个参数不填的话就直接默认为连接从3开始的后面全部字符
b.append(str2, 3, 5);
cout << b << endl;

c.append(str2.begin(), str2.end());
cout << c << endl;

c.append(10, 'h');
cout << c << endl;
程序输出
0123456789
012345678901234
abcdefghij
abcdefghijdefgh
abcdefghij
abcdefghijhhhhhhhhhh
6.比较
比较运算符重载

函数声明
//比较两个字符串是否相等
//运算符">","<",">=","<=","!="均被重载用于字符串的比较;
bool operator++(const string &s1,const string &s2)const;
代码示例
string a = "123333334447", b = "456333337790";
if (a ++ b) cout << "a ++ b" << endl;
if (a != b) cout << "a != b" << endl;
if (a < b) cout << "a < b" << endl;
if (b > a) cout << "b > a" << endl;

string c = "aaaaaaa", d = "aaaaaaa";
if (c ++ d) cout << "c ++ d" << endl;
if (c >= d) cout << "c >= d" << endl;
if (c <= d) cout << "c <= d" << endl;

string e = "9", f = "123";
//这里会输出 e > f 是因为string是从高位到低位逐位比较
//'9' > '1' 所以 e > f 
if (e > f) cout << "e > f" << endl;
if (e < f) cout << "e < f" << endl;
程序输出
a != b
a < b
b > a
c ++ d
c >= d
c <= d
e > f
compare方式

函数声明
//compare返回两个字符串中第一个不同的字符之差

//比较当前字符串和s的大小
int compare(const string &s) const;
//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n, const string &s)const;
//比较当前字符串从pos开始的n个字符组成的字符串
//与s中pos2开始的n2个字符组成的字符串的大小
int compare(int pos, int n,
            const string &s, int pos2, int n2)const;
//比较当前字符串和字符数组的大小
int compare(const char *s) const;
//比较当前字符串从pos开始的n个字符组成的字符串与字符数组的大小
int compare(int pos, int n, const char *s) const;
代码示例
string a = "abc", b = "123456";
string c = "$$abc456$$", d = "$$abc456$$";

//'a' - '1' = 48
cout << a.compare(b) << endl;
cout << b.compare(a) << endl;
cout << c.compare(d) << endl;
//从下标2开始的三个字符即"abc"与a字符串相等
cout << c.compare(2, 3, a) << endl;
//从下标5开始的三个字符即"456"与
//从下标3开始的三个字符即"456"比较
cout << c.compare(5, 3, b, 3, 3) << endl;

char e[] = "abc456";
//'$' - 'a' = -61
cout << c.compare(e) << endl;
cout << c.compare(2, 6, e) << endl;
程序输出
48
-48
0
0
0
-61
0
7.子串
函数声明
//返回pos开始的n个字符组成的字符串
string substr(int pos = 0,int n = npos) const;
代码示例
string a = "0123456789";
cout << substr(3, 4) << endl;
//如果只写一个参数的话就默认从参数开始到结尾的子串
cout << substr(2) << endl;
程序输出
3456
23456789
8.交换
函数声明
void swap(string &s2); //交换当前字符串与s2的值
代码示例
string a = "123456", b = "654321";
cout << a << endl << b << endl;
a.swap(b);
cout << a << endl << b << endl;
程序输出
123456
654321
654321
123456
9.查找
find

函数声明
//查找成功时返回所在位置,失败返回string::npos的值

//从pos开始查找字符c在当前字符串的位置
int find(char c, int pos = 0) const;
//从pos开始查找字符串s在当前串中的位置
int find(const char *s,int pos = 0) const;
//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const char *s, int pos, int n) const;
//从pos开始查找字符串s在当前串中的位置 
int find(const string &s,int pos = 0) const;
代码示例
string a = "0123456789", b = "456";
char c = '7';
char d[] = "234";
//这里如果不填pos这个参数的话就默认为从0开始
cout << a.find(c) << endl;
//从8开始找不到
cout << a.find(c, 8) << endl;

cout << a.find(d) << endl;
//从5开始找不到
cout << a.find(d, 5) << endl;

//a中从2开始查找d的前1个字符
cout << a.find(d, 2, 1) << endl;

cout << a.find(b) << endl;
//从下标7开始找,自然找不到
cout << a.find(b, 7) << endl;
程序输出
7
18446744073709551615
2
18446744073709551615
2
4
18446744073709551615
rfind

函数声明
//从pos开始从后向前查找字符c在当前串中的位置
int rfind(char c, int pos = npos) const;
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置
int rfind(const string &s,int pos = npos) const; 
代码示例
string a = "0123456789", b = "456";
char c = '7';
char d[] = "234";
//这里如果不填pos这个参数的话就默认为从npos开始
cout << a.rfind(c) << endl;
//从6开始从后往前找不到
cout << a.rfind(c, 6) << endl;

cout << a.rfind(d) << endl;
//从1开始从后往前找不到
cout << a.rfind(d, 1) << endl;

//a中从3开始从后往前查找d的前1个字符
cout << a.rfind(d, 3, 1) << endl;

cout << a.rfind(b) << endl;
//从下标7从后往前开始找找不到
cout << a.rfind(b, 3) << endl;
程序输出
7
18446744073709551615
2
18446744073709551615
2
4
18446744073709551615
find_first_of(返回起始索引位置)(从前往后)

函数声明
//查找失败返回string::npos

//从pos开始查找字符c第一次出现的位置
int find_first_of(char c, int pos = 0) const;
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置
int find_first_of(const string &s,int pos = 0) const; 
代码示例
//和find一模一样,这里就不复制了
程序输出
//理由同上
find_first_not_of(返回一个下标,从这个下标开始往后就找不到了)

函数声明
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const; 
代码示例
string a = "0123456789", b = "456";
char c = '7';
char d[] = "234";
//从0开始往后找不到了
cout << a.find_first_not_of(c) << endl;
//从8开始往后找不到了
cout << a.find_first_not_of(c, 8) << endl;

//从0开始往后找不到了(因为d带着\0)
cout << a.find_first_not_of(d) << endl;
//从5开始往后找不到了
cout << a.find_first_not_of(d, 5) << endl;
//从3开始往后找不到了
cout << a.find_first_not_of(d, 2, 1) << endl;

//从0开始往后找不到了
cout << a.find_first_not_of(b) << endl;
//从7开始往后找不到了
cout << a.find_first_not_of(b, 7) << endl;

//如果两串相等返回npos
cout << a.find_first_not_of("0123456789") << endl;
程序输出
0
8
0
5
3
0
7
18446744073709551615
find_last_of(返回结束索引位置)(从后往前)

函数声明
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos,int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
代码示例
string a = "0123456789", b = "456";
char c = '7';
char d[] = "234";
//返回最后一个下标7
cout << a.find_last_of(c) << endl;
//从6开始从后往前找不到
cout << a.find_last_of(c, 6) << endl;

//从后往前返回最后一个下标4
cout << a.find_last_of(d) << endl;
//从1开始从后往前找不到
cout << a.find_last_of(d, 1) << endl;

//a中从3开始从后往前查找d的前1个字符.返回最后一个下标2
cout << a.find_last_of(d, 3, 1) << endl;

//从后往前返回最后一个下标6
cout << a.find_last_of(b) << endl;
//从下标7从后往前开始找找不到
cout << a.find_last_of(b, 3) << endl;
程序输出
7
18446744073709551615
4
18446744073709551615
2
6
18446744073709551615
find_last_not_of(返回一个下标,从这个下标开始往前就找不到了)

函数声明
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
代码示例
string a = "0123456789", b = "456";
char c = '7';
char d[] = "234";
//从最后一个也就是9开始往前就找不到了
cout << a.find_last_not_of(c) << endl;
//从6开始往前找不到
cout << a.find_last_not_of(c, 6) << endl;

//从9开始往前找不到(d带着\0)
cout << a.find_last_not_of(d) << endl;
//从1开始往前找不到
cout << a.find_last_not_of(d, 1) << endl;

//a中从4开始往前查找d的前3个字符,从1开始找不到
cout << a.find_last_not_of(d, 4, 3) << endl;

//从9开始往前找不到
cout << a.find_last_not_of(b) << endl;
//从下标3往前找不到
cout << a.find_last_not_of(b, 3) << endl;  
程序输出
9
6
9
1
1
9
3
10.替换
函数声明
//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s);
//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const char *s, int n);
//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s);
//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,const string &s, int pos, int n);
//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(int p0, int n0, int n, char c);
//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s);
//把[first0,last0)之间的部分替换为s的前n个字符。
string &replace(iterator first0, iterator last0,const char *s, int n);
//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,const string &s);
//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,int n, char c);
//把[first0,last0)之间的部分替换成[first,last)之间的字符串。
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);

代码示例
string a = "0123456789", b = "abcdefghij";
string temp = a;
char c[] = "ABCDEFGHIJ";
//a中从0开始的3个字符替换成c
a.replace(0, 3, c);
cout << a << endl;
a = temp;
//a中从0开始的3个字符替换成c的前3个字符
a.replace(0, 3, c, 3);
cout << a << endl;
a = temp;
//
a.replace(0, 3, b);
cout << a << endl;
a = temp;
//a中从0开始的3个字符替换成b中从3开始的全部字符
a.replace(0, 3, b, 3);
cout << a << endl;
a = temp;
//a中从0开始的3个字符替换成b中从3开始的3个字符
a.replace(0, 3, b, 3, 3);
cout << a << endl;
a = temp;

a.replace(0, 3, 10, 'h');
cout << a << endl;
a = temp;

a.replace(a.begin(), a.end(), c);
cout << a << endl;
a = temp;

a.replace(a.begin(), a.end(), c, 3);
cout << a << endl;
a = temp;

a.replace(a.begin(), a.end(), b);
cout << a << endl;
a = temp;

a.replace(a.begin(), a.end(), 10, 'h');
cout << a << endl;
a = temp;

a.replace(a.begin(), a.end(), b.begin(), b.end());
cout << a << endl;
a = temp;
程序输出
ABCDEFGHIJ3456789
ABC3456789
abcdefghij3456789
defghij3456789
def3456789
hhhhhhhhhh3456789
ABCDEFGHIJ
ABC
abcdefghij
hhhhhhhhhh
abcdefghij
11.插入
函数声明
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n); 
//此函数在p0处插入n个字符c
string &insert(int p0, int n, char c);
//在it处插入字符c,返回插入后迭代器的位置
iterator insert(iterator it, char c);
//在it处插入[first,last)之间的字符
void insert(iterator it, const_iterator first, const_iterator last);
//在it处插入n个字符c
void insert(iterator it, int n, char c);
代码示例
string a = "0123456", b = "abcdefg";
string temp = a;
char c[] = "ABCDEFG";

a.insert(0, c);
cout << a << endl;
a = temp;

a.insert(1, c, 3);
cout << a << endl;
a = temp;

a.insert(2, b);
cout << a << endl;
a = temp;
//在3这个位置插入b中从3开始的3个字符
a.insert(3, b, 3, 3);
cout << a << endl;
a = temp;

a.insert(4, 10, 'h');
cout << a << endl;
a = temp;

a.insert(a.begin(), 'h');
cout << a << endl;
a = temp;

a.insert(a.begin(), b.begin(), b.end());
cout << a << endl;
a = temp;

a.insert(a.end(), 10, 'h');
cout << a << endl;
a = temp;
程序输出
ABCDEFG0123456
0ABC123456
01abcdefg23456
012def3456
0123hhhhhhhhhh456
h0123456
abcdefg0123456
0123456hhhhhhhhhh
12.删除
函数声明
//删除[first,last)之间的所有字符,返回删除后迭代器的位置。
iterator erase(iterator first, iterator last);
//删除it指向的字符,返回删除后迭代器的位置。
iterator erase(iterator it);
//删除pos开始的n个字符,返回修改后的字符串。
string &erase(int pos = 0, int n = npos);
代码示例
string a = "0123456789", temp = a;

a.erase(a.begin(), a.end());
cout << a << endl;
a = temp;

a.erase(a.begin())
cout << a << endl;
a = temp;

a.erase(3, 3);
cout << a << endl;
a = temp;
程序输出
123456789
0126789
13.特性描述
函数声明
//返回当前容量(即string中不必增加内存即可存放的元素个数)
int capacity()const; 
//返回string对象中可存放的最大字符串的长度
int max_size()const; 
//返回当前字符串的大小
int size()const; 
//返回当前字符串的长度
int length()const; 
//当前字符串是否为空
bool empty()const; 
//把字符串当前大小置为len,并用字符c填充不足的部分
void resize(int len,char c);
代码示例
string a = "0123456789";
cout << a.capacity() << endl;
cout << a.max_size() << endl;

cout << a.size() << endl;
cout << a.length() << endl;

if (a.empty()) cout << "empty" << endl;
else cout << "not empty" << endl;

a.resize(20, 'h');
cout << a << endl;

a.resize(20);
cout << "------" << endl;
cout << a.capacity() << endl;
cout << a.size() << endl;
cout << a.length() << endl;
cout << a << endl;
程序输出
10
4611686018427387897
10
10
not empty
0123456789hhhhhhhhhh
14.字符串流处理
首先需要包含#include<sstream>头文件
针对一些题的奇怪读入和输出

代码示例

string input("hello,this is a test");
istringstream sin(input);

string a, b, c, d;
sin >> a >> b >> c >> d;

ostringstream sout;
sout << a << " " << b << " " << c << " " << d;
cout << sout.str() << endl;
程序输出
hello,this is a test

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值