首页 / C++ / c++ list使用方式
c++ list使用方式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c++ list使用方式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3094字,纯文字阅读大概需要5分钟。
内容图文
![c++ list使用方式](/upload/InfoBanner/zyjiaocheng/645/821c2b33b4b0432ca6609be726319995.jpg)
c++ list
定义和初始化
listlst1; // 创建空list
listlst2(6); //创建含有6个元素的list
listlst3(3, 2); // 创建含有三个元素的list
listlst4(lst2); // 使用ls2初始化ls4
listlst5(lst2.begin(), lst2.end()); // 同ls4
list常用操作函数
lst1.assign() // 给list赋值
lst1.front() // 返回第一个元素
lst1.back() // 返回最后一个元素
lst1.begin() // 返回指向第一个元素的迭代器
lst1.end() // 返回末尾的迭代器
lst1.insert() // 插入一个元素到list中
lst1.erase() // 删除一个元素
lst1.pop_back() // 删除最有一个元素
lst1.pop_front() // 删除第一个元素
lst1.clear() // 删除所有元素
lst1.remove(const T & val) // 删除和val相等的元素
lst1.push_back() // 在list的末尾添加一个元素
lst1.push_front() // 在list的首部添加一个元素
lst1.empty() // 判断,若list为空返回true
lst1.max_size() // 返回list能容纳的最大元素数量
lst1.sort() // 给list排序(顺序)
list.reverse() // 把list中的元素倒转
lst1.merge(lst2) // 合并lst2到lst1,并清空lst2
lst1.unique() // 删除所有和前一个元素相等的元素
// 在位置i前面插入链表x中的区间 [begin, end), 并在链表x中删除该区间(链表自身和链表x可以是用一个链表,只要i不在 [begin, end) 中即可
void splice(iterator i, list & x, iterator begin, iterator end)
遍历List
//迭代器法
for(list<int>::const_iteratoriter = lst1.begin();iter != lst1.end();iter++)
{
cout<<*iter;
}
cout<<endl;
案例:
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
class INT {
public:
INT(int x):n(x){}
friend bool operator < (const INT & int_l, const INT & int_2);
friend bool operator == (const INT & int_1, const INT & int_2);
friend ostream & operator << (ostream & out, const INT & int_x);
private:
int n;
};
bool operator < (const INT & int_1, const INT & int_2) {
return int_1.n < int_2.n;
}
bool operator == (const INT & int_1, const INT & int_2) {
return int_1.n == int_2.n;
}
ostream & operator << (ostream & out, const INT & int_x) {
out << int_x.n;
return out;
}
template <class T>
void Plist(T begin, T end) {
for(; begin != end; ++begin)
cout << *begin << " ";
cout << endl;
}
int main(void) {
INT a[6] = {1, 4, 5, 2, 9, 2};
int b[7] = {11, 32, 23, 35, 2, 4, 4};
list<INT> lst1(a, a+6), lst2(b, b+7);
lst1.sort(); //顺序排序
cout << "1. "; Plist(lst1.begin(), lst1.end());
lst1.remove(2); //删除所有与2相等的元素
cout << "2. "; Plist(lst1.begin(), lst1.end());
lst2.pop_front(); //删除第一个元素
cout << "3. "; Plist(lst2.begin(), lst2.end());
lst2.unique(); //删除所有和前一个元素相等的元素
cout << "4. "; Plist(lst2.begin(), lst2.end());
lst2.sort();
lst1.merge(lst2); //合并lst2到lst1并清空lst2
cout << "5. "; Plist(lst1.begin(), lst1.end());
cout << "6. "; Plist(lst2.begin(), lst2.end());
lst1.reverse(); //将lst1倒置
cout << "7. "; Plist(lst1.begin(), lst1.end());
lst2.insert(lst2.begin(), a + 1, a + 4); //在lst2中插入3, 2, 4三个元素
cout << "8. "; Plist(lst2.begin(), lst2.end());
list<INT>::iterator p1, p2, p3; // 定义三个迭代器
p1 = find(lst1.begin(), lst1.end(), 23); //查找元素
p2 = find(lst2.begin(), lst2.end(), 4);
p3 = find(lst2.begin(), lst2.end(), 5);
lst1.splice(p1, lst2, p2, p3); //将p2, p3插入p1之前,并从lst2中删除p2, p3
cout << "9. "; Plist(lst1.begin(), lst1.end());
cout << "10. "; Plist(lst2.begin(), lst2.end());
return 0;
}
内容总结
以上是互联网集市为您收集整理的c++ list使用方式全部内容,希望文章能够帮你解决c++ list使用方式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。