Last active
January 18, 2021 23:45
-
-
Save monhime/b195fe345af8dbcbbac6551b96c56e4d to your computer and use it in GitHub Desktop.
vector<string>を全結合
This file contains 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 <iostream> | |
#include <vector> | |
#include <numeric> | |
#include <string> | |
using namespace std; | |
int main(){ | |
vector<string> A = {"I", "have", "a", "pen."}; | |
string sum_A = accumulate(A.begin() + 1, A.end(), A[0], [](string &init, string &v){ return init + v; }); | |
cout << sum_A << "\n"; // "Ihaveapen." | |
string sum_A2 = accumulate(A.begin() + 1, A.end(), A[0], [](string &init, string &v){ return init + " " + v; }); | |
cout << sum_A2 << "\n"; // "I have a pen." | |
auto f = [](string &init, string &v) -> string { return init + " " + v; }; | |
string sum_A3 = accumulate(A.begin() + 1, A.end(), A[0], f); | |
cout << sum_A3 << "\n"; // "I have a pen." | |
const string delim = ", "; | |
auto f2 = [&delim](string &init, string &v) -> string { return init + delim + v; }; | |
string sum_A4 = accumulate(A.begin() + 1, A.end(), A[0], f2); | |
cout << sum_A4 << "\n"; // "I, have, a, pen." | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment