如何将类型向量>打印到屏幕c ++?

时间:2022-02-19 16:13:33

Let's say I have

让我说我有

std::vector<std::tuple<string ,int ,int>> tupleVector;
tupleVector.push_back(std::tuple<string ,int ,int>("Joe", 2, 3));
tupleVector.push_back(std::tuple<string ,int ,int>("Bob", 4, 5));

How can I iterate on the vector to print all values of this vector containing a tuple?

如何迭代向量以打印包含元组的此向量的所有值?

3 个解决方案

#1


Just iterate the vector and then print each tuple value

只需迭代向量,然后打印每个元组值

for ( const auto& i : tupleVector ) {
  cout << get<0>(i) << get<1>(i) << get<2>(i) << endl;
}

#2


You need to break the problem into two steps. First think about how to print just the tuple, then think about how to print the vector. Here is how I'd do it:

您需要将问题分解为两个步骤。首先考虑如何打印元组,然后考虑如何打印矢量。我是这样做的:

std::ostream& operator<<(std::ostream& s,
                         const std::tuple<std::string, int, int>& t) {
  s << "(" << std::get<0>(t) << "," << std::get<1>(t) << "," <<
      std::get<2>(t) << ")";
  return s;
}

std::ostream& operator<<(std::ostream& s,
                         const std::vector<std::tuple<
                         std::string, int, int> >& v) {
  s << "[";
  for (size_t idx = 0; idx < v.size(); idx++) {
    s << v[idx];
    if (idx < v.size() - 1) {
      s << ",";
    }
  }
  s << "]";
  return s;
}

int main() {
  std::vector<std::tuple<std::string, int, int> > v;
  v.emplace_back("hello", 3, 4);
  v.emplace_back("goodbye", 45, 67);

  std::cout << v << std::endl;

  return 0;
}

This method overrides the operator<< for the tuple and the vector. Printing the vector will loop through the vector calling the operator<< for each tuple.

此方法会覆盖运算符<< for the tuple和vector。打印向量将循环遍历向量调用操作符< <的每个元组。< p>

The output will be:

输出将是:

[(hello,3,4),(goodbye,45,67)]

#3


for pretty printing of any array of any item (including tuples), something like this.

用于漂亮打印任何项目(包括元组)的任何数组,就像这样。

Note: this program written in c++11. c++14 would make it easier to iterate the tuple without recursion.

注意:这个程序用c ++ 11编写。 c ++ 14可以更轻松地迭代元组而不需要递归。

example project here: http://goo.gl/9okLTB

这里的示例项目:http://goo.gl/9okLTB

example output:

Hello World                                                                                                                                                                             
{ hello, 1, 2 }                                                                                                                                                                         
{ { hello, 3, 4 }, { world, 5, 6 } }    

fully compilable example:

完全可编辑的例子:

#include <iostream>
#include <tuple>
#include <string>
#include <vector>


namespace detail {
    template<typename Stream>
    struct printer {
        printer(Stream& os)
        : _os ( os )
        {}

        ~printer() {
            _os << " }";
        }

        template<class X>
        void operator()(const X& x) {
            if (_first) {
                _os << "{ ";
                _first = false;
            }
            else {
                _os << ", ";
            }
            _os << x;
        }
    private:
        Stream& _os;
        bool _first = true;    
    };

    template<size_t index, size_t limit>
    struct print_loop
    {
        template<class Stream, class...Args>
        void operator()(detail::printer<Stream>&& print, const std::tuple<Args...>& tuple) const
        {
            print(std::get<index>(tuple));
            print_loop<index+1, limit>()(std::forward<detail::printer<Stream>>(print), tuple);
        }
    };

    template<size_t limit>
    struct print_loop<limit, limit>
    {
        template<class Stream, class...Args>
        void operator()(detail::printer<Stream>&& print, const std::tuple<Args...>& tuple) const
        {

        }    
    };
}

template<class Stream>
detail::printer<Stream> make_printer(Stream& os)
{
    return detail::printer<Stream>(os);
}




template<class Stream, class...Args>
void print_elements(detail::printer<Stream>&& printer, const std::tuple<Args...>& tuple)
{

    detail::print_loop<0, sizeof...(Args)>()(std::forward<detail::printer<Stream>>(printer), tuple);
}

template<class...Args>
void tuple_print(std::ostream& os, const std::tuple<Args...>& tuple)
{
    print_elements(make_printer(os), tuple);
}

template<class...Args>
inline std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& tuple)
{
    tuple_print(os, tuple);
    return os;
}

template<class T>
inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    auto print = make_printer(os);
    for(const auto& item : vec) {
        print(item);
    }
}

using namespace std;


