Chapter 11.顺序容器forward_list[c++11]

前向链表简介

前向链表是用单链表实现的,可在常量时间内在链表中做插入或删除操作
list比之forward_list,双向链表要消耗额外的空间存储每个元素和在插入和删除元素时一个轻微的更高的时间开销,所以forward_list更有效率,虽然只能向前遍历。
forward_list是唯一的标准容器中故意不给出size()成员函数的,这样是为了更高效而考虑,可以用distance(c.begin(),c.end())来得到forward_list的大小,这将消耗一个线性时间,而如果同list一样实现size()成员函数的话,那样要消耗一些额外的存储空间[用于链表中的内部计数得出size()]和使得插入和删除元素时有一个轻微的效率降低,实现size()要消耗一个常量的时间。

构造函数
//empty (default)
1.explicit forward_list ( const allocator_type& alloc = allocator_type() );
//copy
2.forward_list ( const forward_list& fwdlst );
  forward_list ( const forward_list& fwdlst, const allocator_type& alloc );
//move
3.forward_list ( forward_list&& fwdlst );
  forward_list ( forward_list&& fwdlst, const allocator_type& alloc );
//size
4.explicit forward_list ( size_type n );
//fill
5.explicit forward_list ( size_type n, const value_type& val, const allocator_type& alloc = allocator_type() );
//range
6.template < class InputIterator >
         forward_list ( InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type() );
//initializer list
7.forward_list ( initializer_list<value_type> il, const allocator_type& alloc = allocator_type() );
eg:
  // constructors used in the same order as described above:
  std::forward_list<int> first;                      // default: empty
  std::cout << "first:"; for (int& x: first) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> second (4);                 // fill: 4 "default-constructed" int's
  std::cout << "second:"; for (int& x: second) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> third (3,77);               // fill: 3 sevety-sevens
  std::cout << "third:"; for (int& x: third) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> fourth (third.begin(), third.end()); // range initialization
  std::cout << "fourth:"; for (int& x: fourth) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> fifth (fourth);             // copy constructor
  std::cout << "fifth:"; for (int& x: fifth) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> sixth (std::move(fifth));   // move ctor. (fifth wasted)
  std::cout << "sixth:"; for (int& x: sixth) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> seventh = {3, 52, 25, 90};  // initializer_list constructor
  std::cout << "seventh:"; for (int& x: seventh) std::cout << " " << x; std::cout << std::endl;
