Skip to content

Instantly share code, notes, and snippets.

@jakab922
Created March 20, 2019 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakab922/13575bf69f37f98aaff8403d7890d871 to your computer and use it in GitHub Desktop.
Save jakab922/13575bf69f37f98aaff8403d7890d871 to your computer and use it in GitHub Desktop.
Python like join with C++ variadic templates
// Compile and run with g++ -o vjoin --std=c++17 variadic_join.cc; ./vjoin
// c++17 standard might not be needed since we use the c++11 variadic template notation
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename T>
void join(stringstream &ss, const string &sep, T t) {
ss << t;
}
template<typename T, typename... Rest>
void join(stringstream &ss, const string &sep, T first, Rest... rest) {
ss << first << sep;
join(ss, sep, rest...);
}
int main() {
stringstream ss;
join(ss, ", ", "abbasd", 12, "cdsddsd", 13.1);
cout << ss.str() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment