当前位置: 首页 > news >正文

C++系列-STL容器中的for循环遍历方式

STL容器中的for循环遍历方式

  • 普通的for循环
  • 利用迭代器for循环
  • for循环新用法 for (auto i_st : st)
  • for_each循环


淮上喜会梁川故友 韦应物
江汉曾为客,相逢每醉还。
浮云一别后,流水十年间。
欢笑情如旧,萧疏鬓已斑。
何因不归去,淮上有秋山。


在遍历容器中的元素时,一般我们会使用如下的几种遍历方式。

普通的for循环

  • for (int i_loop = 0; i_loop < st.size(); i_loop++)
  • 利用set的size作为循环条件。
code:
#include <iostream>
#include <set>
using namespace std;template<typename T>
void print_set_normal(const set<T>& st)
{set<int>::iterator it = st.begin();for (int i_loop = 0; i_loop < st.size(); i_loop++){cout << *it << " ";it++;}cout << endl;
}void test01()
{set<int> st1{23, 1, 56, 78, 44, 35, 99, 76};	// 初始化列表构造函数cout << "---------- st1, st1.size() ---------- " << st1.size() << endl;print_set_normal(st1);cout << endl;
}
int main()
{test01();system("pause");return 0;
}result:
---------- st1, st1.size() ---------- 8
1 23 35 44 56 76 78 99

利用迭代器for循环

  • for (set::iterator it = st.begin(); it != st.end(); it++)
  • 利用迭代器begin(), end()作为循环条件,有的容器不支持<,可以用!=。
code:
#include <iostream>
#include <set>
using namespace std;template<typename T>
void print_set_iterator(const set<T>& st)
{for (set<int>::const_iterator it = st.begin(); it != st.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{set<int> st1{23, 1, 56, 78, 44, 35, 99, 76};	// 初始化列表构造函数cout << "---------- st1, st1.size() ---------- " << st1.size() << endl;print_set_iterator(st1);cout << endl;
}
int main()
{test01();system("pause");return 0;
}result:
---------- st1, st1.size() ---------- 8
1 23 35 44 56 76 78 99

for循环新用法 for (auto i_st : st)

  • for (auto i_st : st)
code:
#include <iostream>
#include <set>
using namespace std;template<typename T>
void print_set_for_auto(const set<T>& st)
{for (auto i_st : st)		// auto在这里表示自动类型推导,在这里也可以for (int i_st1: st1){cout << i_st << " ";}cout << endl;
}void test01()
{set<int> st1{23, 1, 56, 78, 44, 35, 99, 76};	// 初始化列表构造函数cout << "---------- st1, st1.size() ---------- " << st1.size() << endl;print_set_for_auto(st1);cout << endl;
}
int main()
{test01();system("pause");return 0;
}result:
---------- st1, st1.size() ---------- 8
1 23 35 44 56 76 78 99

for_each循环

  • for_each(st.begin(), st.end(), func)
  • 要使用for_each,先要导入头文件,#include <algorithm>
code:
#include <iostream>
#include <set>
using namespace std;template<typename T>
void print_set_for_auto(const set<T>& st)
{for (auto i_st : st)		// auto在这里表示自动类型推导,在这里也可以for (int i_st1: st1){cout << i_st << " ";}cout << endl;
}void test01()
{set<int> st1{23, 1, 56, 78, 44, 35, 99, 76};	// 初始化列表构造函数cout << "---------- st1, st1.size() ---------- " << st1.size() << endl;print_set_for_auto(st1);cout << endl;
}
int main()
{test01();system("pause");return 0;
}result:
---------- st1, st1.size() ---------- 8
1 23 35 44 56 76 78 99

http://www.mrgr.cn/news/18358.html

相关文章:

  • 华为云征文|基于华为云Flexus X实例部署Redis及案例实践详解
  • MySQL主从同步
  • 沉浸式体验亚马逊云科技上私有化部署零一万物AI大模型
  • 2222. 分糖果(candy)
  • 深度解析MFT损坏:原因、恢复策略与预防措施
  • 监控平台之批量上报
  • 新员工一口气写完了这些C语言例子,领导给他转正了!
  • mysql(查询)
  • 【C++】N卡无法录制,如何下载C++
  • 【软件测试专栏】软件测试 — 用例篇
  • 什么是Java中的封装?请举例说明如何通过封装实现数据隐藏和访问控制。请解释Java中的抽象类和抽象方法的作用和使用场景。
  • KAN学习Day1——模型框架解析及HelloKAN
  • Linux--网络指令UDP,TCPwindows连接服务器
  • jenkins-gitee-genkins
  • FPGA与高速ADC LVDS数据接口设计考虑
  • 2.6 时序与总线操作
  • 代码随想录算法训练营四十八天|739.每日温度、496.下一个更大元素 I、503.下一个更大元素II
  • 计算机网络之体系结构
  • 解释头结点、第一个结点(或称首元结点)、头指针这三个概念的定义及其区别
  • 12_Linux时间处理操作