NO.27十六届蓝桥杯备战|string|创建|输入|getline|size|iterator|begin|end|push_back|+=和+|pop_back(C++)

string概念

string 字符串其实是⼀种更加⾼级的封装, string 字符串中包含⼤量的⽅法,这些⽅法使得字符串的操作变得更加简单。 string 使⽤的好,慢慢你就不想使⽤字符数组来存放字符串了。
C++中将字符串直接作为⼀种类型,也就是 string 类型,使⽤ string 类型创建的对象就是C++的字符串。

string s1;  
string s2 = "abc";

string常⻅操作

string - C++ Reference

创建字符串
方式解释
string s1创建空字符串
string s2 = “hello world”创建字符串(常⽤)
#include <iostream>  
#include <string> //添加string头⽂件  

using namespace std;  

int main()  
{  
	string s1;  
	string s2 = "hello world";  
	cout << "s1:" << s1 << endl; //s1:  
	cout << "s2:" << s2 << endl; //s2:hello world  
	
	return 0;  
}

创建字符串的⽅式与前⾯学习到创建内置数据类型的⽅式相同,只是这⾥的字符串类型为 string

  1. string s1 表⽰创建空字符串,相当于创建整型 int a ,但未给 a ⼀个初始值。
  2. string s2 = “hello world” 表⽰创建⼀个字符串s2,它的内容是" hello world ",要注意 s2 中的字符串不再以 \0 作为结束标志了。(C语⾔中的字符串是以 \0 作为结束标志的)。
    ![[Pasted image 20250306202049.png]]

除了以上创建字符串的写法外,C++中还有⼀些其他的创建字符串⽅式。

string s("hello world"); //等同于string s1 = "hello world";  
string s1 = s; //⽤⼀个现成的字符串s,初始化另外⼀个字符串s1

当然C++中的 string 创建的字符串和 char 类型的数组所表⽰的字符串还有⼀个区别, string类型的字符串对象可以直接赋值

#include <iostream>  
#include <string>  
using namespace std;  

int main()  
{  
	string s1("hello world");  
	string s2("hehe");  
	s2 = s1;  
	cout << s2 << endl;  
	
	return 0;  
}
string字符串的输⼊
cin的⽅式

可以直接使⽤ cin 给 string 类型的字符串中输⼊⼀个字符串的数据

#include <iostream>  
#include <string>  
using namespace std;  

int main()  
{  
	string s;  
	//输⼊  
	cin >> s;  
	//输出  
	cout << s << endl;  
	
	return 0;  
}

输⼊不带空格的字符串
![[Pasted image 20250306202310.png]]

输⼊带空格的字符串
![[Pasted image 20250306202322.png]]

这⾥我们可以发现,其实 cin 的⽅式给 string 类型的字符串中输⼊数据的时候,可以输⼊不带空格的字符串。但是如果带有空格,遇到空格也就读取结束了,没有办法正常读取,那怎么办呢?解决办法就是使⽤ getline

getline的⽅式

getline 是C++标准库中的⼀个函数,⽤于从输⼊流中读取⼀⾏⽂本,并将其存储为字符串。getline 函数有两种不同的形式,分别对应着字符串的结束⽅式

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

istream 是输⼊流类型, cin 是 istream 类型的标准输⼊流对象。
ostream 是输出流类型, cout 是 ostream 类型的标准输出流对象。
getline 函数是输⼊流中读取⼀⾏⽂本信息,所有如果是在标准输⼊流(键盘)中读取数据,就可以传 cin 给第⼀个参数。

第⼀种 getline 函数以换⾏符( ‘\n’ )作为字符串的结束标志,它的⼀般格式是

getline(cin, string str)  
//cin -- 表⽰从输⼊流中读取信息  
//str 是存放读取到的信息的字符串

这种形式的 getline 函数从输⼊流(例如 cin )中读取⽂本,直到遇到换⾏符( ‘\n’ )为⽌,然后将读取到的⽂本(不包括换⾏符)存储到指定的 string 类型的变量 str 中。

//代码1  
#include<iostream>  
#include <string>  
using namespace std;  
int main ()  
{  
	string name;  
	getline (cin, name);  
	cout << name << endl;  
	
	return 0;  
}

![[Pasted image 20250306202607.png]]

第⼆种 getline 函数允许⽤⼾⾃定义结束标志,它的⼀般格式是

getline(cin, string str, char delim)  
//cin -- 表⽰从输⼊流中读取信息  
//str 是存放读取到的信息的字符串  
//delim 是⾃定义的结束标志

这种形式的 getline 函数从输⼊流中读取⽂本,直到遇到⽤⼾指定的结束标志字符( delim )为⽌,然后将读取到的⽂本(不包括结束标志字符)存储到指定的 string 类型的变量 str 中

//代码2  
#include<iostream>  
#include <string>  
using namespace std;  
int main ()  
{  
	string name;  
	getline (cin, name, 'q');  
	cout << name << endl;  
	
	return 0;  
}

![[Pasted image 20250306202659.png]]

在使⽤C++中的 string 字符串时,想要输⼊的字符串中包含空格,那么 getline 函数就是必须的。
在竞赛中为了⽅便处理字符串,通常会使⽤ string 类型的字符串,所以在字符串输⼊的时候 getline 就很常⻅。

size()

string 中提供了 size() 函数⽤于获取字符串⻓度。
在C++中关于字符串的操作函数都是包含在 string 中的,所以需要调⽤这些函数时,通常⽤ . 点运算符。

#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  

int main()  
{  
	string s;  
	string s1 = "hello";  
	string s2 = "hello world";  
	string s3 = "12ab!~ ";  
	cout << "s:" << s.size() << endl;  
	cout << "s1:" << s1.size() << endl;  
	cout << "s2:" << s2.size() << endl;  
	cout << "s3:" << s3.size() << endl;  
	
	return 0;  
}

![[Pasted image 20250306202830.png]]

前⾯我们学到的像 char 、 int 、 double 等内置类型的数据在操作的时候,不会使⽤ . 操作符的。
string 是C++提供的⼀种更加复杂的封装类型,在 string 类型的变量中加⼊了操作这个字符串的各种⽅法(函数),⽐如求字符串⻓度、字符串末尾插⼊⼀个字符等操作。所以要对 string 类型的变量进⾏各种操作,就可以使⽤ . 操作符来使⽤这些函数。

通过 size() 能获得字符串的⻓度,顺便就可以使⽤这个⻓度遍历字符串的,注意 string 类型的
字符串是可以通过下标访问的

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

![[Pasted image 20250306203109.png]]

迭代器(iterator)

迭代器是⼀种对象,它可以⽤来遍历容器(⽐如我们现在学习的 string )中的元素,迭代器的作⽤类似于指针,或者数组下标
不过访问迭代器指向的值,需要解引⽤(*)
C++中的 string 提供了多种迭代器,⽤于遍历和操作字符串中的内容。这⾥给⼤家介绍⼀种最常⽤的迭代器。

begin() 和 end()
  • begin() :返回指向字符串第⼀个字符的迭代器,需要⼀个迭代器的变量来接收。
  • end() :返回指向字符串最后⼀个字符的下⼀个位置的迭代器(该位置不属于字符串)。
  • string 中 begin() 和 end() 返回的迭代器的类型是 string::iterator
string s = "abcdef";

![[Pasted image 20250306203244.png]]

  • 迭代器是可以进⾏⼤⼩⽐较,也可以进⾏ + 或者 - 整数运算的。
    ⽐如: it++ ,就是让迭代器前进⼀步, it-- 就是让迭代器退后⼀步。
  • 同⼀个容器的两个迭代器也可以相减,相减结果的绝对值,是两个迭代器中间元素的个数。
#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string s = "abcdef";  
	string::iterator it1 = s.begin();  
	string::iterator it2 = s.end();  
	cout << (it1 < it2) << endl;  
	cout << it1 - it2 << endl;  
	
	return 0;  
}

![[Pasted image 20250306203446.png]]

正序遍历

迭代器通常⽤于遍历字符串的,可以正序遍历,也可以逆序遍历

