当前位置: 首页 > article >正文

stl之string的详解

一,string定义的方式

,string定义了多种函数重载的方式,常用的构造函数如下:

string();
string(const string& str);
string(const string& str, size_t pos, size_t len = npos);
string(const char* s);
string(const char* s, size_t n);
string(size_t n, char c);

使用示例

int main()
{
	string s1;
	string s2("hello wcd");//字符串
	string s3(s2);//用s2来初始化s3;
	string s4(s2, 6);//从s2的位置开始,不写长度默认就是npos;
	string s5("hello world", 5);//拷贝5个字符长度
	cout << s5 << endl;
	string s6(10, 'x');//拷贝10个字符
	return 0;
}

二 string中插入

常见的插入方式有几种:

void push_back (char c)

int main()
{
	string st("hello linux");
	st.push_back('w');
	st.push_back('c');
	st.push_back('d');
	return 0;
}

append的插入

string& append (const string& str);
string& append (const char* s);
string& append (size_t n, char c);

void test1()
{
	string s1("hello string");
	string s2("wcd");
	s1.append(s2);//在s1后面追加s2;
	s1.append("xxxx");//在s1后面插入字符串
	s1.append(10,'y');//在s1后面插入10个‘y'的字符
}

insert的插入

string& insert (size_t pos, const char* s);
string& insert (size_t pos, const string& str);
iterator insert (iterator p, char c);

void test2()
{
//	string& insert(size_t pos, const char* s);
//> string& insert(size_t pos, const string & str);
//> iterator insert (iterator p, char c);
	string s1("hello string");
	string s2("wcd");
	s1.insert(0, "string");//在pos位置为0的地方插入string相当于头插
	s1.insert(6, s2);//在下标为6的位置上插入s2对象的值;
	s1.insert(s1.end(), 'a');//在末尾插入字符'c';
}

+=插入

string (1)
string& operator+= (const string& str);
c-string (2)
string& operator+= (const char* s);
character (3)
string& operator+= (char c);

最好用的最方便的就是+=;运用了运算符重载;

void test11()
{
	string st1("good night");
	string st2("good morning");
	st1 += 'c';
	st1 += st2;
	st1 += "xxxxxx";
}

string的迭代器*

正向迭代器与反向迭代器

iterator begin();
const_iterator begin() const;
iterator begin();
const_iterator begin() const;//正向迭代器
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;//反向迭代器
正向迭代器

