• 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

  • 相关阅读:
    手机端上传图片及java后台接收和ajaxForm提交
    JEECG中datagrid方法自定义查询条件
    微信分享到朋友圈按钮 右上角提示
    Js获取后台集合List的值和下标的方法
    redis系列之数据库与缓存数据一致性解决方案
    替换{0}为指定的字符串(MessageFormat)
    java中对array数组的常用操作
    面试题-Java Web-网络通信
    你应该知道的JAVA面试题
    各大互联网公司java开发面试常问问题
  • 原文地址:https://www.cnblogs.com/qbmiller/p/3968417.html
Copyright © 2020-2023  润新知