C++中string的迭代器如何使用?

2026-04-12 10:081阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计625个文字,预计阅读时间需要3分钟。

C++中string的迭代器如何使用?

目录

一、正向迭代器

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

三、反向迭代器

四、反向迭代器(只读数据)

五、使用auto替换这些特殊长类型名

一、正向迭代器

【例子】void test1() { string str; }

目录
  • 一、正向迭代器
  • 二、正向迭代器(只读数据)
  • 三、反向迭代器
  • 四、反向迭代器(只读)
  • 五、auto来替换这些特别长类型名

一、正向迭代器

//正向迭代器 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; }

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

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; }

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

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

C++中string的迭代器如何使用?

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

三、反向迭代器

作用:从后往前读。

//反向迭代器 void test3() { string str1 = "abcdef"; cout << "反向读取字符串:" << endl; string::reverse_iterator it1 = str1.rbegin(); while (it1 != str1.rend()) { *it1 += 1; cout << *it1 << " "; it1++; } cout << endl; }

四、反向迭代器(只读)

//反向迭代器(只读) void test4() { const string str1 = "abcdef"; cout << "反向只读读取字符串:" << endl; string::const_reverse_iterator it1 = str1.rbegin(); while (it1 != str1.rend()) { cout << *it1 << " "; it1++; } cout << endl; }

五、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; }

到此这篇关于C++迭代器iterator在string中使用方法介绍的文章就介绍到这了,更多相关C++迭代器iterator内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计625个文字,预计阅读时间需要3分钟。

C++中string的迭代器如何使用?

目录

一、正向迭代器

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

三、反向迭代器

四、反向迭代器(只读数据)

五、使用auto替换这些特殊长类型名

一、正向迭代器

【例子】void test1() { string str; }

目录
  • 一、正向迭代器
  • 二、正向迭代器(只读数据)
  • 三、反向迭代器
  • 四、反向迭代器(只读)
  • 五、auto来替换这些特别长类型名

一、正向迭代器

//正向迭代器 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; }

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

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; }

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

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

C++中string的迭代器如何使用?

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

三、反向迭代器

作用:从后往前读。

//反向迭代器 void test3() { string str1 = "abcdef"; cout << "反向读取字符串:" << endl; string::reverse_iterator it1 = str1.rbegin(); while (it1 != str1.rend()) { *it1 += 1; cout << *it1 << " "; it1++; } cout << endl; }

四、反向迭代器(只读)

//反向迭代器(只读) void test4() { const string str1 = "abcdef"; cout << "反向只读读取字符串:" << endl; string::const_reverse_iterator it1 = str1.rbegin(); while (it1 != str1.rend()) { cout << *it1 << " "; it1++; } cout << endl; }

五、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; }

到此这篇关于C++迭代器iterator在string中使用方法介绍的文章就介绍到这了,更多相关C++迭代器iterator内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!