[C++提高编程](三):STL-string容器

string基本概念

本质

  • string是c++风格的字符串,本质上是一个类

string和char *的区别

  • char *是一个指针
  • string是一个类,内部封装了char*,管理这个字符串,是一个char*的容器

特点

  • string类内部封装了很多成员函数
  • 查找find、拷贝copy、删除delete、替换replace、插入insert
  • string管理char*分配的内存,不用担心越界等问题,由类内部进行负责

string构造函数

  • string();                                 //默认构造,创建一个空字符串
  • string(const char* s);            //使用字符串s初始化
  • string(const string& str);       //使用一个string对象初始化另一个string对象
  • string(int n, char c);              //使用n个字符c初始化
#include <iostream>
#include <string>

using namespace std;


void test(void)
{
	string s1;//默认构造,创建一个空字符串

	const char* str = "Hello World!";
	string s2(str);//使用字符串s初始化

	cout << "s2=" << s2 << endl;

	string s3(s2);//使用一个string对象初始化另一个string对象
	cout << "s3=" << s3 << endl;

	string s4(10, 'a'); //使用n个字符c初始化
	cout << "s4=" << s4 << endl;
}


int main()
{
	test();
	return 0;
}

string赋值操作

赋值函数原型 描述
string& operator=(const char *s); char*类型字符串,赋值给当前字符串
string& operator=(const string &s); 字符串s赋值给当前字符串
string& operator=(char c); 字符赋值给当前字符串
string& assign(const char *s); 字符串s赋值给当前字符串
string& assign(const char *s, int n); 字符串s的前n个字符赋值给当前字符串
string& assign(const string &s); 把字符串s赋值给当前字符串
string& assign(int n, char c); 用n个字符c赋值给字符串
#include <iostream>
#include <string>

using namespace std;

void test(void)
{
	string str1;

	str1 = "Hello World.";
	cout << "str1 =" << str1 << endl;

	string str2 = str1;
	cout << "str2 =" << str2 << endl;

	string str3;
	str3 = 'a';
	cout << "str3 =" << str3 << endl;

	string str4;
	str4.assign("Hello C++!");
	cout << "str4 =" << str4 << endl;

	string str5;
	str5.assign("Hello C++!", 5);
	cout << "str5 =" << str5 << endl;

	string str6;
	str6.assign(str5);
	cout << "str6 =" << str6 << endl;
	
	string str7;
	str7.assign(5, 'a');
	cout << "str7 =" << str7 << endl;
}

int main()
{
	test();
	return 0;
}

string字符串拼接

字符串拼接函数原型 描述
string& operator+=(const char *s); 重载+=操作符
string& operator+=(const string &s); 重载+=操作符
string& operator+=(char c); 重载+=操作符
string& append(const char *s); 把字符串s连接到当前字符串末尾                
string& append(const char *s, int n); 把字符串s的前n个字符连接到当前字符串末尾 
string& append(const string &s); 同operator+=(const string &s);
string& append(const string &s, int pos, int n); 字符串s中从pos开始的n个字符连接到当前字符串末尾 
#include <iostream>
#include <string>

using namespace std;

void test(void)
{
	string str1 ="我";

	str1 += "爱玩游戏";
	cout << "str1 = " << str1 << endl;

	str1 += ':';
	cout << "str1 = " << str1 << endl;

	string str2("LOL/DNF");
	str1 += str2;
	cout << "str1 = " << str1 << endl;

	string str3 = "I";
	str3.append(" Love ");
	cout << "str3 = " << str3 << endl;

	str3.append("game abcde", 4);
	cout << "str3 = " << str3 << endl;

	str3.append(":LOL ");
	cout << "str3 = " << str3 << endl;

	str3.append("DNF GG", 0, 3);
	cout << "str3 = " << str3 << endl;
}

int main()
{
	test();
	return 0;
}

string查找和替换

字符串查找和替换函数原型 描述
int find(const string& str, int pos = 0) const;  查找str第一次出现的位置,从pos开始查找
int find(const char * s, int pos = 0) const; 查找s第一次出现位置,从pos开始查找
int find(const char * s, int pos, int n) const; 从pos位置查找s的前n个字符第一次位置
int find(const char c, int pos = 0) const; 查找字符c第一次出现位置
int rfind(const string& str, int pos = npos) const; 查找str最后一次位置,从pos开始查找(从右往左查)
string& replace(int pos, int n, const string& str); 替换 从pos开始的n个字符为字符串str
string& replace(int pos, int n, const char *s); 替换 从pos开始的n个字符为字符串s
#include <iostream>
#include <string>

