1. normal method : for, iterator
2. for auto loop
3. for each loop
4. for_each
refer to this example
It would be better if I could handle the parallel loop.
This is next time.
Thank you.
< gist >
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://study.marearts.com/2017/05/for-each-foreach-for-auto-concept-clean.html | |
#include <stdio.h> | |
#include <iostream> | |
#include <vector> | |
#include <time.h> //rand | |
#include <algorithm> //for_each | |
using namespace std; | |
#define N 10 | |
void printfn(int e) | |
{ | |
cout << e << endl; | |
} | |
class sumClass{ | |
public: | |
sumClass() : sum(0),count(0){} | |
int getSum() { return sum; } | |
int getCount() { return count; } | |
void operator()(int e){ | |
sum = sum + e; | |
count++; | |
} | |
private: | |
int sum; | |
int count; | |
}; | |
int main() | |
{ | |
////////// | |
//rand seed | |
srand(time(0)); | |
////////// | |
////////// | |
//basic data | |
vector< int > VectorInt; | |
//data input | |
for (int i = 0; i < 10; ++i) | |
{ | |
VectorInt.push_back( rand() ); | |
} | |
////////// | |
////////// | |
//data access and print | |
////////// | |
printf(" #1-1 basic method - indexing \n"); | |
for (int i = 0; i < VectorInt.size(); ++i) | |
{ | |
cout << "[" << i << "] : " << VectorInt[i] << endl; | |
//printf("[%d] : %d \n", i, VectorInt[i]); | |
} | |
printf("\n"); | |
////////// | |
////////// | |
printf(" #1-2 basic method - iterator \n"); | |
vector<int>::iterator it; | |
int count = 0; | |
for (it = VectorInt.begin(); it != VectorInt.end(); it++) | |
{ | |
cout << "[" << count++ << "] : " << *it << endl; | |
//printf("[%d] : %d \n", count++, *it); | |
} | |
printf("\n"); | |
////////// | |
////////// | |
printf(" #2 - for auto loop - c++11 \n"); | |
printf("style 1 \n"); | |
count = 0; | |
for (auto it2 : VectorInt) | |
{ | |
cout << "[" << count++ << "] : " << it2 << endl; | |
} | |
printf("\n"); | |
printf("style 2 - it is useful when element is class or structure \n"); | |
count = 0; | |
for (auto &it2 : VectorInt) | |
{ | |
cout << "[" << count++ << "] : " << it2 << endl; | |
} | |
printf("\n"); | |
printf("style 3 \n"); | |
count = 0; | |
for (int it2 : VectorInt) | |
{ | |
cout << "[" << count++ << "] : " << it2 << endl; | |
} | |
printf("\n"); | |
////////// | |
////////// | |
printf(" #3 - for each loop - c++11 \n"); | |
printf("style 1 \n"); | |
count = 0; | |
for each(auto it3 in VectorInt) | |
{ | |
cout << "[" << count++ << "] : " << it3 << endl; | |
} | |
printf("\n"); | |
printf("style 2 \n"); | |
count = 0; | |
for each(auto& it3 in VectorInt) | |
{ | |
cout << "[" << count++ << "] : " << it3 << endl; | |
} | |
printf("\n"); | |
printf("style 3 \n"); | |
count = 0; | |
for each(int it3 in VectorInt) | |
{ | |
cout << "[" << count++ << "] : " << it3 << endl; | |
} | |
////////// | |
////////// | |
printf("\n"); | |
printf(" #3 - for each loop - STL \n"); | |
printf("example 1\n"); | |
for_each(VectorInt.begin(), VectorInt.end(), printfn); | |
printf("\n"); | |
printf("example 2\n"); | |
sumClass result = for_each(VectorInt.begin(), VectorInt.end(), sumClass()); | |
printf(" sum : %d, count = %d \n", result.getSum(), result.getCount()); | |
////////// | |
return 0; | |
} | |
< /gist >
No comments:
Post a Comment