2024-03-15 09:28:47 +00:00
|
|
|
#include <iostream>
|
2024-03-15 09:39:42 +00:00
|
|
|
#include <numeric>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class join_strings_functor {
|
|
|
|
private:
|
|
|
|
std::string separator_ = " ";
|
|
|
|
|
|
|
|
public:
|
|
|
|
join_strings_functor() = default;
|
|
|
|
join_strings_functor(std::string_view separator) : separator_(separator) {}
|
|
|
|
|
|
|
|
std::string operator()(const std::string &left,
|
|
|
|
const std::string &right) const {
|
|
|
|
return left + separator_ + right;
|
|
|
|
}
|
|
|
|
};
|
2024-03-15 09:28:47 +00:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
std::cout << "Functor" << std::endl;
|
|
|
|
|
2024-03-15 09:39:42 +00:00
|
|
|
std::vector<std::string> names = {"Albert", "Bernd", "Cora",
|
|
|
|
"Dave", "Emil", "Frank"};
|
|
|
|
|
|
|
|
auto space_joined_string =
|
|
|
|
std::accumulate(std::next(names.begin()), names.end(), names.front(),
|
|
|
|
join_strings_functor());
|
|
|
|
|
|
|
|
std::cout << "Space-joined string: " << space_joined_string << std::endl;
|
|
|
|
|
|
|
|
auto comma_joined_string =
|
|
|
|
std::accumulate(std::next(names.begin()), names.end(), names.front(),
|
|
|
|
join_strings_functor(", "));
|
|
|
|
|
|
|
|
std::cout << "Comma-joined string: " << comma_joined_string << std::endl;
|
|
|
|
|
2024-03-15 09:28:47 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|