fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <cstdint>
  6.  
  7. using namespace std;
  8.  
  9. template <typename T>
  10. static string join(const string &sep, const vector<T> &arr) {
  11. stringstream ss;
  12. decltype(arr.size()) i = 0;
  13.  
  14. for (const auto &el : arr) {
  15. ss << el;
  16. i++;
  17. if (i < arr.size()) {
  18. ss << sep;
  19. }
  20. }
  21.  
  22. return ss.str();
  23. }
  24.  
  25. int main() {
  26. cout << join<string>("", {}) << endl;
  27. cout << join<string>("", {"1", "2", "3"}) << endl;
  28. cout << join<string>(":", {"1", "2", "3"}) << endl;
  29. cout << join<string>("vvv", {"111", "222", "333"}) << endl;
  30. cout << join<int>(":", {1, 2, 3}) << endl;
  31. cout << join<double>(":", {1.23, 2.23, 3.23}) << endl;
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 5556KB
stdin
Standard input is empty
stdout
123
1:2:3
111vvv222vvv333
1:2:3
1.23:2.23:3.23