#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{
	string s = "abcdef";  
	//auto it 是让编译器⾃动推到it的类型  
	for (auto it = s.begin(); it != s.end(); ++it)  
	{  
		cout << *it << ' ';  
	}  
	//string::iterator 是正向迭代器类型  
	//string::iterator it,是直接创建迭代器,it是针对字符串的迭代器  
	for (string::iterator it = s.begin(); it != s.end(); ++it)  
	{  
		cout << *it << ' ';  
	}  
	
	return 0;  
}
逆序遍历
#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string s = "abcdef";  
	for (string::iterator it = s.end() - 1; it >= s.begin(); --it)  
	{  
		cout << *it << ' ';  
	}  
	
	return 0;  
}

![[Pasted image 20250306204131.png]]

通过迭代器找到元素后,改变迭代器指向的元素,是可以直接改变字符串内容的

#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string str = "abcdef";  
	cout << str << endl;  
	for (string::iterator it = str.begin(); it != str.end(); ++it)  
	{  
		*it = 'x';  
	}  
	cout << str << endl;  
	
	return 0;  
}

![[Pasted image 20250306204212.png]]

push_back()

push_back() ⽤于在字符串尾部插⼀个字符
![[Pasted image 20250306204236.png]]

#include <iostream>  
#include<string> //添加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;  
	
	//向⾮空字符串中尾插字符  
	string s1 = "hello ";  
	s1.push_back('w');  
	s1.push_back('o');  
	s1.push_back('r');  
	s1.push_back('l');  
	s1.push_back('d');  
	cout << s1 << endl;  
	
	//批量插⼊字符  
	string s2;  
	for (char c = 'a'; c <= 'f'; c++)  
	{  
		s2.push_back(c);  
	}  
	cout << s2 << endl;  
	return 0;
}

![[Pasted image 20250306204341.png]]

字符串的+=和+运算

push_back() 是⽤于在字符串后添加⼀个字符,然⽽部分情况下我们需要向原有的字符串后继续添加字符串。
其实 string 类型的字符串是⽀持 + 和 += 运算的。这⾥的本质是 string 中重载了operator+= 这个操作符。

#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  

int main()  
{  
	string s = "hello";  
	s += " world";       //字符串⽤双引号,等价于 s = s + " world"
	cout << s << endl;
	
	//除了+=操作,也可以使⽤'+'灵活进⾏字符串拼接  
	
	//1.尾部拼接
	string s1 = "hello";  
	cout << s1 + " world" << endl; //s1仍然是"hello"  
	s1 = s1 + " world";  
	cout << s1 << endl; //s1是"hello world"  
	
	//2.头部拼接  
	string s2 = "hello";  
	s2 = "world " + s2 ;  
	cout << s2 << endl; //s2为:"world hello"  
	
	return 0;  
}

![[Pasted image 20250306210642.png]]

pop_back()

pop_back() ⽤于删除字符串中尾部的⼀个字符。这个成员函数是在 C++11 标准中引⼊的,有的编译器可能不⽀持。
![[Pasted image 20250306210713.png]]

#include <iostream>  
#include<string>  
using namespace std;
int main()  
{  
	string s = "hello";  
	cout << "s:" << s << endl;  
	
	//尾删  
	s.pop_back();  
	cout << "s:" << s << endl;  
	
	//尾删  
	s.pop_back();  
	cout << "s:" << s << endl;  
	
	return 0;  
}

![[Pasted image 20250306210844.png]]

但是当字符串中没有字符的时候,再调⽤ pop_back() 时,程序会出现异常。这种⾏为也是未定义⾏为,要避免这么使⽤

#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s;  
	s.pop_back();  
	
	return 0;  
}

![[Pasted image 20250306210918.png]]

为避免循环删除导致对空字符串进⾏尾删,可以将上⾯错误的代码改写成

#include <iostream>  
#include<string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "abc";  
	while(s.size() > 0) //通过size()函数来控制字符串的⻓度  
	{  
		s.pop_back();  
	}  
	
	return 0;  
}

不可对空字符串继续进⾏pop_back()操作,否则程序出现异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值