在STL列表中输出元组

时间:2013-10-14 17:50:07

标签: c++ c++11 stl

void output_list_contents(std::list<tuple<string, int, double,int>> &my_list)
{
    for(std::list<tuple<string, int, double,int> >::iterator it =my_list.begin(); it!= my_list.end(); ++it)
    {

    }
}

我正在尝试输出存储在STL列表中的所有元组的信息。我不知道语法,我花了几个小时用谷歌搜索答案,但遗憾的是我没有遇到任何问题。我正在努力学习语法和逻辑来访问存储在其中的元组。

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

类似的东西:

void output_list_contents(std::list<tuple<string, int, double,int>> &my_list)
{
    for(const auto& e : my_list)
    {
        std::cout << std::get<0>(e) << " " << std::get<1>(e) << " "
                  << std::get<2>(e) << " " << std::get<3>(e) << std::endl;
    }
}

答案 1 :(得分:2)

operator<<的第一次重载tuple<string, int, double,int>

std::ostream& opertaor<<(std::ostream& out, 
                              tuple<string,int,double,int> const & t)
{
     return out << "{" << std::get<0>(t) 
                << "," << std::get<1>(t) 
                << "," << std::get<2>(t) 
                << "," << std::get<3>(t) << "}"; 
}

然后在循环中使用它:

for(std::list<tuple<string, int, double,int> >::iterator it =my_list.begin(); 
                       it!= my_list.end(); ++it)
{
     std::cout << *it << std::endl;
}

哦,这太丑了。更好地使用基于范围的for循环和auto

for(auto const & item : my_list)
      std::cout << item << std::endl;

希望有所帮助。


operator<< std::tuple的广义实现将是:

namespace detail
{
      template<int ... N> 
      struct seq 
      { 
         using type = seq<N...>; 
         template<int I>
         struct push_back : seq<N..., I> {};
      };

      template<int N> 
      struct genseq : genseq<N-1>::type::template push_back<N-1> {};

      template<> 
      struct genseq<0> : seq<> {};

      template<typename ... Types, int ...N>
      void print(std::ostream & out, std::tuple<Types...> const & t, seq<N...>)
      {
         const auto max = sizeof...(N);
         auto sink = {
                      (out << "{", 0),
                      (out << (N?",":"") << std::get<N>(t) , 0)...,
                      (out << "}", 0)
                     };
      }
}
template<typename ... Types>
std::ostream& operator<<(std::ostream & out, std::tuple<Types...> const & t)
{
   detail::print(out, t, typename detail::genseq<sizeof...(Types)>::type());
   return out;
}

此广义operator<<应该能够使用任意数量的模板参数打印std::tuple,只要所有模板参数依次支持operator<<

测试代码:

int main()
{
    std::cout << std::make_tuple(10, 20.0, std::string("Nawaz")) << std::endl;
    std::cout << std::make_tuple(10, 20.0, std::string("Nawaz"), 9089) << std::endl;
}

输出:

{10,20,Nawaz}
{10,20,Nawaz,9089}

Online Demo: - )

答案 2 :(得分:0)

void output_list_contents(std::list<std::tuple<std::string, int, double, int>>& my_list)
{
    for (auto tup : my_list)
    {
        print_tuple(tup);
    }
}

这就是print_tuple的样子:

template <typename... Ts, int... Is>
void print_tuple(std::tuple<Ts...>& tup, std::index_sequence<Is...>)
{
    auto l = { ((std::cout << std::get<Is>(tup)), 0)... };
}

template <typename... Ts>
void print_tuple(std::tuple<Ts...>& tup)
{
    print_tuple(tup, std::index_sequence_for<Ts...>{});
}