1.?for (type?&元素?: 数组)
2.?for (auto 指针变量?= 数组.begin();? 指针变量 != 数组.end(); 指针变量++)
3.?vector<type>::iterator iters;? ? ? ? //迭代器指针 ? ? for (iters = 数组.begin(); iters != 数组.end(); iters++)
4.?for_each(数组.begin(), 数组.end(), 函数);
#include<functional>
#include<algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
void funcs(string elem)
{
cout << elem << endl;
}
void funci(int elem)
{
cout << elem*3 << endl;
}
int main()
{
vector<string> msg = {"Fine!", "the C++", "World", "from", "Deepin", "with ATOM!"};
for (string &word : msg)
{
cout << word << " ";
}
cout << endl;
vector<int> v = {1,2,3,4};
v.push_back(7); v.push_back(9);
for (int &mint : v) {
cout << mint << " " << endl;
}
vector<char> v1 = {'a','b','c','d'};
v1.push_back('m'); v1.push_back('n');
for (char &mchar : v1) {
cout << mchar << " " << endl;
}
vector<string> v5 = {"aadf","sdfsf","werwr"};
v5.push_back("hello"); v5.push_back("world");
for (string &myvar : v5) { //for (string myvar : v5)
cout << myvar << " " << endl;
}
for (auto itemsi = v5.begin(); itemsi != v5.end(); itemsi++)
{
printf("v5b: ", *itemsi);
cout << "v5b: " << *itemsi << endl;
}
vector<string>::iterator iters;
for (iters = v5.begin(); iters != v5.end(); iters++)
{
printf("v5a: ", *iters);
cout << "v5a: " << *iters << endl;
}
for_each(msg.begin(), msg.end(), funcs);
for_each(v5.begin(), v5.end(), funcs);
for_each(v.begin(), v.end(), funci);
return 0;
}
刚学练的STL操作,继续学习。
?
|