Possible output:
forward_list constructor examples:
first:
second 0 0 0 0
third: 77 77 77
fourth: 77 77 77
fifth: 77 77 77
sixth: 77 77 77
seventh: 3 52 25 90
operator=
函数原型:
1.forward_list& operator= ( const forward_list& fwdlst );//copy
2.forward_list& operator= ( forward_list&& fwdlst ); //move
3.forward_list& operator= ( initializer_list<value_type> il );//initializer list
eg:
#include <forward_list>
template<class Container>//一个自定义的类模板
Container by_two (const Container& x) {
  Container temp(x); for (auto& x:temp) x*=2; return temp;
}
int main ()
{
  std::forward_list<int> first (4);      // 4 ints
  std::forward_list<int> second (3,5);   // 3 ints with value 5
  first = second;                        // copy assignment
  second = by_two(first);                // move assignment
  std::cout << "first: ";
  for (int& x : first) std::cout << " " << x;
  std::cout << std::endl;
  std::cout << "second: ";
  for (int& x : second) std::cout << " " << x;
  std::cout << std::endl;
Output:
first: 5 5 5
second: 10 10 10
Iterators
before_beginReturn iterator to before beginning
beginReturn iterator to beginning
endReturn iterator to end
cbefore_beginReturn const_iterator to before beginning //just like before_begin
cbeginReturn const_iterator to beginning
cendReturn const_iterator to end 
eg:
//before_begin 不能被解引用,用于emplace_after, insert_after, erase_after or splice_after
  std::forward_list<int> mylist = {20, 30, 40, 50};
  mylist.insert_after ( mylist.before_begin(), 11 );
  std::cout << "mylist contains:";
  for ( int& x: mylist ) std::cout << " " << x;
Output:
mylist contains: 11 20 30 40 50
Capacity
emptyTest whether array is empty
max_sizemaximum size
Element access
frontAccess first element
Modifiers
assignAssign content
emplace_frontConstruct and insert element at beginning//construct with args
emplace_afterConstruct and insert element
insert_afterInsert elements
erase_afterErase elements
swapSwap content//algorithm exists swap, and the same behavior.
clearClear content
resizeChange size
push_frontInsert element at beginning→list、forward_list and deque unique
pop_frontDelete first element→list、forward_list and deque unique
//assign
//range
1.template <class InputIterator>
  void assign ( InputIterator first, InputIterator last );
//fill
2.void assign ( size_type n, const value_type& val );
//initializer list
3. void assign ( initializer_list<value_type> il );
eg:
  first.assign (4,15);                           // 15 15 15 15	fill
  second.assign (first.begin(),first.end());     // 15 15 15 15	range
  first.assign ( {77, 2, 16} );                  // 77 2 16	initializer_list
//emplace_front
1.template <class... Args>
void emplace_front (Args&&... args);
eg:
  std::forward_list< std::pair<int,char> > mylist;
  mylist.emplace_front(10,'a');
  mylist.emplace_front(20,'b');
  mylist.emplace_front(30,'c');
  std::cout << "mylist contains:";
  for (auto& x: mylist)
    std::cout << " (" << x.first << "," << x.second << ")";
Output:
mylist contains: (30,c) (20,b) (10,a)
//emplace_after
template <class... Args>
iterator emplace_after ( const_iterator position, Args&&... args );
eg:
  std::forward_list< std::pair<int,char> > mylist;
  auto it = mylist.before_begin();
  it = mylist.emplace_after ( it, 100, 'x' );
  it = mylist.emplace_after ( it, 200, 'y' );
  it = mylist.emplace_after ( it, 300, 'z' );
  std::cout << "mylist contains:";
  for (auto& x: mylist)
    std::cout << " (" << x.first << "," << x.second << ")";
Output:
mylist contains: (100,x) (200,y) (300,z)
//insert_after
1.iterator insert_after ( const_iterator position, const value_type& val );
2.iterator insert_after ( const_iterator position, value_type&& val );
3.iterator insert_after ( const_iterator position, size_type n, const value_type& val );
4.template <class InputIterator>
  iterator insert_after ( const_iterator position, InputIterator first, InputIterator last );
5.iterator insert_after ( const_iterator position, initializer_list<value_type> il );
eg:
  std::array<int,3> myarray = { 11, 22, 33 };
  std::forward_list<int> mylist;
  std::forward_list<int>::iterator it;
  it = mylist.insert_after ( mylist.before_begin(), 10 );          // 10
                                                                   //  ^  <- it
  it = mylist.insert_after ( it, 2, 20 );                          // 10 20 20
                                                                   //        ^
  it = mylist.insert_after ( it, myarray.begin(), myarray.end() ); // 10 20 20 11 22 33
                                                                   //                 ^
  it = mylist.begin();                                             //  ^
  it = mylist.insert_after ( it, {1,2,3} );                        // 10 1 2 3 20 20 11 22 33
                                                                   //        ^
  std::cout << "mylist contains:";
  for (int& x: mylist) std::cout << " " << x;
Output:
mylist contains: 10 1 2 3 20 20 11 22 33
//erase_after
1.iterator erase ( const_iterator position );
2.iterator erase ( const_iterator position, const_iterator last );
eg:
  std::forward_list<int> mylist = {10, 20, 30, 40, 50};
                                            // 10 20 30 40 50
  auto it = mylist.begin();                 // ^
  it = mylist.erase_after(it);              // 10 30 40 50
                                            //    ^
  it = mylist.erase_after(it,mylist.end()); // 10 30
                                            //       ^
  std::cout << "mylist contains:";
  for (int& x: mylist) std::cout << " " << x;
Output:
mylist contains: 10 30
Operations
splice_afterMove elements from another forward_list
removeRemove elements with specific value//algorithm exists one operating between two iterators.
remove_ifRemove elements fulfilling condition//same
uniqueRemove duplicate values//same
mergeMerge sorted lists
sortSort elements in container
reverseReverse the order of elements
//splice_aftermoves the elements in the range (first,last) to position in 3
1.
void splice_after ( const_iterator position, forward_list& fwdlst );//移动是fwdlst里的所有元素到position
void splice_after ( const_iterator position, forward_list&& fwdlst );
2.
void splice_after ( const_iterator position, forward_list& fwdlst, const_iterator i );//移动的是i的后一元素
void splice_after ( const_iterator position, forward_list&& fwdlst, const_iterator i );
3.
void splice_after ( const_iterator position, forward_list& fwdlst,//移动的是fwdlst里的(first,last)到position
                    const_iterator first, const_iterator last );
void splice_after ( const_iterator position, forward_list&& fwdlst,
                    const_iterator first, const_iterator last );
eg:
  std::forward_list<int> first = { 1, 2, 3 };
  std::forward_list<int> second = { 10, 20, 30  };
  auto it = first.begin();  // points to the 1
  first.splice_after ( first.before_begin(), second );
                          // first: 10 20 30 1 2 3
                          // second: (empty)
                          // "it" still points to the 1 (now first's 4th element)
  second.splice_after ( second.before_begin(), first, first.begin(), it);
                          // first: 10 2 3
                          // second: 20 30 1
  first.splice_after ( first.before_begin(), second, second.begin() );
                          // first: 30 10 2 3
                          // second: 20 1
                          // * notice that what is moved is AFTER the iterator
  std::cout << "first contains:";
  for (int& x: first) std::cout << " " << x;
  std::cout << std::endl;
  std::cout << "second contains:";
  for (int& x: second) std::cout << " " << x;
Output:
first contains: 30 10 2 3
second contains: 20 1
//merge
1.void merge ( forward_list& fwdlst );
  void merge ( forward_list&& fwdlst );
2.template <class Compare>
  void merge ( forward_list& fwdlst, Compare comp );
template <class Compare>
  void merge ( forward_list&& fwdlst, Compare comp );
eg:
  std::forward_list<double> first = {4.2, 2.9, 3.1};
  std::forward_list<double> second = {1.4, 7.7, 3.1};
  std::forward_list<double> third = {6.2, 3.7, 7.1};
  first.sort();
  second.sort();
  first.merge(second);
  std::cout << "first contains:";
  for (double& x: first) std::cout << " " << x;
  std::cout << std::endl;
  first.sort (std::greater<double>());
  third.sort (std::greater<double>());
  first.merge (third, std::greater<double>());
  std::cout << "first contains:";
  for (double& x: first) std::cout << " " << x;
Output:
first contains: 1.4 2.9 3.1 3.1 4.2 7.7
first contains: 7.7 7.1 6.2 4.2 3.7 3.1 3.1 2.9 1.4
Observers
get_allocator
Global functions
operators (forward_list)Global relational operator functions for forward_list
swap (forward_list)Exchanges the contents of two forward_list containers//没有实际的移动,只是交换数据的引用
//operators(forward_list) just like array container
1.template <class T, class Alloc>
  bool operator== ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
2.template <class T, class Alloc>
  bool operator!= ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
3.template <class T, class Alloc>
  bool operator< ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
4.template <class T, class Alloc>
  bool operator> ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
5.template <class T, class Alloc>
  bool operator>= ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
6.template <class T, class Alloc>
  bool operator<= ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
//swap
template <class T, class Alloc>
  void swap ( forward_list<T,Alloc>& lhs,
              forward_list<T,Alloc>& rhs );
eg:
  std::forward_list<int> first = {10, 20, 30};
  std::forward_list<int> second = {100, 200};
  std::forward_list<int>::iterator it;
  swap(first,second);

帮我把一下代码设置一个合理请求头,并加入一个延时import requests import os from bs4 import BeautifulSoup class NovelDownloader: def __init__(self, root_url): self.root_url = root_url self.book_list = [] self.chapter_list = [] def get_url(self, url): while True: try: res = requests.get(url) if res.status_code == 200: print("页面获取成功!") return res.text else: print("页面返回异常!", res.status_code) except: print("页面获取错误!") def get_book_list(self): res = self.get_url(self.root_url) html = BeautifulSoup(res, "html.parser") a_list = html.find_all("a", {"class": "name"}) for a in a_list: self.book_list.append(a["href"]) self.book_list = [self.root_url + i for i in self.book_list] self.book_list.remove('http://www.biquge5200.cc/') def get_chapter_list(self, url): res = self.get_url(url) html = BeautifulSoup(res, "html.parser") a_list = html.find_all("a", {"class": "chapter"}) for a in a_list: self.chapter_list.append((a["href"], a.text.replace("\n", ""))) def get_content(self, chapter): url = self.root_url + chapter[0] print(url) book_name = chapter[0].split("/")[1] print(book_name) if not os.path.exists(book_name): os.mkdir(book_name) res = self.get_url(url) html = BeautifulSoup(res, "html.parser") content = html.find("div", {"id": "content"}).text print(content) path = os.path.join(book_name, chapter[1]) with open(path, "w", encoding="utf8") as f: f.write(content) def main(self): self.get_book_list() for book in self.book_list: self.get_chapter_list(book) for chapter in self.chapter_list: self.get_content(chapter) if __name__ == '__main__': root_url = "http://www.biquge5200.cc/" nd = NovelDownloader(root_url) nd.main()
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值