正在格式化流空间
Formatting stream spaces
我一直在尝试使用.prushback来格式化我的字符串,以便它只在每个单词之间打印一个空格。
所以我尝试使用.push_back,但是它不适用于整数。
std::string FormatVehicleString(std::string year,
std::string make,
std::string model,
double price,
double mileage)
{
year.push_back(5);
make.push_back(5);
model.push_back(5);
price.push_back(5);
mileage.push_back(5);
}
有人能给我指一个正确的方向吗?有没有其他的值类型可以包含字符串和整数?
一个选项是使用std::ostringstream
。
std::string FormatCarInfo(std::string year,
std::string make,
std::string model,
double price,
double mileage)
{
std::ostingstream out;
out << year << " ";
out << make << " ";
out << model << " ";
out << price << " ";
out << mileag ;
return out.str();
}
另一种选择是使用std::to_string
。
std::string FormatCarInfo(std::string year,
std::string make,
std::string model,
double price,
double mileage)
{
return ( year + " " + make + " " + model + " " +
std::to_string(price) + " " + std::to_string(mileage) );
}
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- C++格式化输出问题
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- OpenGL相机和相机空间转型的困惑
- CUDA内核和数学函数的显式命名空间
- 打印第二列时的2d字符矢量打印空间
- 嵌套的匿名命名空间
- CMakeLists.txt中的命名空间表示法
- 格式化浮点值:返回默认值
- 类是C++中的命名空间吗
- 在命名空间中使用全局命名空间中的函数
- 使用 astyle 在一行上格式化C++中的嵌套命名空间
- 正在格式化流空间
- boost.log std::异常格式化程序找不到运算符<< 自己的命名空间中的重载