/*首先我是c语言的爱好者,之前没接触过c++,现在开始学习c++,因此总会和c语言进行对比,所谓的容器,其实就是链表的!在c语言数据结构中有所提及,c++把其进行封装成了库,不需要像以前学习数据结构那样具体去实现,只要学院、会调用就可以了。
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了班戈免费建站欢迎大家使用!
*/
#include "stdafx.h"
#include
#include
#include
using namespace std;
typedef list
//从前向后显示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 list1.sort(); put_list(list1, "list1"); list1.splice(++list1.begin(), list3); put_list(list1, "list1"); put_list(list3, "list3"); Sleep(10000); }
标题名称:c++list的使用方法
标题来源:http://jkwzsj.com/article/jeghog.html