初探c++:string类的进阶运用

1.begin()和end(),前一个指向字符串的第一个字符,第二个指向字符串的\0

		string s=("hello world");
		string::iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

这是正向迭代器的经典应用

如果要实现反向迭代器,就要使用以下的格式

		string s=("hello world");
		string::reverse_iterator reit = s.rbegin();
		while (reit != s.rend())
		{
			cout << *reit << ' ';
			reit++;
		}

如果string是const类型的,使用const迭代器const_reverse_iterator,此迭代器与普通迭代器的区别是这个不能写只能读,另一个都行,特别的,反向读取不能使用正向迭代器--实现,会报错,

而且auto reit可以代替上文迭代器的代码

	string s=("hello world");
	cout<<s.at(2);

s.at()与s[]是一样的效果

在某位置前插入字符串

	string s=("world");
	s.insert(0, "hello ");
	cout << s;

		string s=("world");
		s.insert(0, 5,'!');
		cout << s;

也可以插入一定数量的字符

将0替换为迭代器s.begin()代替0也可以

删除字符第几个位置删除几个字符

	string s=("world");
	s.erase(3,1);
	cout << s;

string s=("world");
s.erase(s.begin()+3);
cout << s;

此代码和上面代码效果一样,默认删除一个

如果删除数据大于字符串长度,则默认从起始字符开始删完,插入删除数据的迭代器不建议使用,效率低,需要挪动数据

修改迭代器

string s=("world");
s.replace(3,2,"%%d");
cout << s;

(起始修改位置,覆盖位置数量,修改内容)

寻找迭代器:

	string s=("world");
	size_t good = s.find('r');
	cout << good<<endl;
	size_t good1 = s.find('b');
	cout << good1;

找到返回下标,找不到返回npos,也就是最大的整数

	string s=("roroor");
	size_t good = s.find('r');
	while (good != string::npos)
	{
		s.replace(good, 1, "!");
		good = s.find('r');
	}
	cout << s;

循环会一直往下找

	string s=("roroor");
	size_t good = s.find('r');
	while (good != string::npos)
	{
		s.replace(good, 1, "!");
		good = s.find('r',good+1);
	}
	cout << s;

改进算法,不需要从头找,也可以统计r的数量,先用reseve开足够的空间,以免replace不断开空间,下面是效率最高的方法

	string s=("roroor");
	string s1;
	for (auto ch : s)
	{
		if (ch != 'r')
		{
			s1 += ch;
		}
		else
		{
			s1 += '!';
		}
	}
	cout << s1;

swap(string元素),表示交换两个字符串

c_str:返回c形式的字符串,以‘'\0'为终止,作用是与c语言的接口相匹配

寻找文件后缀:

	string file("string.cpp");
	size_t good = file.find('.');
	if (good != string::npos)
	{
		string suffix = file.substr(good, file.size() - good);
		cout << suffix;
	}

substr的用法为从前者下标复制字符串到后者下标

使用find_first_of来实现字符串中多个字符的替换

		std::string str("please replace the letters signed");
		std::size_t found = str.find_first_of("abcd");
		while (found !=std:: string::npos)
		{
			str[found] = '*';
			found = str.find_first_of("abcd", found + 1);

		}
		cout << str;

相比于find_first_of正着找,find_last_of为倒着找

		std::string str("please replace the letters signed");
		std::size_t found = str.find_last_of("abcd");
		while (found !=std:: string::npos)
		{
			str[found] = '*';
			found = str.find_first_of("abcd");

		}
		cout << str;

此代码效果与上面的相同

题目

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

int main() {
string str;
getline(cin,str);
size_t pos=str.rfind(' ');
if(pos!=std::string::npos)
{
cout<<str.size()-pos-1;
}
else {
{
    cout<<str.size();
}
}
}

getline(cin,字符串)与cin不同前者结束符为换行符,后者为空格

相关推荐

  1. C++编程 --- 2.初始STL

    2024-04-04 20:52:02       43 阅读
  2. Linux初学(十二)AWK

    2024-04-04 20:52:02       29 阅读
  3. 指针

    2024-04-04 20:52:02       47 阅读
  4. CSS

    2024-04-04 20:52:02       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-04 20:52:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-04 20:52:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-04 20:52:02       87 阅读
  4. Python语言-面向对象

    2024-04-04 20:52:02       96 阅读

热门阅读

  1. python笔记(11)序列

    2024-04-04 20:52:02       42 阅读
  2. Windows下Docker创建Mysql5.7

    2024-04-04 20:52:02       35 阅读
  3. 泛型(Generics)

    2024-04-04 20:52:02       37 阅读
  4. Lightroom Classic LRC安装教程介绍

    2024-04-04 20:52:02       44 阅读
  5. 动态规划 Leetcode 647 回文子串

    2024-04-04 20:52:02       36 阅读
  6. 【Python语法实例】-13发牌游戏代码高阶

    2024-04-04 20:52:02       32 阅读
  7. 【Leetcode】279.完全平方数

    2024-04-04 20:52:02       36 阅读
  8. Docker资源管理和分配指南

    2024-04-04 20:52:02       33 阅读