Can boost:algorithm::join() concat a container of floats?

后端 未结 2 1369
花落未央
花落未央 2020-12-31 05:08

Boost join can be used to concatenate a container of strings optionally separated by a separator string as shown in this example: A good example for boost::algorithm::join

2条回答
  •  旧巷少年郎
    2020-12-31 05:36

    Sure, you can combine boost::algorithm::join and boost::adaptors::transformed to convert the doubles to strings and then join them together.

    #include 
    #include 
    #include 
    
    #include 
    #include 
    
    int main()
    {
        using boost::adaptors::transformed;
        using boost::algorithm::join;
    
        std::vector v{1.1, 2.2, 3.3, 4.4};
    
        std::cout 
          << join( v | 
                   transformed( static_cast(std::to_string) ), 
                   ", " );
    }
    

    Output:

    1.100000, 2.200000, 3.300000, 4.400000


    You can also use a lambda to avoid the ugly cast

    join(v | transformed([](double d) { return std::to_string(d); }), ", ")
    

提交回复
热议问题