我真的很喜欢c ++中sprintf给出的答案?但它仍然不是我想要的.
我想用占位符创建一个常量字符串,比如
const std::string LOGIN_URL = "%s://%s/chronicle/spring/api/logout";
Run Code Online (Sandbox Code Playgroud)
然后使用可替换参数构建字符串,如:
sprintf(url, LOGIN_URL, protocol, server); //coding sprintf from long ago memory, please ignore
Run Code Online (Sandbox Code Playgroud)
但是如果我能帮忙的话,我真的想远离C弦.
stringbuilder()方法要求我把我的常量字符串组合起来并在我想要使用它们时组装它们.这是一个很好的方法,但我想要的是更整洁.
Fle*_*exo 11
Boost格式库听起来像您正在寻找的,例如:
#include <iostream>
#include <boost/format.hpp>
int main() {
int x = 5;
boost::format formatter("%+5d");
std::cout << formatter % x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
或者您的具体示例:
#include <iostream>
#include <string>
#include <boost/format.hpp>
int main() {
const std::string LOGIN_URL = "%s://%s/chronicle/spring/api/logout";
const std::string protocol = "http";
const std::string server = "localhost";
boost::format formatter(LOGIN_URL);
std::string url = str(formatter % protocol % server);
std::cout << url << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1347 次 |
最近记录: |