【问题标题】:Reversing the contents of an std::list反转 std::list 的内容
【发布时间】:2011-08-04 10:53:59
【问题描述】:
class Print 
   {  
   public:
       void PrintAll() {}
   private:
    std::list<int> mylist;
   };

我从一本 C++ 语言书籍中看到了这个示例问题。 我想打印内部 mylist 元素。 如果mylist需要反转怎么办,使用C++ STL库,使用输出结果。

非常感谢!

【问题讨论】:

  • 请添加 PrintAll( ) 的代码

标签: c++ list stl iostream reverse


【解决方案1】:

std::list&lt;&gt;::reverse()?

也就是说,如果您只需要反向打印 list,您可以使用list 的反向迭代器(由std::list&lt;&gt;::rbegin()std::list&lt;&gt;::rend() 获得)简单地打印它而不是使用list 的普通迭代器。例如:

// given std::list<int> l;
for (std::list<int>::const_reverse_iterator iter(l.rbegin()), iter_end(l.rend());
        iter != iter_end;
        ++iter)
{
    std::cout << *iter << '\n';
}

【讨论】:

  • std::copy( l.rbegin(), l.rend(), std::ostream_iterator&lt;int&gt;( std::cout, "\n" ) );
【解决方案2】:

您可以使用列表中的reverse() 方法。

mylist.reverse();

将反转列表的内容,然后您可以使用迭代器打印相同的内容。

list<int>::iterator it;
cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
    cout << " " << *it;

您可以将所有功能包装在您自己的成员函数中。

【讨论】:

    【解决方案3】:

    您还可以使用容器提供的反向迭代器,例如l.rbegin() 和 l.rend() 将向后遍历列表。

    【讨论】:

      【解决方案4】:

      list::reverse的代码示例

      // list_reverse.cpp
      // compile with: /EHsc
      #include <list>
      #include <iostream>
      
      int main( ) 
      {
         using namespace std;
         list <int> c1;
         list <int>::iterator c1_Iter;
      
         c1.push_back( 10 );
         c1.push_back( 20 );
         c1.push_back( 30 );
      
         cout << "c1 =";
         for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
            cout << " " << *c1_Iter;
         cout << endl;
      
         c1.reverse( );
         cout << "Reversed c1 =";
         for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
            cout << " " << *c1_Iter;
         cout << endl;
      }
      

      输出将是

      c1 = 10 20 30 反转 c1 = 30 20 10

      【讨论】:

        【解决方案5】:

        iterator 只是指向列表中的当前元素。因此,如果我们编写一个从头到尾的 for 循环,我们可以反向打印列表。在下面给出的代码中:

            #include <iostream>
            #include <list>
        
            using namespace std;
        
            int main()
            {
               std::list<int> ii;
               ii.push_back(1);
               ii.push_back(2);
               ii.push_back(3);
               ii.push_back(4);
               ii.push_back(5);
               for (std::list<int>::iterator it = (ii.begin()); it != (ii.end()) ; ++it)
               {
                   cout << (*it) << " ";
               }
               cout << endl;
               for (std::list<int>::iterator it = (--ii.end()); it != (--ii.begin()) ; it--)
               {
                   cout << (*it) << " ";
               }
               return 0;
            }
        

        第一个 for 循环从前到后打印列表,而第二个循环从后到前打印。

        【讨论】:

          猜你喜欢
          • 2021-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-17
          • 1970-01-01
          • 1970-01-01
          • 2013-04-17
          • 1970-01-01
          相关资源
          最近更新 更多