【C++教程从0到1入门编程】第七篇:STL中string类

本文详细介绍了C++中string类的构造方法、容量操作、访问与遍历(包括[]、迭代器和范围for)、修改操作(如push_back、insert、erase等),以及非成员函数的使用,如c_str、rfind和substr的应用实例。
摘要由CSDN通过智能技术生成

在使用string类时,必须包含#include头文件以及using namespace std。

npos是一个静态的全局变量为-1补码无穷大
 

string类的常用接口说明

1.string类对象的常见构造
#define _CRT_SECURE_NO_WARNINGS   1
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
#include<string>
int main()
{
	//4个构造函数
	string s1;
	string s2("hello");
	string s3(s2);
	string s4(5, 'a');
	string s5 = "hello";
	string s6 = s2;

	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	//析构函数先释放s4,再s3,再s2,再s1
	cout << s5 << endl;
	cout << s6 << endl;

	s1 = s2;
	cout << s1 << endl;
	return 0;
}

void TestString1()            //构造函数
{
	string s1;                 //ok
	string s2("bit");          //ok
	string s3(s2);             //ok
	string s4("bit", 1);
	string s5("bit education", 1, 2);
	string s6("bit education", 1, string::npos); //npos是一个静态的全局变量为-1补码无穷大
	string s7("bit education", 1,20);
	string s8(5, 'a');

	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;
	cout << s6 << endl;
	cout << s7 << endl;
	cout << s8 << endl;

	s1 = s8;                //赋值 ok
	cout << s1 << endl;
}

2.string类对象的容量操作
 
void TestString7() //string类的容量操作
{
	string s1("bit education");
	string s2("C++");
	cout << s1.size() << endl;
	cout << s2.size() << endl;
	cout << s1.length() << endl;
	cout << s2.length() << endl;

	cout << s1.max_size() << endl;
	cout << s2.max_size() << endl;

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

	s1.clear();                  //size减为0
	cout << s1 << endl;
	cout << s1.capacity() << endl;
}

3.string类对象的访问及遍历

void TestString2()         //[]+下标的遍历,重载了+=运算符----追加
{
	string s1("bit");

	s1 += 'e';
	cout << s1 << endl;
	s1 += ' ';
	s1 += "ducation";
	cout << s1 << endl;

	//[]+下标的遍历方式
	for (size_t i = 0; i < s1.size(); i++)   //读
	{
		cout << s1[i] << " ";
	}
	cout << endl;

	for (size_t i = 0; i < s1.size(); i++)  //写,修改字符
	{
		s1[i] = s1[i] + 1;
	}
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
}

迭代器的遍历

void TestString3()    //迭代器的遍历
{
	string s1("bit education");
	string::iterator it = s1.begin();
	//auto it=s1.begin();auto自动推导it是迭代器
	//vector<int>::iterator it=s1.begin();
	//读
	while (it != s1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	//写
	it = s1.begin();
	while (it != s1.end())
	{
		*it = *it + 2;
		it++;
	}
	cout << s1 << endl;

	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	vector<int>::iterator vit = v1.begin();
	while (vit != v1.end())
	{
		cout << *vit << " ";
		vit++;
	}
	cout << endl;
}

范围for的遍历 C++11原理是迭代器
void TestString4()    //范围for的遍历 C++11原理是迭代器
{
	string s1("bit education");
	for (auto s : s1)   //依次取出容器s1里面的字符给s
	{
		cout << s << " ";
	}
	cout << endl;

	for (auto s : s1)
	{
		s = s + 1;
		cout << s << " ";
	}
	cout << endl;
}
反向迭代器,倒着遍历

void TestString5()    //反向迭代器,倒着遍历
{
	string s1("bit education");
	string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
}

4. string类对象的修改操作
void TestString11()  //string 类对象的修改操作
{
	string s("bite");
	cout << s<< endl;
	s.push_back(' ');
	cout << s << endl;
	s.append("education");
	cout << s << endl;
	s += "!";
	cout << s << endl;

	s.insert(s.begin(), 'B');
	cout << s << endl;
	s.insert(0, "A");
	cout << s << endl;

	s.erase(2, 3);
	cout << s << endl;
	s.erase(s.begin(),s.end());
	cout << s << endl;

}

c_str

转换后遇到\0就会结束,string则会全部输出

rfind和substr

void TestString13()   //find获取文件的后缀,rfind可以倒着找
{
	string s1("string.cpp.zip");
	string s2("string.c");
	string s3("string.txt");
	size_t pos1 = s1.find('.');

	if (pos1 != string::npos)
	{
		cout << s1.substr(pos1) << endl;
	}
	size_t pos2 = s2.find('.');
	if (pos2 != string::npos)
	{
		cout << s2.substr(pos2) << endl;
	}
	size_t pos3= s3.find('.');
	if (pos3 != string::npos)
	{
		cout << s3.substr(pos3) << endl;
	}

	//协议 域名 资源名称
	string stringcpp("http://www.cplusplus.com/reference/string/string/find/");

	string &url = stringcpp;
	size_t pos4 = url.find(":");
	if (pos4 != string::npos)
	{
		cout << url.substr(0,pos4) << endl;
	}
	size_t pos5 = url.find('/', pos4 + 3);
	if (pos5 != string::npos)
	{
		cout << url.substr(pos4 + 3, pos5 - pos4-3) << endl;
	}
	cout << url.substr(pos5 + 1) << endl;
}

5. string类非成员函数
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值