【发布时间】:2011-02-22 09:43:31
【问题描述】:
是否可以格式化std::string 传递一组参数?
目前我正在以这种方式格式化字符串:
string helloString = "Hello %s and %s";
vector<string> tokens; //initialized vector of strings
const char* helloStringArr = helloString.c_str();
char output[1000];
sprintf_s(output, 1000, helloStringArr, tokens.at(0).c_str(), tokens.at(1).c_str());
但是向量的大小是在运行时确定的。是否有与sprintf_s 类似的函数,它接受参数集合并格式化std::string/char*?我的开发环境是 MS Visual C++ 2010 Express。
编辑: 我想实现类似的目标:
sprintf_s(output, 1000, helloStringArr, tokens);
【问题讨论】:
-
格式也是在运行时确定的吗?或者它是固定的?您只是想在输出中枚举
vector的内容吗?您可能想查看“加入”:boost.org/doc/libs/1_46_0/doc/html/string_algo/… -
格式也是在运行时确定的。有关更多详细信息,请参阅编辑部分。谢谢!
标签: c++ string-formatting stdstring variadic-functions