C++ for循环的几种使用方法

  1. 普通的for循环
for(int n = 0; n < 50; ++n)
	std::cout << n << '\n';
  1. 用于容器的for循环
for(auto it=list.begin(); it!=list.end(); ++it)
	cout << *it << '\n'; 
  1. 简易for循环
for each(auto item in list)
	cout << item << '\n';
  1. 升级版简易for循环
for ( auto item : list)
	cout << item << '\n';
  1. 使用STL的for循环
// 其实是一个函数。将list的每个元素都放到最后一个参数(匿名函数)中处理
for_each(list.begin(),list.end(),[](string item)
{
	cout << item << '\n';
})
发布了239 篇原创文章 · 获赞 31 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/soulwyb/article/details/100876414