C++ 输出STL列表中的元组 无效输出列表内容(标准::列表和我的列表) { for(std::list::iterator it=my_list.begin();it!=my_list.end();++it) { } }

C++ 输出STL列表中的元组 无效输出列表内容(标准::列表和我的列表) { for(std::list::iterator it=my_list.begin();it!=my_list.end();++it) { } },c++,c++11,stl,C++,C++11,Stl,我试图从STL列表中存储的所有元组中输出信息。我不知道语法,我花了一个小时在谷歌上搜索答案,但遗憾的是,我什么都没遇到。我正在努力使用语法和逻辑来访问存储在其中的元组 有人能帮我吗?比如: void output_list_contents(std::list<tuple<string, int, double,int>> &my_list) { for(std::list<tuple<string, int, double,int> &

我试图从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)
    {

    }
}
无效输出列表内容(标准::列表和我的列表)
{
用于(const auto&e:my_列表)
{
std::cout类似于:

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

    }
}
无效输出列表内容(标准::列表和我的列表)
{
用于(const auto&e:my_列表)
{


std::coutFirst-重载
operatorFirst-重载
operator不是
*它是存储在列表中的元组吗?它不是
*它是存储在列表中的元组吗?嗨,这太棒了,很有效,但我想了解它为什么有效。我希望能解释一下“auto&e”或者,如果您能提供一个有助于我理解的链接。@James它被称为a。
const auto&
仅根据容器包含的内容绑定元素,因此
auto
在您的示例中推断为元组。
const auto&
用于代替
auto
,以避免在迭代时复制元组
列表
。我了解auto是如何工作的,我也了解它是如何通过列表的,但是e的值是什么,这个值做什么?@James
e
(元素的缩写)只是
元组
。它就像传统循环的迭代器,只是迭代器在概念上是指向元素的指针(需要取消引用),而基于范围的循环提供了对元素本身的引用(使用
const auto&
)。
e
在每次执行循环时都是不同的,当然(就像使用
++it
的迭代器一样)。非常感谢。我现在明白了。:)您好,这真是太棒了,很有效,但我想了解它为什么有效。如果您能解释一下“auto&e”,我将不胜感激或者,如果您能提供一个有助于我理解的链接。@James它被称为a。
const auto&
仅根据容器包含的内容绑定元素,因此
auto
在您的示例中推断为元组。
const auto&
用于代替
auto
,以避免在迭代时复制元组
列表
。我了解auto是如何工作的,我也了解它是如何通过列表的,但是e的值是什么,这个值做什么?@James
e
(元素的缩写)只是
元组
。它就像传统循环的迭代器,只是迭代器在概念上是指向元素的指针(需要取消引用),而基于范围的循环提供了对元素本身的引用(使用
const auto&
)。
e
在每次执行循环时都是不同的,当然(就像使用
++it
的迭代器一样)。非常感谢。我现在明白了。:)谢谢你的输入,它给了我答案的另一个视图。@James:我没有给你任何输入。我只给了你输出。:p谢谢你的输入,它给了我答案的另一个视图。@James:我没有给你任何输入。我只给了你输出。:p谢谢你的帮助:)@James你有C++14支持吗?为什么
std::index_sequenceprint\u tuple
中的ode>?一定是打字错误。谢谢你的帮助:)@James你有C++14支持吗?为什么第二个
print\u tuple
中的
std::index\u sequence
是什么?一定是打字错误。
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 const & item : my_list)
      std::cout << item << std::endl;
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;
}
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}
void output_list_contents(std::list<std::tuple<std::string, int, double, int>>& my_list)
{
    for (auto tup : my_list)
    {
        print_tuple(tup);
    }
}
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...>{});
}