首页 > 代码库 > c++ stl list使用总结(转)
c++ stl list使用总结(转)
转自:http://blog.csdn.net/nupt123456789/article/details/8120397
#include <iostream>#include <list>#include <string>using namespace std;class Student{private: int ID; string Name;public: Student(int ID,string Name) { this->ID=ID; this->Name=Name; } int getID() { return ID; } string getName() { return Name; }};int main(){ // create an empty list (of zero size) capable of holding doubles list<double> list0; cout << "Size of list0 is " << list0.size() << endl; // create a list with 5 empty elements list<double> list1(5); cout << "Size of list1 is " << list1.size() << endl; // create a list with 5 elements, each element having the value 10.2 list<double> list2(5, 10.2); cout << "list2: "; list<double>::iterator it; for(it = list2.begin(); it != list2.end(); ++it) cout << *it << " "; cout << endl; // create a list based on an array of elements // only the first 5 elements of the array are copied into the vector double array[8] = {3.45, 67, 10, 0.67, 8.99, 9.78, 6.77, 34.677}; list<double> list3(array, array + 5); cout << "list3: "; for(it = list3.begin(); it != list3.end(); ++it) cout << *it << " "; cout << endl; // use the copy constructor to copy list3 list into list3copy list list<double> list3copy(list3); cout << "list3copy: "; for(it = list3copy.begin(); it != list3copy.end(); ++it) cout << *it << " "; cout << endl; // assign 5 values of 10.2 to the list list<double> list4; list4.assign(5, 10.2); cout << "list4: "; for(it = list4.begin(); it != list4.end(); ++it) cout << *it << " "; cout << endl; //定义自己的数据类型 list<Student> list5; Student stu1(1,"ZhengHaibo"); Student stu2(2,"nupt"); list5.push_back(stu1); list5.push_back(stu2); list<Student>::iterator iter_stu; cout << "list5: "<<endl; for (iter_stu=list5.begin();iter_stu!=list5.end();iter_stu++) { cout<<"ID:"<<iter_stu->getID()<<" Name:"<<iter_stu->getName()<<endl; } return 0; // Output // Size of list0 is 0 // Size of list1 is 5 // list2: 10.2 10.2 10.2 10.2 10.2 // list3: 3.45 67 10 0.67 8.99 // list3copy: 3.45 67 10 0.67 8.99 // list4: 10.2 10.2 10.2 10.2 10.2 //list5: //ID:1 Name:ZhengHaibo //ID:2 Name:nupt}
上面只是一点,原文很多
vector的文章http://blog.csdn.net/nupt123456789/article/details/7482923
c++ stl list使用总结(转)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。