Skip to content

Instantly share code, notes, and snippets.

@chrisjurich
Created April 11, 2020 07:05
Show Gist options
  • Save chrisjurich/a78287999b5548aa12fd5d11328a330a to your computer and use it in GitHub Desktop.
Save chrisjurich/a78287999b5548aa12fd5d11328a330a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
struct Strings : std::vector<std::string> {
std::vector<std::string> tokens;
// unfortunately you have to override all of the constructors
Strings(std::vector<std::string> input_tokens) : tokens(std::move(input_tokens)) {}
Strings(size_t size) {tokens.reserve(size);}
Strings(size_t size, std::string token) {
tokens.reserve(size);
for(auto it = 0; it<size; ++it) {
tokens.at(it) = token;
}
}
std::string
join(const std::string& delim ) {
auto output = std::string();
for(const auto & it : tokens) {
output += it + delim;
}
output.pop_back();
return output;
}
};
int main() {
auto tokens = Strings({"1", "2", "3", "4"});
std::cout << tokens.join("|") << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment