C++STL----string类

初识string类

在这里插入图片描述

string类对象的构造

string类实现了多个构造函数的重载,常用的构造函数如下:

在这里插入图片描述

代码实例:

void test_1()
{
	string s1;
	string s2("hello world!!!");
	cout << s1 << endl;
	cout << s2 << endl;

	string s3(s2);
	cout << s3 << endl;

	string s4(s2, 6, 5);
	cout << s4 << endl;

	//未给第三个参数时,按照缺省值来拷贝
	string s5(s2, 6);
	cout << s5 << endl;

	string s6("hello world", 5);
	cout << s6 << endl;

	string s7(100, 'x');
	cout << s7 << endl;
}

结果如下

在这里插入图片描述

注意

空字符串中并不是什么都没有,其中存在一个’\0’字符;只是显示出来为空。

string类的容量和大小

1、使用size函数或length函数获取当前有效字符的个数

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	cout << s.size() << endl; //11
	cout << s.length() << endl; //11
	return 0;
}

2、使用max_size函数获取string对象对多可包含的字符数

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	cout << s.max_size() << endl; //2147483647
	return 0;
}

3、使用capacity函数获取当前对象所分配的存储空间的大小

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	cout << s.capacity() << endl; //15
	return 0;
}

4、使用resize改变字符串的大小
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str2 = "Hello";
	cout << "size: " <<str2.size() << endl;
	cout << "capacity: " << str2.capacity() << endl;
	cout << "Before resize: " << str2 << endl;

	str2.resize(8);
	cout << "size: " << str2.size() << endl;
	cout << "capacity: " << str2.capacity() << endl;
	cout << "After resize (to 8): " << str2 << endl;

	str2.resize(12, '!');
	cout << "size: " << str2.size() << endl;
	cout << "capacity: " << str2.capacity() << endl;
	cout << "After resize (to 12 with '!'): " << str2 << endl;
	return 0;
}

在这里插入图片描述

注意:若给出的n大于对象当前的capacity,则capacity也会根据自己的增长规则进行扩大。

5、使用reserve改变当前对象的容量大小
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	cout << s << endl; 
	cout << s.size() << endl; 
	cout << s.capacity() << endl; 

	//reverse(n)当n大于对象当前的capacity时,将当前对象的capacity扩大为n或大于n
	s.reserve(20);
	cout << s << endl; 
	cout << s.size() << endl; 
	cout << s.capacity() << endl; 

	//reverse(n)当n小于对象当前的capacity时,什么也不做
	s.reserve(2);
	cout << s << endl;
	cout << s.size() << endl; 
	cout << s.capacity() << endl; 
	return 0;
}

在这里插入图片描述

6、使用clear删除对象的内容,删除后对象变为空字符串

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	//clear()删除对象的内容,该对象将变为空字符串
	s.clear();
	cout << s << endl; //空字符串
	return 0;
}

7、使用empty判断对象是否为空

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	cout << s.empty() << endl;//0

	//clear()删除对象的内容,该对象将变为空字符串
	s.clear();
	cout << s.empty() << endl; //1
	return 0;
}

string类的访问及遍历

1、[ ]+下标
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	//[]+下标访问对象元素
	for (size_t i = 0; i < s.size(); i++)
	{
		cout << s[i];
	}
    //hello world
	cout << endl;

	//[]+下标修改对象元素内容
	for (size_t i = 0; i < s.size(); i++)
	{
		s[i] = 'x';
	}
	cout << s << endl;//xxxxxxxxxxx
	return 0;
}

2、使用at访问对象中的元素
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	for (size_t i = 0; i < s.size(); i++)
	{
		//at(pos)访问pos位置的元素
		cout << s.at(i);
	}
    //hello world
	cout << endl;

	for (size_t i = 0; i < s.size(); i++)
	{
		//at(pos)访问pos位置的元素,并对其进行修改
		s.at(i) = 'x';
	}
	cout << s << endl;//xxxxxxxxxxx
	return 0;
}

3、使用范围for访问对象中的元素

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	//使用范围for访问对象元素
	for (auto e : s)
	{
		cout << e;
	}
	//hello world
	cout << endl;

	//使用范围for访问对象元素,并对其进行修改
	for (auto& e : s) //需要修改对象的元素,e必须是引用类型
	{
		e = 'x';
	}
	cout << s << endl;//xxxxxxxxxxx
	return 0;
}

4、使用迭代器访问对象中的元素
在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");
	//使用迭代器访问对象元素
	string::iterator it1 = s.begin();
	while (it1 != s.end())
	{
		cout << *it1;
		it1++;
	}
	//hello world 
	cout << endl; 

	//使用迭代器访问对象元素,并对其进行修改
	string::iterator it2 = s.begin();
	while (it2 != s.end())
	{
		*it2 += 1;
		it2++;
	}
	cout << s << endl; //ifmmp!xpsme
	return 0;
}

string类中运算符

1、operator=

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1;
	string s2("hello world");

	//支持string类的赋值
	s1 = s2;
	cout << s1 << endl; //hello world

	//支持字符串的赋值
	s1 = "abcdef";
	cout << s1 << endl;  //abcdef

	//支持字符的赋值
	s1 = 'x';
	cout << s1 << endl; //x
	return 0;
}

2、operator+=

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1;
	string s2("hello world");

	//支持string类的复合赋值
	s1 += s2;
	cout << s1 << endl; //hello world

	//支持字符串的复合赋值
	s1 += "abcd";
	cout << s1 << endl; //hello worldabcd

	//支持字符的复合赋值
	s1 += '!';
	cout << s1 << endl; //hello worldabcd!
	return 0;
}

3、operator+

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s;
	string s1("super");
	string s2("man");
	char str[] = "woman";
	char ch = '!';

	//string类 + string类
	s = s1 + s2;
	cout << s << endl; //superman

	//string类 + 字符串
	s = s1 + str;
	cout << s << endl; //superwoman

	//字符串 + string类
	s = str + s1;
	cout << s << endl; //womansuper

	//string类 + 字符
	s = s1 + ch;
	cout << s << endl; //super!
	
	//字符 + string类
	s = ch + s1;
	cout << s << endl; //!super
	return 0;
}

string类对象的修改操作

插入

push_back

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s;
	s.push_back('h');
	s.push_back('e');
	s.push_back('l');
	s.push_back('l');
	s.push_back('o');
	cout << s << endl; //hello
	return 0;
}

insert

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("a"); //a

	//insert(pos, str)在pos位置插入字符串str
	s.insert(1, "b"); //ab

	//insert(pos, string)在pos位置插入string对象
	string t("c");
	s.insert(2, t); //abc

	//insert(pos, char)在pos位置插入字符char
	s.insert(s.end(), 'd'); //abcd
	
	cout << s << endl; //abcd
	return 0;
}

删除

1、使用pop_back进行尾删

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("C++");
	s.pop_back();
	s.pop_back();
	cout << s << endl; //C
	return 0;
}

2、使用erase删除

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("I like C++!!!");

	//erase(pos, n)删除pos位置开始的n个字符
	s.erase(8, 5); //I like C

	//erase(pos)删除pos位置的字符
	s.erase(s.end()-1); //I like

	//erase(pos1, pos2)删除[pos1pos2)上所有字符
	s.erase(s.begin() + 1, s.end()); //I

	cout << s << endl; //I
	return 0;
}

拼接

使用append函数完成string的拼接
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1("I");
	string s2(" like");

	//append(string)完成两个string对象的拼接
	s1.append(s2); //I like

	//append(str)完成string对象和字符串str的拼接
	s1.append(" C++"); //I like C++

	//append(n, char)将n个字符char拼接到string对象后面
	s1.append(3, '!'); //I like C++!!!
	
	cout << s1 << endl; //I like C++!!!
	return 0;
}

查找

