• 【C ++基础】迭代器(iterator)在string里面的简单使用


    【C ++基础】迭代器(iterator)在string里面的简单使用

    前言

    本文是为了扫清后面学习的难点,而特意写的文章,只是介绍迭代器如何在string中使用。

    迭代器的详细解释请看这里:

    [点击跳转(这里还没有写哦)]

    C++专栏主页: C++学习

    正文

    一、正向迭代器

    image-20221027104304687

    【例子】

    //正向迭代器
    void test1()
    {
    	string str1 = "abcdef";
    
    	cout << "读取字符串:" << endl;
    	string::iterator it1 = str1.begin();
    	while (it1 != str1.end())
    	{
    		cout << *it1 << " ";
    		it1++;
    	}
    	cout << endl;
    
    	cout << "每个字母向后移动一位:" << endl;
    	string::iterator it2 = str1.begin();
    	while (it2 != str1.end())
    	{
    		*it2 +=1;
    		cout << *it2 << " ";
    		it2++;
    	}
    	cout << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    【运行结果】

    image-20221027105234662

    二、正向迭代器(只读数据)

    const_iterator begin( ) const;

    这种迭代器,只支持读,不支持修改数据。

    【例子】

    //只读正向迭代器
    void test2()
    {
    	const string str1 = "abcdef";
    
    	cout << "只能读取字符串:" << endl;
    	string::const_iterator it1 = str1.begin();
    	while (it1 != str1.end())
    	{
    		cout << *it1 << " ";
    		it1++;
    	}
    	cout << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    image-20221027110939279

    【问题】

    为什么不能直接在 string::iterator it 前面加const?

    答:这样的话,const修饰的是it,it将无法被修改,并不是*it无法被修改。

    it无法被修改的后果是无法遍历。

    三、反向迭代器

    image-20221027111101517

    作用:从后往前读。

    【例子】

    //反向迭代器
    void test3()
    {
    	string str1 = "abcdef";
    
    	cout << "反向读取字符串:" << endl;
    	string::reverse_iterator it1 = str1.rbegin();
    	while (it1 != str1.rend())
    	{
    		*it1 += 1;
    		cout << *it1 << " ";
    		it1++;
    	}
    	cout << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    【运行结果】

    image-20221027111340082

    四、反向迭代器(只读)

    【例子】

    //反向迭代器(只读)
    void test4()
    {
    	const string str1 = "abcdef";
    
    	cout << "反向只读读取字符串:" << endl;
    	string::const_reverse_iterator it1 = str1.rbegin();
    	while (it1 != str1.rend())
    	{
    		cout << *it1 << " ";
    		it1++;
    	}
    	cout << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    五、auto来替换这些特别长类型名

    是不是感觉这些类型名特别长?别担心,用auto试试。

    //auto
    void test5()
    {
    	cout << "auto的演示" << endl;
    	const string str1 = "abcdef";
    	cout << "反向只读读取字符串:" << endl;
    	auto it1 = str1.rbegin();
    	while (it1 != str1.rend())
    	{
    		cout << *it1 << " ";
    		it1++;
    	}
    	cout << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    六、后记

    你是不是觉得这次的文章特别水?那么说明你已经是大神的水平啦。

    这篇文章的主要作用还是迭代器的简单使用说明指南,再介绍string类的时候将不再详细讲解。

  • 相关阅读:
    基于Kubernetes集群构建MongoDB
    点成分享 | 一文读懂什么是微流控芯片
    jmeter模拟多用户并发
    【湖科大教书匠】计算机网络随堂笔记第5章(计算机网络运输层)
    代码随想录算法训练营|day42
    线上流量卡申请须知
    SpringBoot集成Activiti7
    Dijkstra算法求最短路
    CMake常用命令(一) cmake_minimum_required
    error: unable to unlink old ‘.gitlab-ci.yml‘: Permission denied
  • 原文地址:https://blog.csdn.net/m0_54381284/article/details/127548820