C++学习笔记(二十三)

在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第二十三篇博客。

本篇博客介绍了C++的string容器。

本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。

目录

string容器

string容器基本概念

string构造函数

string赋值操作

string拼接

string查找和替换

string比较

string存取

string插入和删除

string子串获取


string容器

string容器基本概念

string是C++风格的字符串,本质是一个类。使用string需要包含头文件string

char *是一个指针。string是一个类,类内封装了char *,管理这个字符串。

string内封装了很多成员方法。

string管理char *分配的内存,不用担心复制越界和取值越界等问题,由类内进行负责。

string构造函数

string()创建一个空的字符串。

string(const char * s)使用字符串进行初始化。

string(const string& str)使用一个string对象初始化另一个string对象

string(int n,char c)使用n个字符c进行初始化。

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	const char* ch = "Hello C++ STL";
	string s1(ch);
	string s2(s1);
	string s3(10, 'h');
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	return 0;
}

程序的输出是:

Hello C++ STL
Hello C++ STL
hhhhhhhhhh

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;
int main(void)
{
	string s1;
	s1 = "Hello C++";
	cout << s1 << endl;

	string s2;
	s2 = s1;
	cout << s2 << endl;
	string s3;
	s3 = 'C';
	cout << s3 << endl;
	return 0;
}

程序的输出是:

Hello C++
Hello C++
C

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string s1;
	s1.assign("Hello C++");
	string s2;
	s2.assign("Hello C++", 5);
	string s3;
	s3.assign(s2);
	string s4;
	s4.assign(10, 'C');

	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	return 0;
}

程序的输出是:

Hello C++
Hello
Hello
CCCCCCCCCC

string拼接

string& operator += (const char* str)重载+=运算符。

string& operator += (const char c)重载+=运算符。

string& operator += (const string& str)重载+=运算符。

string& append(const char* s)将字符串s连接到当前字符串结尾。

string& append(const char* s,int n)把字符串s的前n个字符连接到当前字符串结尾。

string& append(const string &s)将s连接到当前字符串结尾。

string& append(const string &s,int pos,int n)将字符串s从pos开始的n个字符连接到字符串结尾。

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string s1("Hello");
	s1 += " C++";
	cout << s1 << endl;

	string s2("Iok");
	s2 += 'e';
	cout << s2 << endl;

	string s3("Sam");
	string s4(" FW");
	s3 += s4;
	cout << s3 << endl;
	return 0;
}

程序的输出是:

Hello C++
Ioke
Sam FW

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string s1("Hello");
	s1.append(" C++");
	cout << s1 << endl;
	string s2("Hello");
	s2.append("C++ or Java", 3);
	cout << s2 << endl;
	string s3("Sam");
	string s4("FW");
	s3.append(s4);
	cout << s3 << endl;

	string s5("This is a test");
	string s6("There is end-term");
	s6.append(s5, 9, 5);
	cout << s6 << endl;
	return 0;
}

程序的输出是:

Hello C++
HelloC++
SamFW
There is end-term test

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开始查找。

int rfind(const char* s,int pos = npos)const;查找s最后一次出现的位置,从pos开始查找。

int rfind(const char* s,int pos,int n)const;从pos开始查找s的前n个字符最后一次出现的位置。

int rfind(const char c,int pos = 0)const;查找字符c最后一次出现的位置。

string& replace(int pos,int n,const string& str)替换从pos开始的n个字符为字符串str。

string& replace(int pos,int n,const char* s)替换从pos开始的n个字符为字符串s。

位置指的是下标,从0开始。npos代表最后一个元素的位置。如果找不到,返回-1。

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string s1 = "abcdefghijklmnopqrstuvwxyz";
	cout << s1.find("de") << endl;
	cout << s1.find("fr") << endl;
	cout << s1.find("dennis", 0, 2) << endl;
	cout << s1.find("dennis", 0, 3) << endl;
	cout << s1.find('i') << endl;
	cout << endl;

	string s2 = "JimanaJuanJoaquinJulianJavierJeanneJuliaJovaJoseJohnJoyceJulietteJerryJulioJosephine";
	cout << s2.find("Ju") << endl;
	cout << s2.rfind("Ju") << endl;
	cout << s2.find("Jo", 0, 2) << endl;
	cout << s2.rfind("Jo", 84, 2) << endl;
	cout << s2.find('i') << endl;
	cout << s2.rfind('i') << endl;
	return 0;
}

程序的输出是:

3
18446744073709551615
3
18446744073709551615
8

6
70
10
75
1
81

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string s1 = "JimanaJuanJoaquinJulianJavierJeanneJuliaJovaJoseJohnJoyceJulietteJerryJulioJosephine";
	string s2 = "Jul";
	cout << s1.find(s2) << endl;
	cout << s1.rfind(s2) << endl;
	string s3 = "JK";
	cout << s1.find(s3) << endl;
	cout << s1.rfind(s3) << endl;

	string s4 = "abcedfghijklmnopqrstuvwxyz";
	s4.replace(20, 3, "20220717");
	cout << s4 << endl;
	s4.replace(9, 2, s3);
	cout << s4 << endl;
}

程序的输出是:

17
70
18446744073709551615
18446744073709551615
abcedfghijklmnopqrst20220717xyz
abcedfghiJKlmnopqrst20220717xyz

string比较

字符串比较按照ASCII码比较,相等返回0,大于返回1,小于返回-1。

int compare(const string &s)const;与字符串s比较。

int compare(const char* s)const;与字符串s比较。

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string s1 = "hello";
	string s2("hello");
	cout << s1.compare(s2) << endl;
	cout << s1.compare("Hello") << endl;
	cout << s1.compare("hi") << endl;
	return 0;
}

程序的输出是:

0
1
-1

string存取

char& operator[](int n)通过[]获取字符。

char& at(int n)通过at方法获取字符。

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string s = "hello";
	int i;
	for (i = 0; i < s.size(); i += 1) {
		cout << s[i] << " ";
	}
	cout << endl;

	for (i = 0; i < s.size(); i += 1) {
		cout << s.at(i) << " ";
	}
	cout << endl;
}

程序的输出是:

h e l l o
h e l l o

string插入和删除

string& insert(int pos,const char* s)将s插入到pos的位置。

string& insert(int pos,const string& str)将str插入到pos的位置。

string& insert(int pos,int n,char c)将n个c插入到pos的位置。

string& erase(int pos,int n = npos)删除从pos开始的n个字符。

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string s1 = "hello";
	s1.insert(1, "lll");
	cout << s1 << endl;
	s1.erase(1, 3);
	cout << s1 << endl;

	string s2 = "not";
	s1.insert(2,s2);
	cout << s1 << endl;
	s1.insert(4, 3, 'i');
	cout << s1 << endl;
	return 0;
}

程序的输出是:

hlllello
hello
henotllo
henoiiitllo
 

string子串获取

string substr(int pos = 0,int n = npos)const;返回从pos开始的n个字符组成的字符串。

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string s = "Keith";
	cout << s.substr(1, 2) << endl;
	cout << s.substr(3, 2) << endl;
	return 0;
}

程序的输出是:

ei
th

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值