现代C++中强调,使用基于范围的 for 循环(Visual studio 2012之后的),相比于旧版的 for 循环更整洁和易于使用,并且不容易发生意外错误。让我们一睹为快。

当然,使用前需要包含头文件:

#include <algorithm>

1 基于范围的for语句

基于范围的for语句(Range-based for Statement),其语句形式为:

for ( for-range-declaration : expression )
statement

看一段示例代码:

#include <iostream>
#include <vector>
using namespace std; int main()
{
cout << "Test 1 : ";
// Basic 10-element integer array.
int x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Range-based for loop to iterate through the array.
for( int y : x ) { // Access by value using a copy declared as a specific type.
// Not preferred.
cout << y << " ";
}
cout << endl; cout << "Test 2 : ";
// The auto keyword causes type inference to be used. Preferred.
for( auto y : x ) { // Copy of 'x', almost always undesirable
cout << y << " ";
}
cout << endl; cout << "Test 3 : ";
for( auto &y : x ) { // Type inference by reference.
// Observes and/or modifies in-place. Preferred when modify is needed.
cout << y << " ";
}
cout << endl; cout << "Test 4 : ";
for( const auto &y : x ) { // Type inference by reference.
// Observes in-place. Preferred when no modify is needed.
cout << y << " ";
}
cout << endl; cout << "Test 5 : ";
// Create a vector object that contains 10 elements.
vector<double> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i + 0.14159);
} // Range-based for loop to iterate through the vector, observing in-place.
for( const auto &j : v ) {
cout << j << " ";
}
cout << endl;
}

输出结果为:

Test 1 : 1 2 3 4 5 6 7 8 9 10
Test 2 : 1 2 3 4 5 6 7 8 9 10
Test 3 : 1 2 3 4 5 6 7 8 9 10
Test 4 : 1 2 3 4 5 6 7 8 9 10
Test 5 : 0.14159 1.14159 2.14159 3.14159 4.14159 5.14159 6.14159 7.14159 8.14159 9.14159

2 for_each语句

for_each语句,其语句形式为:

template<class InputIterator, class Function>
Function for_each(
InputIterator _First,
InputIterator _Last,
Function _Func
);

其中,_First是迭代器对应起始元素位置,_Last是迭代器对应的结束位置,必须是序列中可以访问的位置,_Func是用户定义的函数对象,将在迭代器范围内的所有元素,均应用到该函数中。

看一段代码示例:

// alg_for_each.h
#include <vector>
#include <algorithm>
#include <iostream> // The function object multiplies an element by a Factor
template <class Type>
class MultValue
{
private:
Type Factor; // The value to multiply by
public:
// Constructor initializes the value to multiply by
MultValue ( const Type& _Val ) : Factor ( _Val ) {
} // The function call for the element to be multiplied
void operator ( ) ( Type& elem ) const
{
elem *= Factor;
}
}; // The function object to determine the average
class Average
{
private:
long num; // The number of elements
long sum; // The sum of the elements
public:
// Constructor initializes the value to multiply by
Average ( ) : num ( 0 ) , sum ( 0 )
{
} // The function call to process the next elment
void operator ( ) ( int elem ) \
{
num++; // Increment the element count
sum += elem; // Add the value to the partial sum
} // return Average
operator double ( )
{
return static_cast <double> (sum) /
static_cast <double> (num);
}
};
// main.cpp
#include "alg_for_each.h" void main( )
{
vector <int> v1;
vector <int>::iterator Iter1; // Constructing vector v1
int i;
for ( i = -4 ; i <= 2 ; i++ )
{
v1.push_back( i );
} cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl; // Using for_each to multiply each element by a Factor
for_each ( v1.begin ( ) , v1.end ( ) , MultValue<int> ( -2 ) ); cout << "Multiplying the elements of the vector v1\n "
<< "by the factor -2 gives:\n v1mod1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl; // The function object is templatized and so can be
// used again on the elements with a different Factor
for_each (v1.begin ( ) , v1.end ( ) , MultValue<int> (5 ) ); cout << "Multiplying the elements of the vector v1mod\n "
<< "by the factor 5 gives:\n v1mod2 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl; // The local state of a function object can accumulate
// information about a sequence of actions that the
// return value can make available, here the Average
double avemod2 = for_each ( v1.begin ( ) , v1.end ( ) ,
Average ( ) );
cout << "The average of the elements of v1 is:\n Average ( v1mod2 ) = "
<< avemod2 << "." << endl;
}

运行结果为:

Original vector  v1 = ( -4 -3 -2 -1 0 1 2 ).
Multiplying the elements of the vector v1
by the factor -2 gives:
v1mod1 = ( 8 6 4 2 0 -2 -4 ).
Multiplying the elements of the vector v1mod
by the factor 5 gives:
v1mod2 = ( 40 30 20 10 0 -10 -20 ).
The average of the elements of v1 is:
Average ( v1mod2 ) = 10.

最新文章

  1. java 导出Excel 大数据量,自己经验总结!(二)
  2. (原)win8下编译GLUT
  3. js常用插件
  4. C# 引用类型的&quot;祸害&quot;
  5. mongodb基本操作及存储图片显示方案
  6. phpstorm IDE编辑器使用手记
  7. SQLSERVER读懂语句运行的统计信息
  8. webpack配合vue.js实现完整的单页面demo
  9. PHP递归算法的一个实例 帮助理解
  10. .NET Core 2.0 正式发布信息汇总
  11. Oracle_insert_delete_update
  12. 关于$ORACLE_HOME/bin/oracle文件属性
  13. maven 入门 (二)
  14. Android Camera开发:周期性循环自动聚焦auto focus挂掉原因分析(preview is not enabled)
  15. 《DSP using MATLAB》Problem 4.13
  16. sql标量值函数,将汉字转化为拼音,无音标
  17. OneDrive不能上了?DNS被污染,解决方法很简单
  18. 使用js插件进行设备检测
  19. mysql在查询结果列表前添加一列递增的序号列(最简)
  20. c#+mysql 中文乱码

热门文章

  1. 基于 OSGi 的面向服务的组件编程
  2. [BOI2007]摩基亚
  3. 光盘文件的挂载和yum源配置
  4. 想说再见不容易,win7最新市占率依然超36%
  5. [剑指offer] 7. 斐波那契数列 (递归 时间复杂度)
  6. [WPF] 圆形等待效果
  7. 样本方差的无偏估计与(n-1)的由来
  8. java拷贝字符文件
  9. scrapy研究探索(二)——爬w3school.com.cn
  10. Linux - Redmine使用方式 | SVN提交代码