Created
April 11, 2020 07:05
-
-
Save chrisjurich/a78287999b5548aa12fd5d11328a330a to your computer and use it in GitHub Desktop.
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 <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