C++ 在C+中转换为字符串时限制浮点小数位数+;

C++ 在C+中转换为字符串时限制浮点小数位数+;,c++,vector,floating-point,C++,Vector,Floating Point,我正在制作一个函数,它打印std::vector的元素 工作代码: std::vector<float> components { 1, 2, 3 }; string result = "<"; for ( auto it = begin(this->components); it != end(this->components); ++it ) { result.append(to_string(*it)); if (it != (this-&

我正在制作一个函数,它打印
std::vector
的元素

工作代码:

std::vector<float> components { 1, 2, 3 };

string result = "<";

for ( auto it = begin(this->components); it != end(this->components); ++it ) {
    result.append(to_string(*it));
    if (it != (this->components))
        result.append(", ");
}

result.append(">");
std::cout << result;
std::向量分量{1,2,3};
字符串结果=”);
标准::cout

是否有一种方法可以控制字符串中的小数位数,而不必逐个字符手动检查?


作为旁注,我如何防止它在最后一个元素后添加一个
,“

您可以使用
std::stringstream.precision

只需创建一个
std::stringstream
将其转换为字符串,就可以了

像这样:

stringstream ss;
ss.precision(3);

ss << "<";

for ( auto it = begin(this->components); it != end(this->components); ++it ) {
    ss << *it;
    if (it != (this->components))
        ss << ", ";
}

ss << ">";

string result = ss.str();
stringstreamss;
精密度(3);
不锈钢组件);它结束(此->组件)++(it){
(不锈钢组件)
ss您可以在施法之前使用
sprintf()

float a = 1.000000;
char aa[20];
sprintf(aa, "%1.3f", a);

以下是我运行的完整代码:

#include <vector>
#include <iterator>
#include <iostream>

using namespace std;

int main()
{
    std::vector<float> components{ 1, 2, 3 };

    string result = "<";

    for (auto it = components.begin(); it != components.end(); ++it) {
        float a = *it;
        char aa[20];
        sprintf(aa, "%1.3f", a);
        result.append(string(aa));
        if (it+1 != components.end())
            result.append(", ");
    }

    result.append(">");
    std::cout << result.c_str();
    getchar();
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
std::向量分量{1,2,3};
字符串结果=”);

std::cout我会使用

#包括
#包括
#包括
#包括
int main()
{
向量分量{1,2,3,1.5f,2.5f,3.5f,1.25f,2.25f,3.25f,1.12345f};
std::stringstream结果;

结果正如@Axalo已经指出的,您可以将
setprecision
ostream
一起使用来设置其精度(并且它可以与任何
ostream
一起使用,而不仅仅是
cout

为了消除后面的逗号,我可能会使用我在别处发布的

使用该方法,代码可以编写如下内容:

#include <iostream>
#include <sstream>
#include <vector>
#include <iomanip>

#include "infix_iterator.h"

int main () {
    // Data that would display extra precision if we didn't stop it
    std::vector<float> components { 1.123f, 2.234f, 3.345f };

    std::ostringstream buff("<", std::ios::app);
    buff << std::setprecision(2);

    std::copy(components.begin(), components.end(), 
              infix_ostream_iterator<float>(buff, ", "));

    buff << ">";

    std::cout << buff.str();
}
#包括
#包括
#包括
#包括
#包括“中缀迭代器.h”
int main(){
//如果我们不阻止它,数据会显示出更高的精度
std::向量分量{1.123f,2.234f,3.345f};
标准::ostringstream buff(“”;

std::cout使用cout时,这不仅仅是为了输出?@jaska:
cout
并不特别。它只是一个碰巧进入控制台的输出流。其他输出流(到文件或内存)采用相同的格式。唯一相关的区别是刷新,这对stringstream没有意义。不过,刷新
cout
与刷新
std::ofstream
没有什么不同。两者都表示“立即写入,而不是稍后”。sprintf()中的第一个参数不应该是成为一个char*?我建议改用as
sprintf
永远不会真正安全,而且它很容易意外地溢出缓冲区。
#include <iostream>
#include <sstream>
#include <vector>
#include <iomanip>

#include "infix_iterator.h"

int main () {
    // Data that would display extra precision if we didn't stop it
    std::vector<float> components { 1.123f, 2.234f, 3.345f };

    std::ostringstream buff("<", std::ios::app);
    buff << std::setprecision(2);

    std::copy(components.begin(), components.end(), 
              infix_ostream_iterator<float>(buff, ", "));

    buff << ">";

    std::cout << buff.str();
}