如何打印类型向量<tuple <string,int,=“”int =“”>&gt;屏幕c ++?

时间:2015-05-06 19:54:16

标签: c++ vector tuples std

我们说我有

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));

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

3 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:2)

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

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;
}

此方法会覆盖运算符&lt;&lt;对于元组和向量。打印向量将遍历调用操作符的向量&lt;&lt;对于每个元组。

输出将是:

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

答案 2 :(得分:1)

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

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

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

示例输出:

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

完全可编辑的例子:

#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;
}