Skip to content

[cpp,c++] Using utx::print_all

• Include Utxcpp

#include <utxcpp/core.hpp>

• Print one container using utx::print_all:

std::vector<int> v1{2,3,5,7,11};
utx::print_all(v1); // 2 3 5 7 11

• Print many containers using utx::print_all:

        std::vector<int> v1{2,3,5,7,11};
        std::vector<float> v2{2.3, 3.4, 4.5, 5.6};
        std::list<int> l1{1,2,3};
        std::string str = "c++";
        std::array<int, 5> array{5,4,3,2,1};

        utx::print_all(v1, v2, l1, str, array);

Output:

2 3 5 7 11 
2.300000 3.400000 4.500000 5.600000 
1 2 3 
c + + 
5 4 3 2 1 

If you have many containers, using utx::print_all shows its benefit, which needs less code.

• c++ Example: Using std::transform transformer

#include <utxcpp/core.hpp>
#include <vector>
#include <algorithm> // std::transform, std::copy
#include <numeric> // std::iota
#include <complex> // std::sqrt, std::pow

int main()
{
        std::vector<float> v1(10);
        std::iota(v1.begin(), v1.end(), 1); // Init vector v1

        std::vector<float> v2(v1.size());
        std::vector<float> v3(v1.size());

        std::transform(
                v2.begin(),
                std::transform(
                        v1.begin(),
                        v1.end(),
                        v2.begin(),
                        [] (float & x) {return std::sqrt<float>(x).real();}
                ),
                v3.begin(),
                [] (float & x) {return std::pow<float>(x, 3);}
        );

        utx::print_all(v1, v2, v3);
}

Output:

1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 10.000000 
1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000 3.162278 
1.000000 2.828427 5.196152 8.000000 11.180341 14.696940 18.520258 22.627417 27.000000 31.622778 

 

 

 

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options

Submitted comments will be subject to moderation before being displayed.

@cppfx.xyz