I'm trying to use variadic templates (for the first time really) to make a string replacement function.
Essentially, I want to make a function (we'll call it Replace) that takes a key value that is used to search up and modify a template string based on the additional parameters provided in the Replace function. The template string has placeholders using the string "%s".
My problem is, I'm not sure how to iterate through the variadic template parameters... Here is some sample code.
const std::string FindOriginal(std::string Key)
{
if(Key == "Toon")
{
return "This is example %s in my program for Mr. %s.";
}
return "";
}
std::string Replace(std::string OriginalKey) {
return "";
}
template<typename First, typename ... Strings>
std::string Replace(std::string OriginalKey, First arg, const Strings&... rest)
{
const std::string from = "%s";
std::string the_string = FindOriginal(OriginalKey);
int i = 0;
size_t start_pos = the_string.find(from);
while(start_pos != std::string::npos)
{
// Ideally here I can somehow get the parameter at index i...
the_string.replace(start_pos, from.length(), rest[i]);
start_pos = the_string.find(from, start_pos);
i++;
}
Replace(rest...);
return the_string;
}
int main()
{
std::cout << Replace("Toon", "5", "Jimmy") << std::endl;
}
Aucun commentaire:
Enregistrer un commentaire