using namespace std;

void test(void)
{
	string str1 ="abcdefde";

	int pos = str1.find("de");

	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else {
		cout << "找到字符串:" << pos << endl;
	}
	//rfind从右往左查找,find从左往右查找
	pos = str1.rfind("de");
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else {
		cout << "找到字符串:" << pos << endl;
	}

	string str2 = "hello";
	cout << "str1替换前:" << str1 << endl;
	//从0个字符起3个字符替换为str2
	str1.replace(0, 3, str2);
	cout << "str1替换后:" << str1 << endl;

}

int main()
{
	test();
	return 0;
}

string字符串比较

比较方式:按字符的ASCII码进行对比

  • = 返回 0
  • > 返回 1
  • < 返回 -1
字符串比较函数原型 描述
int compare(const string &s)const;  与字符串s比较
int find(const char * s) const; 与字符串s比较

string字符串存取

字符串存取函数原型 描述
char& operator[](int n);  通过[]方式取字符
char& at(int n); 通过at方式取字符
#include <iostream>
#include <string>

using namespace std;

void test(void)
{
	string str1 ="abcdefde";

	cout << str1[0] << endl;
	str1[0] = 'x';
	cout << str1[0] << endl;

	cout << str1.at(1) << endl;
	str1.at(1) = 'm';
	cout << str1[1] << endl;
	cout << str1 << endl;
}

int main()
{
	test();
	return 0;
}

string字符串插入和删除

字符串插入和删除函数原型 描述
string& insert(int pos, const char* s);  插入字符串
string& inser(int pos, const string& str); 插入字符串
string& insert(int pos, int n, char c); 在指定位置插入n个字符c
string& erase(int pos, int n = npos); 删除从pos开始的n个字符
#include <iostream>
#include <string>

using namespace std;

void test(void)
{
	string str1 ="abcdefde";
	cout << "str1 = " << str1 << endl;

	str1.insert(1, "Hello");
	cout << "str1 = " << str1 << endl;

	str1.erase(1, 5);
	cout << "str1 = " << str1 << endl;
}

int main()
{
	test();
	return 0;
}

string字串

字符串字串函数原型 描述
string substr(int pos = 0, int n = npos)const;  返回由pos开始的n个字符组成的字符串
#include <iostream>
#include <string>

using namespace std;

void test(void)
{
	string str1 ="san.Liu@qq.com";
	cout << "str1 = " << str1 << endl;
	
	int pos = str1.find('@');
	cout << "pos = " << pos << endl;

	string Substr = str1.substr(0, pos);
	cout << "str1 = " << Substr << endl;
}

int main()
{
	test();
	return 0;
}

推荐阅读:[C++提高编程](三):STL初识

相关推荐

  1. [C++提高编程]():STL-string容器

    2024-03-30 13:42:03       20 阅读
  2. [C++提高编程]():STL初识

    2024-03-30 13:42:03       16 阅读
  3. C++提高编程——STL:函数对象

    2024-03-30 13:42:03       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-30 13:42:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-30 13:42:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-30 13:42:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-30 13:42:03       20 阅读

热门阅读

  1. 高等代数复习:矩阵秩的基本公式

    2024-03-30 13:42:03       16 阅读
  2. Manticore Search 中文分词搜索入门

    2024-03-30 13:42:03       17 阅读
  3. 在ROS2-foxy环境中配置nooploop-linktrack

    2024-03-30 13:42:03       17 阅读
  4. Android JNI---入门了解

    2024-03-30 13:42:03       14 阅读
  5. 信号量或互斥锁

    2024-03-30 13:42:03       19 阅读
  6. VSCode中6个AI顶级插件

    2024-03-30 13:42:03       29 阅读
  7. Openreview公式不能正常显示

    2024-03-30 13:42:03       13 阅读
  8. linux终端介绍

    2024-03-30 13:42:03       19 阅读