Created
June 4, 2019 12:24
-
-
Save cblp/46b2574d056f6394763d3c4ce6a35056 to your computer and use it in GitHub Desktop.
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
#include <algorithm> | |
using std::transform; | |
#include <iostream> | |
using std::cerr; | |
using std::cout; | |
using std::endl; | |
#include <string> | |
using std::string; | |
#include <vector> | |
using std::vector; | |
#include <boost/assign.hpp> | |
using namespace boost::assign; | |
#include <boost/range/iterator.hpp> | |
using boost::make_iterator_range; | |
#include <boost/range/adaptors.hpp> | |
using boost::adaptors::transformed; | |
#include <boost/range/algorithm.hpp> | |
using boost::range::transform; | |
#include <boost/range/algorithm_ext/push_back.hpp> | |
//////////////////////////////////////////////////////////////////////////////// | |
namespace detail { | |
struct push_back_symbol{}; | |
template <typename Range> | |
struct push_back_range{ | |
Range range; | |
template <typename Container> | |
operator Container() { | |
Container c; | |
push_back(c, range); | |
return c; | |
} | |
}; | |
} | |
detail::push_back_symbol push_back() { | |
return {}; | |
} | |
template <typename Range> | |
detail::push_back_range<Range> | |
operator|(Range range, detail::push_back_symbol) { | |
return {range}; | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
int main() { | |
vector<string> args{"all", "your", "base", "are", "belong", "to", "us"}; | |
vector<const char *> cargs1(args.size()); | |
transform( | |
begin(args), | |
end(args), | |
begin(cargs1), | |
[](auto & arg){ return arg.c_str(); } | |
); | |
cout << make_iterator_range(cargs1) << endl; | |
vector<const char *> cargs2; | |
for (auto & arg : args) | |
cargs2.push_back(arg.c_str()); | |
cout << make_iterator_range(cargs2) << endl; | |
vector<const char *> cargs3; | |
transform( | |
args, | |
back_inserter(cargs3), | |
[](auto & arg){ return arg.c_str(); } | |
); | |
cout << make_iterator_range(cargs3) << endl; | |
vector<const char *> cargs4; | |
push_back( | |
cargs4, args | transformed([](auto & arg){ return arg.c_str(); }) | |
); | |
cout << make_iterator_range(cargs4) << endl; | |
vector<const char *> cargs5 | |
= args | |
| transformed([](auto & arg){ return arg.c_str(); }) | |
| push_back(); | |
cout << make_iterator_range(cargs5) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fmap c_str