Created
March 20, 2019 15:53
-
-
Save jakab922/13575bf69f37f98aaff8403d7890d871 to your computer and use it in GitHub Desktop.
Python like join with C++ variadic templates
This file contains hidden or 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
// 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