0
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 列表中的所有元组中输出信息。我不知道语法,过去一个小时我一直在谷歌上搜索答案,但遗憾的是我没有遇到任何问题。我正在努力使用语法和逻辑来访问存储在其中的元组。

有人可以帮我吗?

4

3 回答 3

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;
    }
}
于 2013-10-14T17:57:12.790 回答
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<<for的一般实现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::tupleoperator<<

测试代码:

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}

在线演示:-)

于 2013-10-14T17:58:01.810 回答
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...>{});
}
于 2013-10-14T18:02:40.897 回答