//const 版本的迭代器
void test4(const string& st1)
{
	string st1("hello world");
	string::const_iterator it = st1.begin();
	while (it != st1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}
//普通版本的迭代器
void test3()
{
	string st1("hello world");
	string::iterator it = st1.begin();
	while (it != st1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

反向迭代器
反向打印数据,从后往前打印

//const 版本的迭代器
void test5(const string& st1)
{
	string st1("hello world");
	string::const_reverse_iterator rit = st1.rbegin();
	while (rit != st1.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
}
//普通版本的迭代器
void test6()
{
	string st1("hello world");
	string::reverse_iterator rit = st1.rbegin();
	while (rit != st1.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
}

string的capacity

size_t size() const

void test7()
{
	string st1("hello world");
	cout << st1.size() << endl;//计算当前的string对象里面的字符串的大小
}

size_t length() const;

这个跟size()的用法是一样的,我们平常都使用size().

size_t max_size() const;

计算string最大可以返回的字符串的长度,取决于编译器,不同的编译器,大小都不一样,所以不准。
在这里插入图片描述

上面这个是vs的版本,下面我们来看看g++编译器。
在这里插入图片描述

俩个相差太多了,所以不准。

size_t capacity() const;

void test8()
{
	string st1("hello world");
	cout <<"初始的容量" << st1.capacity() << endl;
	size_t old = st1.capacity();
	for (size_t i = 0; i < 100; i++)
	{
		st1 += ' ';
		if (old != st1.capacity())
		{
			cout << "扩容后的:" << st1.capacity() << endl;
			old = st1.capacity();
		}
	}
	
}

在这里插入图片描述

void clear()

它的作用是清空string里面的数据,我们也可以观察一下它的容量是不会变化的,而他的size()是变化的。
在这里插入图片描述

void reserve (size_t n = 0);
这个是改变它的容量的

我们可以在开始的时候直接扩容至我们想要的大小。我们可以发现他不是刚刚好扩容至150,而是比150大一点。

在这里插入图片描述

假如我们要缩容呢?我们先clear完之后在缩容就行了。
在这里插入图片描述

void resize (size_t n);
void resize (size_t n, char c);
具有扩容还具初始化的功能

void test9()
{
	string st1;
	st1.resize(100);
	cout << "size:" << st1.size() << " " << "capacity:" << st1.capacity() << endl;
}

在这里插入图片描述
默认就是初始化成\0;
我们也可以指定初始化成字符。
在这里插入图片描述

string 成员的获取

Element access:比较好用的就一种,还有一种迭代器在string中不经常用。

char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
const对象调用const,普通对象调用普通的,普通的可读可写。

void test10()
{
	string st1("hello linux");
	//写的功能
	for (size_t i = 0; i < st1.size(); i++)
	{
		st1[i]++;
	}
	cout << endl;
	//读的功能
	for (size_t i = 0; i < st1.size(); i++)
	{
		cout << st1[i] << " ";
	}
	cout << endl;
	//注意一点的就是
char st2[3] = { 0 };//本质是*(st2+3)
st1[3];//本质是st1.operator(3);
}

在这里插入图片描述

string类的删除

sequence (1)
string& erase (size_t pos = 0, size_t len = npos);
character (2)
iterator erase (iterator p);
range (3)
iterator erase (iterator first, iterator last);

void test12()
{
	string s2("hello world");
	s2.erase(6, 1);//在pos为6的地方删除1个字节的大小;
	cout << s2 << endl;
	s2.erase(s2.begin() + 1);;//在首元素+1的位置的元素删掉;
	cout << s2 << endl;
	s2.erase(s2.begin()+1, s2.end());//在首元素+1的位置全部删掉
	cout << s2 << endl;
}

String operations:

const char* c_str() const;为了跟一些C语言的一些库配合可以混着一起使用;

void test13()
{
	string s = "filename.c";
	FILE* fout = fopen(s.c_str(), "r");
}

查找相关的

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;
size_t find (char c, size_t pos = 0) const;

用于初始化的另一个对象的

tring substr (size_t pos = 0, size_t len = npos) const;

//查找协议,域名,网址;
void test()
{
	string uri ("https://legacy.cplusplus.com/reference/string/string/substr/");
	size_t pos1 = uri.find("://");
	string protocol;
	if (pos1 != string::npos)
	{
	 protocol = uri.substr(0, pos1);
	}
	cout << protocol << endl;
	string domain;
	size_t pos2 = uri.find('/', pos1 + 3);
	if (pos2 != string::npos)
	{
		domain = uri.substr(pos1 + 3, pos2 - (pos1 + 3));
	}
	cout << domain << endl;
	string URL = uri.substr(pos2+1);
	cout << URL << endl;

}

除了正着查找还有反向查找

size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos, size_t n) const;
size_t rfind (char c, size_t pos = npos) const;

用法跟find相似。

string非成员函数

Non-member function overloads

istream& getline (istream& is, string& str, char delim);

stream& getline (istream& is, string& str);

我们知道,使用>>进行输入操作时,当>>读取到空格便会停止读取,基于此,我们将不能用>>将一串含有空格的字符串读入到string对象中。

void test1()
{
	string st;
	cin >> st;//hello world;
	cout << st << endl;//输出hello
}

我们使用getline就可以读取一行了

oid test1()
{
	string st;
	cin >> st;//hello world;
	getline(cin, st);
	cout << st << endl;//正常输出hello world
}

常用的string基本基本上就这些了,再用到哪些我们去查对应的文档就好。


http://www.kler.cn/a/588377.html

相关文章:

  • thefuck是如何帮助你提高命令行效率
  • C#开发笔记:INI文件操作
  • 分别用树型和UML结构展示java集合框架常见接口和类
  • 从Instagram到画廊:社交平台如何改变艺术家的展示方式
  • 掌握Windows命令提示符中的万能符:*号的全方位使用指南
  • Java基础编程练习第32题-定义接口(Biology、Animal)、类(Person)、子类(Pupil)(PTA选题)
  • 使用c#进行串口通信
  • 商品详情页
  • 第7章 站在对象模型的尖端3: RTTI
  • 考研系列-408真题计算机网络篇(10-17)
  • Centos 7 升级 GCC
  • 找第一个只出现一次的字符(信息学奥塞一本通-1130)
  • 基于大模型的智能客服搭建
  • doris:安全概览
  • R语言:初始环境配置
  • 【实战解析】smallredbook.item_get_video API:小红书视频数据获取与电商应用指南
  • 大语言模型基础--task2:大模型技术基础
  • 【Java】高频面试题
  • 图解多头注意力机制:维度变化一镜到底
  • 完整的模型验证套路