int main()
{
   cout << "Hello World" << endl; 
   auto x = make_tuple(string { "hello" }, 1, 2);
   cout << x << endl;

   auto y = vector<tuple<string, int, int>> {
    make_tuple(string { "hello" }, 3, 4),
    make_tuple(string { "world" }, 5, 6)
   };
   cout << y << endl;

   return 0;
}

#1


Just iterate the vector and then print each tuple value

只需迭代向量,然后打印每个元组值

for ( const auto& i : tupleVector ) {
  cout << get<0>(i) << get<1>(i) << get<2>(i) << endl;
}

#2


You need to break the problem into two steps. First think about how to print just the tuple, then think about how to print the vector. Here is how I'd do it:

您需要将问题分解为两个步骤。首先考虑如何打印元组,然后考虑如何打印矢量。我是这样做的:

std::ostream& operator<<(std::ostream& s,
                         const std::tuple<std::string, int, int>& t) {
  s << "(" << std::get<0>(t) << "," << std::get<1>(t) << "," <<
      std::get<2>(t) << ")";
  return s;
}

std::ostream& operator<<(std::ostream& s,
                         const std::vector<std::tuple<
                         std::string, int, int> >& v) {
  s << "[";
  for (size_t idx = 0; idx < v.size(); idx++) {
    s << v[idx];
    if (idx < v.size() - 1) {
      s << ",";
    }
  }
  s << "]";
  return s;
}

int main() {
  std::vector<std::tuple<std::string, int, int> > v;
  v.emplace_back("hello", 3, 4);
  v.emplace_back("goodbye", 45, 67);

  std::cout << v << std::endl;

  return 0;
}

This method overrides the operator<< for the tuple and the vector. Printing the vector will loop through the vector calling the operator<< for each tuple.

此方法会覆盖运算符<< for the tuple和vector。打印向量将循环遍历向量调用操作符< <的每个元组。< p>

The output will be:

输出将是:

[(hello,3,4),(goodbye,45,67)]

#3


for pretty printing of any array of any item (including tuples), something like this.

用于漂亮打印任何项目(包括元组)的任何数组,就像这样。

Note: this program written in c++11. c++14 would make it easier to iterate the tuple without recursion.

注意:这个程序用c ++ 11编写。 c ++ 14可以更轻松地迭代元组而不需要递归。

example project here: http://goo.gl/9okLTB

这里的示例项目:http://goo.gl/9okLTB

example output:

Hello World                                                                                                                                                                             
{ hello, 1, 2 }                                                                                                                                                                         
{ { hello, 3, 4 }, { world, 5, 6 } }    

fully compilable example:

完全可编辑的例子:

#include <iostream>
#include <tuple>
#include <string>
#include <vector>


namespace detail {
    template<typename Stream>
    struct printer {
        printer(Stream& os)
        : _os ( os )
        {}

        ~printer() {
            _os << " }";
        }

        template<class X>
        void operator()(const X& x) {
            if (_first) {
                _os << "{ ";
                _first = false;
            }
            else {
                _os << ", ";
            }
            _os << x;
        }
    private:
        Stream& _os;
        bool _first = true;    
    };

    template<size_t index, size_t limit>
    struct print_loop
    {
        template<class Stream, class...Args>
        void operator()(detail::printer<Stream>&& print, const std::tuple<Args...>& tuple) const
        {
            print(std::get<index>(tuple));
            print_loop<index+1, limit>()(std::forward<detail::printer<Stream>>(print), tuple);
        }
    };

    template<size_t limit>
    struct print_loop<limit, limit>
    {
        template<class Stream, class...Args>
        void operator()(detail::printer<Stream>&& print, const std::tuple<Args...>& tuple) const
        {

        }    
    };
}

template<class Stream>
detail::printer<Stream> make_printer(Stream& os)
{
    return detail::printer<Stream>(os);
}




template<class Stream, class...Args>
void print_elements(detail::printer<Stream>&& printer, const std::tuple<Args...>& tuple)
{

    detail::print_loop<0, sizeof...(Args)>()(std::forward<detail::printer<Stream>>(printer), tuple);
}

template<class...Args>
void tuple_print(std::ostream& os, const std::tuple<Args...>& tuple)
{
    print_elements(make_printer(os), tuple);
}

template<class...Args>
inline std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& tuple)
{
    tuple_print(os, tuple);
    return os;
}

template<class T>
inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    auto print = make_printer(os);
    for(const auto& item : vec) {
        print(item);
    }
}

using namespace std;


int main()
{
   cout << "Hello World" << endl; 
   auto x = make_tuple(string { "hello" }, 1, 2);
   cout << x << endl;

   auto y = vector<tuple<string, int, int>> {
    make_tuple(string { "hello" }, 3, 4),
    make_tuple(string { "world" }, 5, 6)
   };
   cout << y << endl;

   return 0;
}