Difference between revisions of "CPP/STL/ForEach"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '<source lang = "cpp"> #include <iostream> #include <algorithm> #include <vector> using namespace std; void showSquaredValue(const int n){ cout << n << "*" << n << " = " …')
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 +
==ForEach.cpp==
 
<source lang = "cpp">
 
<source lang = "cpp">
 
 
#include <iostream>
 
#include <iostream>
 
#include <algorithm>
 
#include <algorithm>
 
#include <vector>
 
#include <vector>
  
using namespace std;
+
struct printFunctor
 +
{
 +
  void operator() (int x)
 +
  {
 +
    std::cout << x;
 +
  }
 +
 
 +
};
  
 +
int main()
 +
{
 +
  std::vector<int> v;
 +
  v.push_back(1);
 +
  v.push_back(2);
 +
  v.push_back(3);
 +
 +
  printFunctor myPrint;
 +
  std::for_each(v.begin(), v.end(), myPrint);
 +
 +
  return 0;
 +
}
 +
/*
 +
#include <iostream>
 +
#include <algorithm>
 +
#include <vector>
 +
 +
using namespace std;
 +
 
void showSquaredValue(const int n){
 
void showSquaredValue(const int n){
 
cout  <<  n << "*" << n  << " = " << n*n << endl;  
 
cout  <<  n << "*" << n  << " = " << n*n << endl;  
 
};
 
};
 
+
 
class RepeatSequence{
 
class RepeatSequence{
 
int begin, end;
 
int begin, end;
Line 21: Line 47:
 
}
 
}
 
};
 
};
 
+
 
int main(){
 
int main(){
vector<int> num;
+
vector<int> num;
const int MAX = 10;
+
const int MAX = 10;
+
   
  std::generate_n(std::back_insert_iterator<vector<int>>( num ),
+
std::generate_n(std::back_insert_iterator<vector<int>>( num ),
MAX,
+
MAX,
RepeatSequence(0,5)
+
RepeatSequence(0,5)
);
+
);
 +
 +
//for each values in the vector num, show its squared value
 +
std::for_each(num.begin(),num.end(),showSquaredValue);
 +
 +
return 0;
 +
}
 +
*/
 +
</source>
  
//for each values in the vector num, show its squared value
+
==CMakeLists.txt==
std::for_each(num.begin(),num.end(),showSquaredValue);
+
<source lang="cmake">
 +
cmake_minimum_required(VERSION 2.6)
  
  return 0;
+
PROJECT(ForEach)
}</source>
+
ADD_EXECUTABLE(ForEach ForEach.cpp )
 +
 
 +
</source>

Latest revision as of 13:16, 20 September 2011

ForEach.cpp

#include <iostream>
#include <algorithm>
#include <vector>
 
struct printFunctor
{
  void operator() (int x)
  {
    std::cout << x;
  }
 
};
 
int main()
{
  std::vector<int> v;
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
 
  printFunctor myPrint;
  std::for_each(v.begin(), v.end(), myPrint);
 
  return 0;
}
/*
#include <iostream>
#include <algorithm>
#include <vector>
 
using namespace std;
 
void showSquaredValue(const int n){ 	
	cout  <<  n << "*" << n  << " = " << n*n << endl; 
};
 
class RepeatSequence{
	int begin, end;
	int offset;
public:
	RepeatSequence(int first, int last): begin(first),end(last - 1), offset(begin - 1){}
	RepeatSequence(): begin(),end(), offset(){}
	int operator()(){
		return offset = offset >= end ? begin: offset + 1;
	}
};
 
int main(){
	vector<int> num;
	const int MAX = 10;
 
	std::generate_n(std::back_insert_iterator<vector<int>>( num ),
					MAX,
					RepeatSequence(0,5)
					);
 
	//for each values in the vector num, show its squared value 
	std::for_each(num.begin(),num.end(),showSquaredValue);
 
 return 0;
}
*/

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(ForEach)
ADD_EXECUTABLE(ForEach ForEach.cpp )