赞
踩
遍历:指的是集合中每个元素只访问一次。常见的方法有一下六种。
下面以字符串遍历与数组(向量)遍历为例
【字符串遍历】
string str("XDU");
传统Cfor
写法
for(size_t i = 0; i < str.size(); i++){
cout << str[i] << endl;
}
size_t:
一个基本的无符号整型的C/C++类型,它是sizeof操作返回的结果,该类型的大小可选择。因此,它可以存储在理论上可能的任何类型的数组的大小。
size_t相当于unsigned int 是无符号整型
typedef unsigned int size_t
迭代器for
写法
for(string::iterator it = str.begin(); it!=str.end(); it++){
cout << *it << endl;
}
STLfor_each
写法
for(char c : str){
cout << c <<endl;
}
C++11迭代器写法
for(string::iterator it = begin(str); it != end(str); it++){
cout << *it << endl;
}
或者
for(auto it = begin(str); it != end(str); it++){
cout<< *it << endl;
}
C++11 for loop scope 写法
for(char c : str){
cout << c << endl;
}
或者
for(auto c : str){
cout << c <<endl;
}
C++11 STL for_each
与lamdba表达式
//使用for_each遍历需要引用头文件#include<algorithm>
for_each(begin(str),end(str),[](char c){cout << c << endl;});
【C++代码】
#include<algorithm> int main() { string str = "XDU"; //遍历1 for (size_t i = 0; i < str.size(); i++) { cout << str[i] << endl; } cout << "--------------------------------" << endl; //遍历2 for (string::iterator it = str.begin(); it != str.end(); it++) { cout << *it << endl; } cout << "--------------------------------" << endl; //遍历3 for (char c : str) { cout << c << endl; } cout << "--------------------------------" << endl; //遍历4:写法一 for (string :: iterator it = begin(str); it != end(str); it++) { cout << *it << endl; } cout << "--------------------------------" << endl; //遍历4:写法二:用auto代替string::iterator 类型 for (auto it = begin(str); it != end(str); it++) { cout << *it << endl; } cout << "--------------------------------" << endl; //遍历5:写法一 for (char c : str) { cout << c << endl; } cout << "--------------------------------" << endl; //遍历5:写法二用auto代替char类型 for (auto c : str) { cout << c << endl; } cout << "--------------------------------" << endl; //遍历6:for_each for_each(begin(str), end(str), [](char c) { cout << c << endl; }); system("pause"); return 0; }
【结果】
【向量遍历】
vector<char> vec;
vec.push_back('X');
vec.push_back('D');
vec.push_back('U');
C语言写法
for(size_t i = 0; i < vec.size(); i++){
cout << vec[i] << endl;
}
迭代器
for(vector<char>::iterator it = vec.begin(); it != vec.end(); it++){
cout << *it << endl;
}
STL for_each写法
void print(char n){
cout << n <<endl;
}
for_each(vec.begin(), vec.end(), print);
C++11迭代器写法
for(vector<int>::iterator it = begin(vec); it != end(vec); it++){
cout << *it << endl;
}
或者
for(auto it = begin(vec); it != end(vec); it++){
cout << *it << endl;
}
C++11 for新语法写法
for(int n : vec){
cout << n << endl;
}
或者
for(auto n : vec){
cout << n << endl;
}
C++11STL for_each与lamdba表达式
for_each(begin(vec),end(vec),[](int n){cout << n << endl;});
【c++代码】
int main(){ Scout << "向量遍历" << endl; vector<char> vec; vec.push_back('M'); vec.push_back('B'); vec.push_back('Y'); //向量遍历1: for (size_t i = 0; i < vec.size(); i++) { cout << vec[i] << endl; } cout << "--------------------------------" << endl; //向量遍历2:迭代法写法一 for (vector<char>::iterator it = vec.begin(); it != vec.end(); it++) { cout << *it << endl; } cout << "--------------------------------" << endl; //向量遍历2:迭代法写法二 for (auto it = vec.begin(); it != vec.end(); it++) { cout << *it << endl; } cout << "--------------------------------" << endl; //向量遍历3:STL for_each for_each(vec.begin(), vec.end(),print); cout << "--------------------------------" << endl; //向量遍历4:C++迭代器写法 for (vector<char>::iterator it = begin(vec); it != end(vec); it++) { cout << *it << endl; } //或 for (auto it = begin(vec); it != end(vec); it++) { cout << *it << endl; } cout << "--------------------------------" << endl; //向量遍历 forC++新语法 for (auto n : vec) { cout << n << endl; } cout << "--------------------------------" << endl; //C++11STLfor_each与lamdba表达式 for_each(begin(vec), end(vec), [](char n) {cout << n << endl; }); cout << "--------------------------------" << endl; system("pause"); return 0; return 0; }
【结果】
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。