如何在C++中将值连接到其键
how concat a Value to its key in C++
我不知道如何写下我的问题,但我会尽力的。
说我使用C++语言:
我想得到下一个结果:
#include<iostream>
std::string line = "";
int lemmas = 1;
int main() {
line = "<b>Lemma %d. <b>n"; // i want line = <b>Lemma 1. <b>n
return 1;
}
如何添加它的值(在我的例子中是lemma)?
正如我的评论中所提到的,std::ostringstream
类可以用来构造您想要的字符串:
#include <iostream>
#include <sstream>
std::string line = "";
int lemmas = 1;
int main() {
std::ostringstream oss;
oss << "<b>Lemma " << lemmas << ". <b>n"; // i want line = <b>Lemma 1. <b>nnn
line = oss.str();
return 1;
}
C++输出不像C printf()
函数族那样使用格式化子句(例如%d
)。
如果使用C++11,可以使用std::to_string
将lemmas
转换为字符串。
#include<string>
std::string line = "";
int lemmas = 1;
int main() {
line = "<b>Lemma " + std::to_string(lemmas) + ". <b>n";
return 1;
}
或者,如果Boost可用,你可以做一些类似于原始代码的事情:
#include <string>
#include <boost/format.hpp>
std::string line = "";
int lemmas = 1;
int main() {
line = (boost::format("<b>Lemma %1%. <b>n") % lemmas).str();
return 1;
}
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- 在 libcurl 连接池中预创建连接
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- libcurl 和 DNS ttl 中的内部连接管理
- 如何应用 libcurl 的持久连接选项
- 提升 ASIO - io_service 不要等待连接到线程
- WinSock2:connect() 提供"连接被拒绝"
- 升压信号2将插槽传递到成员功能以断开连接