1.list是可以在常数范围内可以在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2.list的底层是双向链表结构,双向链表中的每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
3.list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,比起更加简单高效。
4.与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入,移除元素的执行效率更好。
5.与其他的序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)。
list lt1;
list lt2(4, 100);
list lt3(lt2.begin(), lt2.end());
list lt4(lt3);
std::list::operator=
copy (1)
list& operator= (const list& x);
分配内容
list lt1; list lt2(4, 100); lt1 = lt2;
std::list::begin
iterator begin();
const_iterator begin() const;
返回指向开始的迭代器
list lt(4, 100); list::iterator it = lt.begin(); while (it != lt.end()) { cout << *it << " "; ++it; } cout << endl;
list lt(4, 100); list::iterator it = lt.end(); while (it != lt.begin()) { cout << *it << " "; --it; } cout << endl;
【注意】
1.begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动。
2.rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动。
list lt(2, 5); cout << lt.empty() << endl;
list lt(2, 5); cout << lt.size() << endl;
list lt; cout << lt.max_size() << endl;
list lt(5, 20); cout << lt.front() << endl;
list lt(5, 20); cout << lt.back() << endl;
list lt1; list lt2; lt1.assign(4, 12); lt2.assign(lt1.begin(), lt1.end());
list lt1; lt1.assign(4, 12); lt1.push_front(1); for (auto t : lt1) { cout << t << " "; } cout << endl;
list lt1; lt1.assign(4, 12); lt1.pop_back(); for (auto t : lt1) { cout << t << " "; } cout << endl;
list lt1; lt1.assign(4, 12); lt1.push_back(1); for (auto t : lt1) { cout << t << " "; } cout << endl;
list lt1; lt1.assign(4, 12); lt1.pop_front(); for (auto t : lt1) { cout << t << " "; } cout << endl;
list lt1; lt1.push_back(1); lt1.push_back(2); lt1.push_back(3); lt1.push_back(4); list::iterator it = lt1.begin(); ++it; lt1.insert(it, 19);
list lt1; lt1.push_back(1); lt1.push_back(2); lt1.push_back(3); lt1.push_back(4); list::iterator it = lt1.begin(); ++it; lt1.erase(it);
list lt1(3, 5); list lt2(4, 2); lt1.swap(lt2)
list lt1(3, 5); lt1.resize(10, 1);
list lt1(3, 5); lt1.clear();
list lt1; lt1.push_back(1); lt1.push_back(2); lt1.push_back(3); lt1.push_back(4); lt1.reverse(); for (auto t : lt1) { cout << t << " "; } cout << endl;
list lt1; lt1.push_back(2); lt1.push_back(3); lt1.push_back(1); lt1.push_back(4); lt1.sort(); for (auto t : lt1) { cout << t << " "; } cout << endl;
上一篇:i5 750最高配什么显卡
下一篇:i7 970配什么主板好