#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <iterator>
#include <iostream>
// ------------------------------------
typedef std::vector<std::string> string_vector;
// Functor created by the wizard
struct size_sorter
: public std::binary_function<std::string, std::string, bool>
{
bool operator() (const std::string &left, const std::string &right) const
{
return (left.size() < right.size());
}
};
void test_functor_solution(string_vector sv)
{
std::sort(sv.begin(), sv.end(), size_sorter());
std::copy(sv.begin(), sv.end(), std::ostream_iterator<std::string>(std::cout, " "));
}
// ------------------------------------
void test_lambda_solution(string_vector sv)
{
// Sort with lambda created by the wizard
std::sort(sv.begin(), sv.end(),
[] (const std::string &left, const std::string &right) -> bool
{
return (left.size() < right.size());
}
);
std::copy(sv.begin(), sv.end(), std::ostream_iterator<std::string>(std::cout, " "));
}
// ------------------------------------
int main()
{
string_vector test_data;
test_data.push_back("george");
test_data.push_back("nick");
test_data.push_back("peter");
test_functor_solution(test_data);
std::cout << std::endl;
test_lambda_solution(test_data);
return 0;
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHN0cmluZz4KCiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxmdW5jdGlvbmFsPgojaW5jbHVkZSA8aXRlcmF0b3I+CgojaW5jbHVkZSA8aW9zdHJlYW0+CgovLyAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KCnR5cGVkZWYgc3RkOjp2ZWN0b3I8c3RkOjpzdHJpbmc+IHN0cmluZ192ZWN0b3I7CgovLyBGdW5jdG9yIGNyZWF0ZWQgYnkgdGhlIHdpemFyZApzdHJ1Y3Qgc2l6ZV9zb3J0ZXIKICAgIDogcHVibGljIHN0ZDo6YmluYXJ5X2Z1bmN0aW9uPHN0ZDo6c3RyaW5nLCBzdGQ6OnN0cmluZywgYm9vbD4KewoJYm9vbCBvcGVyYXRvcigpIChjb25zdCBzdGQ6OnN0cmluZyAmbGVmdCwgY29uc3Qgc3RkOjpzdHJpbmcgJnJpZ2h0KSBjb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