#include <iostream>#include <vector>
#include <iterator>
int
main
( ) {// 创建一个 vector 容器并初始化
std
:: vector < int >vec
= { 1,
2,
3,
4,
5 } ;// 使用迭代器遍历 vector
for (
std
:: vector < int > :: iteratorit
=vec.
begin ( ) ;it
! =vec.
end ( ) ; ++it
) {std
:: cout << *it
<< " " ;}
std
:: cout <<std
:: endl ;// 使用 auto 关键字简化迭代器类型
for ( auto
it
=vec.
begin ( ) ;it
! =vec.
end ( ) ; ++it
) {std
:: cout << *it
<< " " ;}
std
:: cout <<std
:: endl ;// 使用 C++11 范围 for 循环
for ( int
elem
:vec
) {std
:: cout <<elem
<< " " ;}
std
:: cout <<std
:: endl ;return 0 ;
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/1562.html