• c++11 标准模板(STL)string(四)


    迭代器

    返回指向起始的迭代器

    1. std::basic_string<CharT,Traits,Allocator>::begin
    2. std::basic_string<CharT,Traits,Allocator>::cbegin
    1. iterator begin(); (C++11 前)
    2. iterator begin() noexcept; (C++11 起)
    3. const_iterator begin() const; (C++11 前)
    4. const_iterator begin() const noexcept; (C++11 起)
    5. const_iterator cbegin() const noexcept; (C++11 起)

    返回指向字符串首字符的迭代器。

    begin() 返回可变或常迭代器,取决于 *this 的常性。

    cbegin() 始终返回常迭代器。它等价于 const_cast<const basic_string&>(*this).begin() 。

     返回指向末尾的迭代器

    1. std::basic_string<CharT,Traits,Allocator>::end
    2. std::basic_string<CharT,Traits,Allocator>::cend
    1. iterator end(); (C++11 前)
    2. iterator end() noexcept; (C++11 起)
    3. const_iterator end() const; (C++11 前)
    4. const_iterator end() const noexcept; (C++11 起)
    5. const_iterator cend() const noexcept; (C++11 起)

    返回指向后随字符串末字符的字符的迭代器。此字符表现为占位符,试图访问它导致未定义行为。

    参数                                        (无)

    返回值                                        指向后随尾字符的字符的迭代器

    复杂度                                        常数

    返回指向起始的逆向迭代器

    1. std::basic_string<CharT,Traits,Allocator>::rbegin
    2. std::basic_string<CharT,Traits,Allocator>::crbegin
    1. reverse_iterator rbegin(); (C++11 前)
    2. reverse_iterator rbegin() noexcept; (C++11 起)
    3. const_reverse_iterator rbegin() const; (C++11 前)
    4. const_reverse_iterator rbegin() const noexcept; (C++11 起)
    5. const_reverse_iterator crbegin() const noexcept; (C++11 起)

    返回指向逆转字符串首字符的逆向迭代器。它对应非逆向字符串的末字符。

     参数                             (无)

    返回值                           指向首字符的逆向迭代器

    复杂度                          常数

    返回指向末尾的逆向迭代器

    1. std::basic_string<CharT,Traits,Allocator>::rend
    2. std::basic_string<CharT,Traits,Allocator>::crend
    1. reverse_iterator rend(); (C++11 前)
    2. reverse_iterator rend() noexcept; (C++11 起)
    3. const_reverse_iterator rend() const; (C++11 前)
    4. const_reverse_iterator rend() const noexcept; (C++11 起)
    5. const_reverse_iterator crend() const noexcept; (C++11 起)

    返回指向后随逆向字符串末字符的字符的逆向迭代器。它对应前驱非逆向字符串首字符的字符。此字符表现为占位符,试图访问它会导致未定义行为。

    参数                     (无)

    返回值                    指向后随末字符的字符的逆向迭代器

    复杂度                   常数

     代码示例

    1. string str = "abcdefg hijk";
    2. // 通过const_iterator迭代器顺序遍历string
    3. std::cout << "const_iterator: " << std::endl;
    4. for (string::const_iterator it = str.cbegin(); it != str.cend(); it++)
    5. {
    6. std::cout << *it << " " ;
    7. }
    8. std::cout << std::endl;
    9. // 通过iterator迭代器顺序修改string
    10. std::cout << "iterator: " << std::endl;
    11. for (string::iterator it = str.begin(); it != str.end(); it++)
    12. {
    13. *it += 3;
    14. }
    15. std::cout << str << std::endl;
    16. // 通过const_reverse_iterator迭代器逆序遍历string
    17. std::cout << "const_reverse_iterator: " << std::endl;
    18. for (string::const_reverse_iterator it = str.crbegin(); it != str.crend(); it++)
    19. {
    20. std::cout << *it << " " ;
    21. }
    22. std::cout << std::endl;
    23. // 通过reverse_iterator迭代器逆序修改string
    24. std::cout << "reverse_iterator: " << std::endl;
    25. for (string::reverse_iterator it = str.rbegin(); it != str.rend(); it++)
    26. {
    27. *it += 3;
    28. }
    29. std::cout << str << std::endl;

     

  • 相关阅读:
    SpringIoc依赖查找-5
    首选C++,彻底麻了!
    golang 编译器 汉化
    MSPM0G3507——GPIO例程讲解1——input_capture
    HTML5废除的元素
    国产高云FPGA开发软件Gowin的下载、安装、Licence共享,按照我的方案保证立马能用,不能用你铲我耳屎
    数据库篇:mysql锁详解
    约定编程与Sping AOP
    基于奇异谱分析法和长短时记忆网络组合模型的滑坡位移预测
    爱玛电动车:不止有时尚设计,更有领先的引擎动力科技
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/125629326