/*首先我是c语言的爱好者,之前没接触过c++,现在开始学习c++,因此总会和c语言进行对比,所谓的容器,其实就是链表的!在c语言数据结构中有所提及,c++把其进行封装成了库,不需要像以前学习数据结构那样具体去实现,只要学院、会调用就可以了。
*/
#include "stdafx.h"
#include <iostream>
#include <list>
#include <windows.h>
using namespace std;
typedef list<int> INTLIST; //定义一个数列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"); //print the lists
put_list(list2, "list2");
put_list(list3, "list3");
list1.push_back(7); //mask the lseven num put out
list1.push_back(8);
cout << "list1.push_back(7) and list1.push_back(8):" << endl;
put_list(list1, "list1");
list1.push_front(6); //把第一个元素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); //list1的首位直添加3个9.
cout << "list1.insert(list1.begin()+1,3,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, "list1");
put_list(list3, "list3");
Sleep(10000);
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。