反转 std::list 的内容

发布于 2024-10-31 22:48:49 字数 232 浏览 2 评论 0原文

class Print 
   {  
   public:
       void PrintAll() {}
   private:
    std::list<int> mylist;
   };

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

非常感谢你!

class Print 
   {  
   public:
       void PrintAll() {}
   private:
    std::list<int> mylist;
   };

I see this example question from a C++ language book.
And I want to print the internal mylist elements.
How can it be done if mylist needs to be reversed, using C++ STL library and using to output the result.

Thanks you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

意中人 2024-11-07 22:48:49

std::list<>::reverse()

也就是说,如果您只需要反向打印 list,则可以使用 list 的反向迭代器(通过 < a href="http://en.cppreference.com/w/cpp/container/list/rbegin" rel="nofollow">std::list>>::rbegin()< /a> 和 std::list>>::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::list<>::reverse()?

That said, if you only need to print the list in reverse, you can simply print it using list's reverse iterators (obtained by std::list<>::rbegin() and std::list<>::rend()) rather than by using list's normal iterators. E.g.:

// 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';
}
漫雪独思 2024-11-07 22:48:49

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

mylist.reverse();

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

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

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

You can use the reverse() method on your list.

mylist.reverse();

will reverse the contents of your list and then you can print the same, using iterators.

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

You can wrap all the functionality up in your own member function.

旧竹 2024-11-07 22:48:49

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

You can also use reverse iterators provided by the container e.g. l.rbegin() and l.rend() and that will iterate through the list backwards.

吻风 2024-11-07 22:48:49

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

Code example for 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;
}

And the output will be

c1 = 10 20 30
Reversed c1 = 30 20 10

萌酱 2024-11-07 22:48:49

迭代器只是指向列表中的当前元素。因此,如果我们编写一个从头到尾的 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 循环从前到后打印列表,而第二个 for 循环从后到前打印。

iterator simply point to the current element in the list. So, if we write a for loop going from end to beginning, we can print the list in reverse. in the code given below:

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

The first for loop prints out the list from front to back while the second one prints from back to front.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文