1、使用find函数正向搜索第一个匹配项

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1("http://www.cplusplus.com/reference/string/string/find/");

	//find(string)正向搜索与string对象所匹配的第一个位置
	string s2("www");
	size_t pos1 = s1.find(s2);
	cout << pos1 << endl; //7

	//使用substr获取子串,配合find使用
	string s3 = s2.substr(0,3);
	size_t pos2 = s1.find(s3);
	cout << pos2 << endl;//7

	//find(str)正向搜索与字符串str所匹配的第一个位置
	char str[] = "cplusplus.com";
	size_t pos3 = s1.find(str);
	cout << pos3 << endl;  //11

	//find(char)正向搜索与字符char所匹配的第一个位置
	size_t pos4 = s1.find(':');
	cout << pos4 << endl; //4
	return 0;
}

2、使用rfind函数反向搜索第一个匹配项

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1("http://www.cplusplus.com/reference/string/string/find/");

	//rfind(string)反向搜索与string对象所匹配的第一个位置
	string s2("string");
	size_t pos1 = s1.rfind(s2);
	cout << pos1 << endl; //42

	//rfind(str)反向搜索与字符串str所匹配的第一个位置
	char str[] = "reference";
	size_t pos2 = s1.rfind(str);
	cout << pos2 << endl;  //25

	//rfind(char)反向搜索与字符char所匹配的第一个位置
	size_t pos3 = s1.rfind('/');
	cout << pos3 << endl; //53
	return 0;
}

比较

使用compare函数完成比较
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1("hello world");
	string s2("hello abcd");

	//"hello world"和"hello abcd"比较
	cout << s1.compare(s2) << endl; //1

	//"ell"和"hello abcd"比较
	cout << s1.compare(1, 3, s2) << endl; //-1

	//"hello"和"hello"比较
	cout << s1.compare(0, 4, s2, 0, 4) << endl; //0

	return 0;
}

替换

replace函数完成string的替换
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("hello world");

	//replace(pos, len, str)将pos位置开始的len个字符替换为字符串str
	s.replace(6, 4, "abcd"); //hello abcd

	//replace(pos, len, n, char)将pos位置开始的len个字符替换为n个字符char
	s.replace(10, 1, 3, '!'); //hello abcd!!!
	cout << s << endl;
	return 0;
}

交换

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
main ()
{
   string buyer ("money");
   string seller ("goods");
   seller.swap (buyer);
   cout <<buyer<<endl;//goods
   cout <<seller <<endl;//money
   return 0;
}

string类中的getline函数

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

这时,可用getline函数完成一串含有空格的字符串的读取操作

在这里插入图片描述

getline函数将从输入缓冲区中提取到的字符存储到str中,直到读取到换行符’\n’为止

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s;
	getline(cin, s); //输入:hello world
	cout << s << endl; //输出:hello world
	return 0;
}

getline函数将从输入缓冲区中提取到的字符存储到str中,直到读取到分隔符delim或换行符’\n’为止

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s;
	getline(cin, s, 'd'); //输入:hello world
	cout << s << endl; //输出:hello CS
	return 0;
}

string类中的c_str函数

在这里插入图片描述

#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
int main()
{
	string	filename("memory.cpp");
    //当需要用C语言中的函数来读取文件时,就需要用到c_str函数
	FILE* fout = fopen(filename.c_str(), "r");
	assert(fout);
	char ch = fgetc(fout);
	while (ch!=EOF)
	{
		cout << ch;
		ch = fgetc(fout);
	}
	return 0;
}

string类在的to_string函数

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
int main()
{
	int ival;
	double dval;
    //输入1234 12.34
	cin >> ival >> dval;
    //将数字转换为字符串
	string istr = to_string(ival);
	string dstr = to_string(dval);

    //输出结果为 1234 12.340000
	cout << istr << endl;
	cout << dstr << endl;
	return 0;
}

注意:由于浮点数不能在内存中精确保存,故转换为字符串时,可能小数点后面的数有偏差

编码知识补充

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值