Reputation: 12259
I want to add a simple prefix to a list of strings:
Input: "a, b, c, d, e"
Output: ":a, :b, :c, :d, :e"
Is there any boost library already implemented for that kind of operations? adding prefixes or even a apply a boost::format
operation to a sequence of strings?
I have searched on the boost::algorithm::string
library but found nothing. Of course, I could solve it simply with std::for_each
or any other mean, but I want to know if there's anything already made.
Upvotes: 1
Views: 1075
Reputation: 14119
So I will assume that you have a container with strings in it and each of those string should be prefixed: Maybe like this:
std::for_each(v.begin(), v.end(), [](auto& s){ s.insert(0, ":");});
or if c++11 is no option like this:
string& (string::*FP)(size_t pos, const string& str) = &string::insert;
std::for_each(v.begin(), v.end(), std::bind(FP, std::placeholders::_1, 0, ":"));
This will prefix each element in your container with a :
.
A full example that will at least produce the output in your example would be like this:
int main() {
std::vector<std::string> v;
v.push_back("a");
v.push_back("b");
v.push_back("c");
v.push_back("d");
v.push_back("e");
std::for_each(v.begin(), v.end(), [](auto& s){ s.insert(0, ":");});
for(const auto& s : v)
{
std::cout << s << ", ";
}
return 0;
}
Upvotes: 2