C++中list的使用方法及常用list操作总结
C++中list的使用方法及常用list操作总结 一、List定义: List是stl实现的双向链表,与向量(vectors)相比,它允许快速的插入和删除,但是随机访问却比较慢。使用时需要添加头文件 二、List定义和初始化: list<int>lst1; //创建空list 三、List常用操作函数: Lst1.assign() 给list赋值 四、List使用示例: 示例1:遍历List //迭代器法 for(list<int>::const_iteratoriter = lst1.begin();iter != lst1.end();iter++) { cout<<*iter; } cout<<endl; 示例2: #include <iostream> #include <list> #include <numeric> #include <algorithm> #include <windows.h> using namespace std; typedef list<int> LISTINT; typedef list<int> LISTCHAR; void main() { //用LISTINT创建一个list对象 LISTINT listOne; //声明i为迭代器 LISTINT::iterator i; listOne.push_front(3); listOne.push_front(2); listOne.push_front(1); listOne.push_back(4); listOne.push_back(5); listOne.push_back(6); cout << "listOne.begin()--- listOne.end():" << endl; for (i = listOne.begin(); i != listOne.end(); ++i) cout << *i << " "; cout << endl; LISTINT::reverse_iterator ir; cout << "listOne.rbegin()---listOne.rend():" << endl; for (ir = listOne.rbegin(); ir != listOne.rend(); ir++) { cout << *ir << " "; } cout << endl; int result = accumulate(listOne.begin(),listOne.end(),0); cout << "Sum=" << result << endl; cout << "------------------" << endl; //用LISTCHAR创建一个list对象 LISTCHAR listTwo; //声明i为迭代器 LISTCHAR::iterator j; listTwo.push_front('C'); listTwo.push_front('B'); listTwo.push_front('A'); listTwo.push_back('D'); listTwo.push_back('E'); listTwo.push_back('F'); cout << "listTwo.begin()---listTwo.end():" << endl; for (j = listTwo.begin(); j != listTwo.end(); ++j) cout << char(*j) << " "; cout << endl; j = max_element(listTwo.begin(),listTwo.end()); cout << "The maximum element in listTwo is: " << char(*j) << endl; Sleep(10000); } #include <iostream> #include <list> #include <windows.h> using namespace std; typedef list<int> INTLIST; //从前向后显示list队列的全部元素 void put_list(INTLIST list,char *name) { INTLIST::iterator plist; cout << "The contents of " << name << " : "; for (plist = list.begin(); plist != list.end(); plist++) cout << *plist << " "; cout << endl; } //测试list容器的功能 void main(void) { //list1对象初始为空 INTLIST list1; INTLIST list2(5,1); INTLIST list3(list2.begin(),--list2.end()); //声明一个名为i的双向迭代器 INTLIST::iterator i; put_list(list1,"list1"); put_list(list2,"list2"); put_list(list3,"list3"); list1.push_back(7); list1.push_back(8); cout << "list1.push_back(7) and list1.push_back(8):" << endl; put_list(list1,"list1"); list1.push_front(6); list1.push_front(5); cout << "list1.push_front(6) and list1.push_front(5):" << endl; put_list(list1,"list1"); list1.insert(++list1.begin(),3,9); cout << "list1.insert(list1.begin()+1,9):" << endl; put_list(list1,"list1"); //测试引用类函数 cout << "list1.front()=" << list1.front() << endl; cout << "list1.back()=" << list1.back() << endl; list1.pop_front(); list1.pop_back(); cout << "list1.pop_front() and list1.pop_back():" << endl; put_list(list1,"list1"); list1.erase(++list1.begin()); cout << "list1.erase(++list1.begin()):" << endl; put_list(list1,"list1"); list2.assign(8,1); cout << "list2.assign(8,1):" << endl; put_list(list2,"list2"); cout << "list1.max_size(): " << list1.max_size() << endl; cout << "list1.size(): " << list1.size() << endl; cout << "list1.empty(): " << list1.empty() << endl; put_list(list1,"list1"); put_list(list3,"list3"); cout << "list1>list3: " << (list1 > list3) << endl; cout << "list1<list3: " << (list1 < list3) << endl; list1.sort(); put_list(list1,"list1"); list1.splice(++list1.begin(),list3); put_list(list1,"list3"); Sleep(10000); } 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |