How to print out the contents of a vector?

后端 未结 19 1474
旧时难觅i
旧时难觅i 2020-11-22 03:46

I want to print out the contents of a vector in C++, here is what I have:

#include 
#include 
#include 
#include         


        
19条回答
  •  一整个雨季
    2020-11-22 04:20

    template collection:

    apply std::cout << and std::to_string

    to std::vector, std::array and std::tuple

    As printing a vector in cpp turned out to be surprisingly much work (at least compared to how basic this task is) and as one steps over the same problem again, when working with other container, here a more general solution ...

    Template collection content

    This template collection handles 3 container types: std::vector, std::array and std::tuple. It defines std::to_string() for those and makes it possible to directly print them out by std::cout << container;.

    Further it defines the << operator for std::string << container. With this it gets possible to construct strings containig these container types in a compact way.

    From

    std::string s1 = "s1: " + std::to_string(arr) + "; " + std::to_string(vec) + "; " + std::to_string(tup);
    

    we get to

    std::string s2 = STR() << "s2: " << arr << "; " << vec << "; " << tup;
    

    Code

    You can test this code interactively: here.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    namespace std
    {   
        // declations: needed for std::to_string(std::vector>)
        std::string to_string(std::string str);
        std::string to_string(const char *str);
        template
        std::string to_string(std::array const& arr);
        template
        std::string to_string(std::vector const& vec);
        template
        std::string to_string(const std::tuple& tup);
        
        std::string to_string(std::string str)
        {
            return std::string(str);
        }
        std::string to_string(const char *str)
        {
            return std::string(str);
        }
    
        template
        std::string to_string(std::array const& arr)
        {
            std::string s="{";
            for (std::size_t t = 0; t != N; ++t)
                s += std::to_string(arr[t]) + (t+1 < N ? ", ":"");
            return s + "}";
        }
    
        template
        std::string to_string(std::vector const& vec)
        {
            std::string s="[";
            for (std::size_t t = 0; t != vec.size(); ++t)
                s += std::to_string(vec[t]) + (t+1 < vec.size() ? ", ":"");
            return s + "]";
        }
        
        // to_string(tuple)
        // https://en.cppreference.com/w/cpp/utility/tuple/operator%3D
        template
        struct TupleString
        {
            static std::string str(const Tuple& tup)
            {
                std::string out;
                out += TupleString::str(tup);
                out += ", ";
                out += std::to_string(std::get(tup));
                return out;
            }
        };
        template
        struct TupleString
        {
            static std::string str(const Tuple& tup)
            {
                std::string out;
                out += std::to_string(std::get<0>(tup));
                return out;
            }
        };
        template
        std::string to_string(const std::tuple& tup)
        {
            std::string out = "(";
            out += TupleString::str(tup);
            out += ")";
            return out;
        }
    } // namespace std
    
    
    /**
     * cout: cout << continer
     */
    template  // cout << array
    std::ostream& operator <<(std::ostream &out, std::array &con)
    {
        out <<  std::to_string(con);
        return out;
    }
    template  // cout << vector
    std::ostream& operator <<(std::ostream &out, std::vector &con)
    {
        out <<  std::to_string(con);
        return out;
    }
    template // cout << tuple
    std::ostream& operator <<(std::ostream &out, std::tuple &con)
    {
        out <<  std::to_string(con);
        return out;
    }
    
    /**
     * Concatenate: string << continer
     */
    template 
    std::string operator <<(std::string str, C &con)
    {
        std::string out = str;
        out += std::to_string(con);
        return out;
    }
    #define STR() std::string("")
    
    int main()
    {
        std::array arr {1, 2, 3};
        std::string sArr = std::to_string(arr);
        std::cout << "std::array" << std::endl;
        std::cout << "\ttest to_string: " << sArr << std::endl;
        std::cout << "\ttest cout <<: " << arr << std::endl;
        std::cout << "\ttest string <<: " << (std::string() << arr) << std::endl;
        
        std::vector vec {"a", "b"};
        std::string sVec = std::to_string(vec);
        std::cout << "std::vector" << std::endl;
        std::cout << "\ttest to_string: " << sVec << std::endl;
        std::cout << "\ttest cout <<: " << vec << std::endl;
        std::cout << "\ttest string <<: " << (std::string() << vec) << std::endl;
        
        std::tuple tup = std::make_tuple(5, "five");
        std::string sTup = std::to_string(tup);
        std::cout << "std::tuple" << std::endl;
        std::cout << "\ttest to_string: " << sTup << std::endl;
        std::cout << "\ttest cout <<: " << tup << std::endl;
        std::cout << "\ttest string <<: " << (std::string() << tup) << std::endl;
        
        std::vector> vt {std::make_tuple(1, .1), std::make_tuple(2, .2)};
        std::string sVt = std::to_string(vt);
        std::cout << "std::vector" << std::endl;
        std::cout << "\ttest to_string: " << sVt << std::endl;
        std::cout << "\ttest cout <<: " << vt << std::endl;
        std::cout << "\ttest string <<: " << (std::string() << vt) << std::endl;
        
        std::cout << std::endl;
        
        std::string s1 = "s1: " + std::to_string(arr) + "; " + std::to_string(vec) + "; " + std::to_string(tup);
        std::cout << s1 << std::endl;
        
        std::string s2 = STR() << "s2: " << arr << "; " << vec << "; " << tup;
        std::cout << s2 << std::endl;
    
        return 0;
    }
    

    Output

    std::array
        test to_string: {1, 2, 3}
        test cout <<: {1, 2, 3}
        test string <<: {1, 2, 3}
    std::vector
        test to_string: [a, b]
        test cout <<: [a, b]
        test string <<: [a, b]
    std::tuple
        test to_string: (5, five)
        test cout <<: (5, five)
        test string <<: (5, five)
    std::vector
        test to_string: [(1, 0.100000), (2, 0.200000)]
        test cout <<: [(1, 0.100000), (2, 0.200000)]
        test string <<: [(1, 0.100000), (2, 0.200000)]
    
    s1: {1, 2, 3}; [a, b]; (5, five)
    s2: {1, 2, 3}; [a, b]; (5, five)
    

提交回复
热议问题