singly linked list in the C++ standard library or other widely-used libraries?

假装没事ソ 提交于 2019-12-13 02:37:37

问题


Seems that there is only doubly linked list (but no singly linked list) in the C++ standard library, right? Is there any widely-used C++ libraries with singly linked list?


回答1:


There is slist, which is an SGI extension (__gnu_cxx::slist)

#include <iostream>
#include <iterator>
#include <ext/slist>

int main(int argc, char** argv) {
  __gnu_cxx::slist<int> sl;
  sl.push_front(1);
  sl.push_front(2);
  sl.push_front(0);
  std::copy(sl.begin(), sl.end(),  // The output is 0 2 1
            std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;
  return 0;
}



回答2:


There is the slist class from Boost that is a singly linked list implementation.




回答3:


Just for reference...

Time has passed and C++11 has brought us the std::forward_list container that is implemented as a singly-linked list and essentially does not have any overhead compared to its implementation in C.

Compared to std::list this container provides more space efficient storage when bidirectional iteration is not needed.

Warning: missing push_back method (std::forward_list and std::forward_list::push_back)



来源:https://stackoverflow.com/questions/2800064/singly-linked-list-in-the-c-standard-library-or-other-widely-used-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!