I\'m using gcc 4.6. Assume that there is a vector v of parameters I have to pass to a variadic function f(const char* format, ...).
One approach of doing this is:
Judging by your own answer given, it sounds like you could make use of boost format.
Examples:
#include
#include
#include
#include
using namespace std;
using namespace boost;
template
string formatted_str_from_vec(const T& v) {
ostringstream fs;
size_t count = 1;
for (const auto& i : v) {
if (&i != &v[0]) {
fs << " ";
}
fs << '%' << count << '%';
++count;
}
format fmtr(fs.str());
for (const auto& i : v) {
fmtr % i;
}
// looks like fmtr("%1% %2% %3% %4%") % v[0] % v[1] etc.
return fmtr.str();
}
int main() {
cout << formatted_str_from_vec(vector({1, 2, 3, 4, 5, 6, 7, 8, 8, 10, 11, 12})) << endl;
cout << formatted_str_from_vec(vector({"a", "b", "c"})) << endl;
format test1("%1% %2% %3%");
test1 % 1 % "2" % '3';
cout << test1.str() << endl;
format test2("%i %s %c");
test2 % 1 % "2" % '3';
cout << test2.str() << endl;
format test3("%1% %2%");
test3.exceptions(io::no_error_bits);
test3 % 'g';
cout << test3.str() << endl;
format test4("%%1%% = %1%");
test4 % "zipzambam";
cout << test4.str() << endl;
}
// g++ -Wall -Wextra printvector.cc -o printvector -O3 -s -std=c++0x
Of course, none of that's necessary to just print out a vector.