格式化流空间

时间:2015-11-16 03:35:14

标签: c++

我一直在尝试使用.pushback格式化我的字符串,以便在每个单词之间只打印一个空格。

所以我试图使用.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);

}

有人能指出我正确的方向吗,是否有另一种值类型可以包含字符串和整数?

1 个答案:

答案 0 :(得分:1)

一种选择是使用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) );
}