Created
April 29, 2021 00:50
-
-
Save RinChanNOWWW/ee9173c14a645e336d7dd4e3b70a6763 to your computer and use it in GitHub Desktop.
join function implementation in c++
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 <numeric> | |
#include <string> | |
#include <vector> | |
template <class T> | |
std::string join(std::vector<T> vec, char connector) { | |
if (vec.size() == 0) { | |
return ""; | |
} | |
if (vec.size() == 1) { | |
return std::to_string(vec[0]); | |
} | |
return std::accumulate( | |
std::next(vec.begin()), vec.end(), std::to_string(vec[0]), | |
[connector](std::string a, T b) { | |
return std::move(a) + connector + std::to_string(b); | |
}); | |
} | |
int main() { | |
std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 9, 10}; | |
// 1.2.3.4.5.6.7.9.10 | |
std::cout << join(vec, '.') << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment