T
Tom Lynch
Hey all,
I've been messing around trying to turn a std::transform to an
intermediate vector followed by a std::accumulate into a single
application of std::accumulate with a binary functor built "on the
fly" using Boost Bind.
Unfortunately, my best guess as to how to use the "nested" binds that
are required (specifying the placeholders of the main bind inside the
inner binds seems to be incorrect.
Be interested to hear any suggestions as to why this is or is not
possible, or a good idea
Tom
----------------------------
#include <boost/bind.hpp>
#include <functional>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
using bind;
struct StringReverser
{
StringReverser(const char* input): contents_(input) {}
string getReverse()
{
string result(contents_);
reverse(result.begin(), result.end());
return result;
}
string contents_;
};
int main()
{
vector<StringReverser> inputs;
// ... populate with whatever
const char* NAMES[] = {"Bob", "Larry", "David"};
inputs.assign(NAMES, NAMES + 3);
// transform, then accumulate: works
string acc;
vector<string> capitalised;
transform(inputs.begin(), inputs.end(), back_inserter(capitalised),
bind(&StringReverser::reverse, _1));
cout << accumulate(capitalised.begin(), capitalised.end(), acc,
plus<string>()) << endl;
// output is "boByrraLdivaD" as expected
// attempted one-liner: doesn't work
/*StringReverser base("");
cout << accumulate(inputs.begin(), inputs.end(), base,
bind(plus<string>(),
bind(&StringReverser::reverse, _1),
bind(&StringReverser::reverse, _2))) << endl;*/
return 0;
}
I've been messing around trying to turn a std::transform to an
intermediate vector followed by a std::accumulate into a single
application of std::accumulate with a binary functor built "on the
fly" using Boost Bind.
Unfortunately, my best guess as to how to use the "nested" binds that
are required (specifying the placeholders of the main bind inside the
inner binds seems to be incorrect.
Be interested to hear any suggestions as to why this is or is not
possible, or a good idea
Tom
----------------------------
#include <boost/bind.hpp>
#include <functional>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
using bind;
struct StringReverser
{
StringReverser(const char* input): contents_(input) {}
string getReverse()
{
string result(contents_);
reverse(result.begin(), result.end());
return result;
}
string contents_;
};
int main()
{
vector<StringReverser> inputs;
// ... populate with whatever
const char* NAMES[] = {"Bob", "Larry", "David"};
inputs.assign(NAMES, NAMES + 3);
// transform, then accumulate: works
string acc;
vector<string> capitalised;
transform(inputs.begin(), inputs.end(), back_inserter(capitalised),
bind(&StringReverser::reverse, _1));
cout << accumulate(capitalised.begin(), capitalised.end(), acc,
plus<string>()) << endl;
// output is "boByrraLdivaD" as expected
// attempted one-liner: doesn't work
/*StringReverser base("");
cout << accumulate(inputs.begin(), inputs.end(), base,
bind(plus<string>(),
bind(&StringReverser::reverse, _1),
bind(&StringReverser::reverse, _2))) << endl;*/
return 0;
}