1.关于本文
文中描述的是一个学习List类过程中写的程序,程序中进行了以下步骤
1)创建list<string>
2)调用函数push_front和push_back添加元素
3)调用迭代器遍历list
4)调用函数front和back查找首尾结点
5)调用函数pop_front和pop_back删除元素
6)调用函数insert添加元素
7)调用函数erase和removed删除元素
8)调用函数sort对list进行排序
9)调用函数merge将两个list合并
10)调用函数reverse翻转list中全部元素
11)调用函数clear清空list中全部元素
2.程序代码
下面的代码是在VC++6.0中编译的
#include <iostream> #include <cstdlib> #include <string> #include <list> using namespace std; int main() { // 创建list string temp[] = {"Tsybius", "Galatea"}; list<string> namelist (temp, temp + sizeof(temp)/sizeof(string)); // 添加元素 cout << "Push from front and back" << endl; namelist.push_front("Quintus"); namelist.push_back("Titus"); cout << endl; // 遍历list cout << "Traverse List" << endl; list<string> :: iterator iter; for(iter = namelist.begin(); iter != namelist.end(); iter++) { cout << *iter << ‘ ‘; } cout << endl << endl; // 首尾结点 cout << "Front & Back" << endl; cout << "front: " << namelist.front() << endl; cout << "back: " << namelist.back() << endl; cout << endl; // 删除元素 cout << "Delete from Front and Back" << endl; namelist.pop_front(); namelist.pop_back(); cout << endl; // 遍历list cout << "Traverse List" << endl; for(iter = namelist.begin(); iter != namelist.end(); iter++) { cout << *iter << ‘ ‘; } cout << endl << endl; // 插入元素 cout << "Insert Data" << endl; namelist.insert(namelist.begin(), "Julius"); iter = namelist.begin(); iter++;iter++; //注意:不能写成iter+=2,相关运算符没有重载 namelist.insert(iter, "Marcus"); cout << endl; // 遍历list cout << "Traverse List" << endl; for(iter = namelist.begin(); iter != namelist.end(); iter++) { cout << *iter << ‘ ‘; } cout << endl << endl; // 删除数据 cout << "Erase Data" << endl; iter = namelist.begin(); advance(iter, 3); namelist.erase(iter); //方法1 namelist.remove("Julius"); //方法2 cout << endl; // 遍历list cout << "Traverse List" << endl; for(iter = namelist.begin(); iter != namelist.end(); iter++) { cout << *iter << ‘ ‘; } cout << endl << endl; // 排序List(自小到大,sort中可以添加返回布尔值的函数,作为比较依据) namelist.sort(); // 遍历list cout << "Traverse List" << endl; for(iter = namelist.begin(); iter != namelist.end(); iter++) { cout << *iter << ‘ ‘; } cout << endl << endl; // 与另一个list合并 cout << "Merge Two Lists" << endl; string temp2[] = {"Licinius", "Cicero"}; list<string> namelist2 (temp2, temp2 + sizeof(temp2)/sizeof(string)); namelist.merge(namelist2); cout << endl; // 遍历list cout << "Traverse List" << endl; for(iter = namelist.begin(); iter != namelist.end(); iter++) { cout << *iter << ‘ ‘; } cout << endl << endl; // 翻转list cout << "Reverse List" << endl; namelist.reverse(); cout << endl; // 遍历list cout << "Traverse List" << endl; for(iter = namelist.begin(); iter != namelist.end(); iter++) { cout << *iter << ‘ ‘; } cout << endl << endl; // 清空List cout << "Size: " << namelist.size() << endl; cout << "IsEmpty? " << namelist.empty() << endl; namelist.clear(); cout << "Size: " << namelist.size() << endl; cout << "IsEmpty? " << namelist.empty() << endl; system("pause"); return 0; }
4.运行结果
END
原文地址:http://my.oschina.net/Tsybius2014/blog/292778