簡體   English   中英

如何打印類型向量<tuple<string, int, int> &gt; 篩選 C++?

[英]How to print a type vector<tuple<string, int, int>> to screen c++?

假設我有

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

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

只需迭代vector ,使用cout打印每個tuple值:

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

您需要將問題分解為兩個步驟。 首先考慮如何只打印元組,然后再考慮如何打印向量。 這是我的做法:

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

此方法覆蓋元組和向量的運算符<<。 打印向量將遍歷向量,為每個元組調用 operator<<。

輸出將是:

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

用於漂亮打印任何項目(包括元組)的任何數組,類似這樣的東西。

注意:這個程序是用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM