Apply a callback to an array: Difference between revisions
Content added Content deleted
No edit summary |
(c++ stl for_each example) |
||
Line 15: | Line 15: | ||
end |
end |
||
# prints 1,4,9,16,25 |
# prints 1,4,9,16,25 |
||
==[[C++]]== |
|||
Compiler: [[GNU Compiler Collection]] gcc/g++ version 4.1.1 |
|||
Using c-style array |
|||
#include <iostream> //cout for printing |
|||
#include <algorithm> //for_each defined here |
|||
//create the function (print the square) |
|||
void print_square(int i) { |
|||
std::cout << i*i << " "; |
|||
} |
|||
int main() { |
|||
//create the array |
|||
int ary[]={1,2,3,4,5}; |
|||
//stl for_each |
|||
std::for_each(ary,ary+5,print_square); |
|||
return 0; |
|||
} |
|||
//prints 1 4 9 16 25 |
|||
Using std::vector |
|||
#include <iostream> //cout for printing |
|||
#include <algorithm> //for_each defined here |
|||
#include <vector> //stl vector class |
|||
//create the function (print the square) |
|||
void print_square(int i) { |
|||
std::cout << i*i << " "; |
|||
} |
|||
int main() { |
|||
//create the array |
|||
std::vector<int> ary; |
|||
ary.push_back(1); |
|||
ary.push_back(2); |
|||
ary.push_back(3); |
|||
ary.push_back(4); |
|||
ary.push_back(5); |
|||
//stl for_each |
|||
std::for_each(ary.begin(),ary.end(),print_square); |
|||
return 0; |
|||
} |
|||
//prints 1 4 9 16 25 |
|||
More tricky with binary function |
|||
#include <iostream> //cout for printing |
|||
#include <algorithm> //for_each defined here |
|||
#include <vector> //stl vector class |
|||
#include <functional> //bind and ptr_fun |
|||
//create a binary function (print any two arguments together) |
|||
template<class type1,class type2> |
|||
void print_juxtaposed(type1 x, type2 y) { |
|||
std::cout << x << y; |
|||
} |
|||
int main() { |
|||
//create the array |
|||
std::vector<int> ary; |
|||
ary.push_back(1); |
|||
ary.push_back(2); |
|||
ary.push_back(3); |
|||
ary.push_back(4); |
|||
ary.push_back(5); |
|||
//stl for_each, using binder and adaptable unary function |
|||
std::for_each(ary.begin(),ary.end(),std::bind2nd(std::ptr_fun(print_juxtaposed<int,std::string>),"x ")); |
|||
return 0; |
|||
} |
|||
//prints 1x 2x 3x 4x 5x |
Revision as of 22:27, 22 January 2007
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Apply a callback function to each element of an Array
Ruby
#create the array ary = [1,2,3,4,5] #create the function (print the square) def print_square(i) puts i**2 end #ruby ary.each do |i| print_square(i) end # prints 1,4,9,16,25
C++
Compiler: GNU Compiler Collection gcc/g++ version 4.1.1
Using c-style array
#include <iostream> //cout for printing #include <algorithm> //for_each defined here //create the function (print the square) void print_square(int i) { std::cout << i*i << " "; } int main() { //create the array int ary[]={1,2,3,4,5}; //stl for_each std::for_each(ary,ary+5,print_square); return 0; } //prints 1 4 9 16 25
Using std::vector
#include <iostream> //cout for printing #include <algorithm> //for_each defined here #include <vector> //stl vector class //create the function (print the square) void print_square(int i) { std::cout << i*i << " "; } int main() { //create the array std::vector<int> ary; ary.push_back(1); ary.push_back(2); ary.push_back(3); ary.push_back(4); ary.push_back(5); //stl for_each std::for_each(ary.begin(),ary.end(),print_square); return 0; } //prints 1 4 9 16 25
More tricky with binary function
#include <iostream> //cout for printing #include <algorithm> //for_each defined here #include <vector> //stl vector class #include <functional> //bind and ptr_fun //create a binary function (print any two arguments together) template<class type1,class type2> void print_juxtaposed(type1 x, type2 y) { std::cout << x << y; } int main() { //create the array std::vector<int> ary; ary.push_back(1); ary.push_back(2); ary.push_back(3); ary.push_back(4); ary.push_back(5); //stl for_each, using binder and adaptable unary function std::for_each(ary.begin(),ary.end(),std::bind2nd(std::ptr_fun(print_juxtaposed<int,std::string>),"x ")); return 0; } //prints 1x 2x 3x 4x 5x