How to Map or Transform a Vector in C++ (Template Function)? | ninjasquad
Given a vector, e.g. of integers, we want to apply a function to each element and transform to another vector/array. For example, we might want to return a new array that squares each elements in the original array or list. C++ STL doesn’t provide the map function but we can provide a template function to convert an iterator (not necessary vector) to another using the given function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$ cat test.cpp && make test && ./test #include <iostream> #include <vector> using namespace std; template<typename T, typename F> T convert(T input, F f) { T out; for (auto x = input.begin(); x != input.end(); x++) { out.push_back(f(*x)); } return out; } int main() { vector<int> a({1, 2, 3}); auto b = convert(a, [](const int& n) { return n * n; }); for (int i = 0; i < static_cast<int>(b.size()); i++) { cout << b[i] << endl; } } make: 'test' is up to date. 1 4 9 |
$ cat test.cpp && make test && ./test #include <iostream> #include <vector> using namespace std; template<typename T, typename F> T convert(T input, F f) { T out; for (auto x = input.begin(); x != input.end(); x++) { out.push_back(f(*x)); } return out; } int main() { vector<int> a({1, 2, 3}); auto b = convert(a, [](const int& n) { return n * n; }); for (int i = 0; i < static_cast<int>(b.size()); i++) { cout << b[i] << endl; } } make: 'test' is up to date. 1 4 9
If we want to directly modify the input, we can use the transform function from the STL::algorithm. The transform function takes four parameters which are:
- begin of source iterator
- end of source iterator
- begin of destination iterator, could be the same of the begin of the source iterator
- transform function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ cat test.cpp && make && ./test #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> data({1,2,3,4,5}); std::transform(begin(data), end(data), begin(data), [](auto &n) { return n * n; }); std::for_each(begin(data), end(data), [](auto &c) { cout << c << ", "; }); cout << endl; return 0; } g++ -o test test.cpp 1, 4, 9, 16, 25, |
$ cat test.cpp && make && ./test #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> data({1,2,3,4,5}); std::transform(begin(data), end(data), begin(data), [](auto &n) { return n * n; }); std::for_each(begin(data), end(data), [](auto &c) { cout << c << ", "; }); cout << endl; return 0; } g++ -o test test.cpp 1, 4, 9, 16, 25,
The Makefile:
CC=g++ # compile to binary test test: test.cpp $(CC) -o test test.cpp # delete the binary clean: rm -f test
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading…
460 words
Last Post: Teaching Kids Programming – How to Design a Random Maze? Random Maze Generation Algorithm
The Permanent URL is: How to Map or Transform a Vector in C++ (Template Function)? (AMP Version)
Source: Internet