【C++】string常用接口

一、STL

STL(standard template libaray-标准模板库): 是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。

image-20220303213315748


二、string

1. 定义

string是一个字符串的模板类

int main()
{
    string s1("hello");//构造对象
    return 0;
}

image-20220303221129489

可以看出string是std标准库里的,且是typedef出来的

库里定义:

template<class T>
class basic_string
{
    pr
}

可以看出string原来是一个类模板,但它的模板类型是**char**

但这里不能直接就是以char为类型写string,要使得模板更加能利用,因为存在其他字符串类型

编码 由值+符号建议映射关系——形成编码表
ascii编码表——表示英文编码表
unicode——表示全世界文字编码表 utf-8(Linux下) utf-16 utf-32
gbk——中文自己量身定做都编码表,中国汉字用两个字节来表示


2. 常用接口

构造函数:

红色是常用的

image-20220304152427577

这里第三个可以控制自己想要的拷贝构造的长度,

npos指的是string里面的一个静态变量,实际是-1,给size_t,无符号整型,-1是最大的。实际上就是拷贝构造到最末尾

第五个是字符串的前几个字符去拷贝构造,第六个是以n个一个字符去构造

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string s1;//无参构造
  string s2("abc");//传参构造
  string s3(s2);//拷贝构造

  cin >> s1;

  cout << s2 << endl;
  cout << s3 << endl;
  cout << s1 << endl;

  string s4(s1);

  cout << s4 << endl;

  return 0;
}

image-20220304153129140


字符串长度:

不包含‘\0’

最好用size

image-20220304160852104

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1;
	string s2("abc");
	string s3(s2);

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


	return 0;
}

image-20220304161057445


申请空间大小:

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1;

	cout << s1.capacity() << endl;

	return 0;
}

image-20220304162959327


清除数据:

数据虽然没了,但开辟的空间还在

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("abc");
	cout << s1 << endl;
	
	s1.clear();
	cout << s1 << endl;
	cout << s1.capacity() << endl;

	return 0;
}

image-20220304163634111


删除数据

image-20220306120116157

本质是顺序表,尽量减少头部删除,因为需要挪动数据


字符串数组:

二者相似,但检查越界的方式不一样,operator断言,at抛异常

image-20220304165608973

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("abc");

	cout << s1 << endl;
	
	s1.clear();
	cout << s1 << endl;

	cout << s1.capacity() << endl;

	return 0;
}

image-20220304165333702


添加字符:

image-20220306115701212

image-20220306115730144

int main()
{
	string s1;
	s1.push_back('a');
	s1.append("bcde");
	cout << s1 << endl;

	s1 += ':';
	s1 += "hello world";
	cout << s1 << endl;


	return 0;
}

image-20220304171938510


image-20220306115808638

支持头插尾插,但因为是顺序表所以效率很低


遍历字符串:

  • 普通size遍历,注意返回值是size_t

image-20220306090241982

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("abc");

	for (size_t i = 0; i < s1.size(); ++i)
	{
		cout << s1[i];
	}
	
	return 0;
}

  • 迭代器

访问和修改容器的

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("abc");

    //iterator在string的类域里面
    /
	string::iterator it = s1.begin();
    //返回第一个数据的地址
	while (it != s1.end())//返回最后一个数据地址'\0'
	{
		cout << *it;
		++it;
	}
    //倒着输出
    string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *it;
		++it;
	}

	return 0;
}

迭代器遍历的意义是什么呢?
所有的容器都可以使用迭代器这种方式去访问修改
对于string,无论是正着遍历,倒着遍历,下标+ []都足够好用,为什么还要迭代器呢?
对于string,下标和[]就足够好用,确实可以不用迭代器。
但是如果是其他容器(数据结构)呢?


  • 范围for,语法糖
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("abc");

	for (auto input : s1)
	{
		cout << input;
	}

	return 0;
}

开空间:

这里开空间不一定是正好,一般是1.5-2倍逐渐开空间

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("abc");
	s1.reserve(100);

	cout << s1.capacity() << endl;
	return 0;
}

image-20220306101858517


resize和reserve区别

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("abc");
	s1.reserve(100);

	string s2;
	s2.resize(100);

	return 0;
}

image-20220306102941802

resize会初始化

image-20220306103020633

通过对比size就可以发现


打印字符串:

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("abc");
	cout << s1 << endl;
	
	cout << s1.c_str() << endl;

	return 0;
}

image-20220306103514735


二者的返回值不同,

image-20220306103648910

const char* //可以通过c_str()获取字符串的地址
//例子
string s1;
st1.c_str();

查找字符串:

find返回的是找到该字符串的第一个字符的位置

查找顺序是从左往右寻找

这里有rfind,是从右往左找

image-20220306105317014

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("test.txt");
	cout << s1.find(".txt");

	return 0;
}

image-20220306105410199


和substr组合使用

image-20220306105258220

string substr (size_t pos = 0, size_t len = npos) const;//起始位置,长度
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("test.txt");
	size_t pos = s1.find('.');
	cout << s1.substr(pos, s1.size() - pos);
	
	return 0;
}

image-20220306110256413


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凛音Rinne

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值