码迷,mamicode.com
首页 > 编程语言 > 详细

c++ stl list使用总结(转)

时间:2014-09-12 16:45:53      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   for   

转自: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使用总结(转)

标签:style   blog   http   color   io   os   使用   ar   for   

原文地址:http://www.cnblogs.com/qbmiller/p/3968417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!