标签:style class blog code color com
STL 中的list容器
//对已string型list进行添加,删除,查找,插入操作 #include "stdafx.h" #include <iostream> #include <string> #include <list> using namespace std; int main() { list<string> oList; //list不能对相应的对象进行直接操作,只能通过算法或迭代器 //oList[0] = "www.jiesoon.com"; //不行 //list没有预留空间 //oList.capacity(); //没有这个函数 //给oList对象添加元素 oList.push_back("3.jiesoon.com"); oList.push_front("2.jiesoon.com"); oList.push_back("4.jiesoon.com"); oList.push_front("1.jiesoon.com"); list<string>::iterator itList = oList.begin(); for(; itList != oList.end(); ++itList){ cout<< *itList << endl; } cout<< "oList存有的元素:" << oList.size() << endl; cout << "**********************************************" << endl; //list 是个双向链表 vector deque 也可以用迭代器双向访问 itList = oList.begin(); //itList += 2; //不可以,list 不是一个连续的容器 ++itList; ++itList; cout << *itList << endl; --itList; cout << *itList << endl; cout << "**********************************************" << endl; //插入数据 (不同于vector,deque 在不同位置插入数据有着一样的效率) itList = oList.begin(); oList.insert(itList,"0.jiesoon.com"); for(itList = oList.begin(); itList != oList.end(); ++itList){ cout<< *itList << endl; } cout << "**********************************************" << endl; //删除数据方法① oList.remove("2.jiesoon.com"); for(itList = oList.begin(); itList != oList.end(); ++itList){ cout<< *itList << endl; } cout << "**********************************************" << endl; //删除数据方法② itList = oList.begin(); oList.erase(itList); for(itList = oList.begin(); itList != oList.end(); ++itList){ cout<< *itList << endl; } return 0; }
标签:style class blog code color com
原文地址:http://www.cnblogs.com/zenseven/p/3809035.html