C++ List集合

lists.cpp

#include <string>
#include <iostream>
#include <algorithm>
#include "lists.h"
#include "student.h"
//这里没有.h
#include "list"

using namespace std;
//for循环带参函数 
void fun(Student i) {
    i.toString();
}

void showLists() {
//这里的基本初始化和vector相同
    list<int> one;                                //定义一个空的、元素类型是 int 的 list 链表
    list<int> two(4, 100);               //定义一个包含4个元素,每个元素的值都是100的 list 链表
    list<int> three(two.begin(), two.end());  //使用 two 这个对象的迭代器,从开始到结束的所有元素来初始化当前对象
    list<int> four(three);                       // 使用 three 这个对象来初始化当前对象
    int myints[] = {16, 2, 77, 29};
    list<int> five(myints, myints + sizeof(myints) / sizeof(int)); //使用一个普通的 int 数组来初始化当前对象
    list<Student> listMaqi;
    Student maqi_1(*new string("maqi_1"));
    Student maqi_2(*new string("maqi_2"));
    Student maqi_3(*new string("maqi_3"));
    //add方法
    listMaqi.push_back(maqi_1);
    listMaqi.push_back(maqi_2);
    listMaqi.push_back(maqi_3);

    //获取指向第一位的迭代器 auto类似于java的Object或者kotiln的Any
    auto five_it = five.begin();
    //取其中第2个元素 第二参数为迭代器的偏移,记住第一个参数为five_it迭代器,不要传错了
    advance(five_it, 1);
    cout << "advance(two, two.size()- 1) = " << *five_it << endl;
    //替换two到five中类似于java的replaceAll()
    five.swap(two);
    //获取指向第一位的迭代器
    list<int>::iterator fiveBegin = five.begin();
    //迭代器循环
    for (int i = 0; i < five.size(); ++i) {
        cout << " five[" << i << "] = " << *fiveBegin << endl;
        fiveBegin++;
    }
    //循环简写
    for (auto item :five) cout << " three[i] = " << item << endl;
    //清空
    three.clear();
    for (auto item :three) cout << " three[i] = " << item << endl;

    //取第一个元素
    //listMaqi.back().toString();
    //取最后一个元素
    //listMaqi.front().toString();
    //排序 注意:一定要重写Student的 bool operator<(const Student &rhs) 不然报错,这个java的重写compare一样。
    listMaqi.sort();
    //倒序
    listMaqi.reverse();
    //又一种循环
    for (auto item :listMaqi) item.toString();
    system("pause");
    unsigned int size = listMaqi.size();
    //有一种for循环:配合advance使用
    for (int i = 0; i < size; ++i) {
    // typename std::申明为是一种类类型而非变量名 ,有时候避免报错。
        typename std::list<Student, std::allocator<Student>>::iterator iterator = listMaqi.begin();
        advance(iterator, i);//取其中第N个元素
        //取值并调方法
        (*iterator).toString();
    }
    system("pause");
    list<Student> newListMaqi(listMaqi.begin(), listMaqi.end());
    newListMaqi.insert(newListMaqi.begin(), listMaqi.begin(), listMaqi.end()--);
    Student maqi_5(*new string("maqi_5"));
    //add到第一个元素
    newListMaqi.push_front(maqi_5);
    //插入3个到末尾
    newListMaqi.insert(newListMaqi.end(),3,maqi_5);
    //带参循环fun函数输出
    for_each(newListMaqi.begin(), newListMaqi.end(), fun);
    system("pause");
    //删除第一个元素
    newListMaqi.pop_front();
    //删除最后一个元素
    newListMaqi.pop_back();
    //删除第一个元素 erase第一个参数为其实迭代器,第二个参数为结束迭代器,属于范围删除
    newListMaqi.erase(newListMaqi.begin());
    //带参循环fun函数输出
    for_each(newListMaqi.begin(), newListMaqi.end(), fun);
    system("pause");
    //listMaqi.clear();
    newListMaqi.reverse();
    listMaqi.splice(listMaqi.begin(), newListMaqi);
    //该循环使用&item 可直接修改item成员变量。
    for (auto item :listMaqi) item.toString();
}

Student.cpp


enum Sex {
    man = 1,
    wuman = 0
};

struct Student {
    string name;
    int num;
    Sex sex;
    int age;

    string &getName() {
        return name;
    }
    //重载函数 类似java的compare函数
    bool operator<(const Student &rhs) const {
        return rhs.num > this->num;
    }

    bool operator>(const Student &rhs) const {
        return rhs.num < this->num;
    }

    bool operator>=(const Student &rhs) const {
        return !(this->num > rhs.num);
    }

    bool operator<=(const Student &rhs) const {
        return !(this->num < rhs.num);
    }

    Student() {}

    Student(const string &name, int num, Sex sex, int age) : name(name), num(num), sex(sex),
                                                             age(age) {}

    Student(const string &name)
            : name(name), num(num = 1000 * rand()), sex(sex = man), age(age = 18) {
    }

    Sex getSex() const {
        return sex;
    }

    void toString() {
        cout << " name = " << name << " num = " << num << " sex = " << sex << " age = " << age
             << endl;
    }
};

list.h

#ifndef CADDMORE_LISTS_H
#define CADDMORE_LISTS_H

void showLists();

#endif //CADDMORE_LISTS_H

输出结果

···
five[0] = 16
five[1] = 2
five[2] = 77
five[3] = 29
three[i] = 16
three[i] = 2
three[i] = 77
three[i] = 29
name = maqi_2 num = 18467000 sex = 1 age = 18
name = maqi_3 num = 6334000 sex = 1 age = 18
name = maqi_1 num = 41000 sex = 1 age = 18
请按任意键继续…

name = maqi_2 num = 18467000 sex = 1 age = 18
name = maqi_3 num = 6334000 sex = 1 age = 18
name = maqi_1 num = 41000 sex = 1 age = 18
请按任意键继续…

name = maqi_2 num = 18467000 sex = 1 age = 18
name = maqi_3 num = 6334000 sex = 1 age = 18
name = maqi_1 num = 41000 sex = 1 age = 18
请按任意键继续…

name = maqi_1 num = 41000 sex = 1 age = 18
name = maqi_3 num = 6334000 sex = 1 age = 18
name = maqi_2 num = 18467000 sex = 1 age = 18
name = maqi_2 num = 18467000 sex = 1 age = 18
name = maqi_3 num = 6334000 sex = 1 age = 18
name = maqi_1 num = 41000 sex = 1 age = 18

···

总结:

1.对象的sort和reverse函数需要重写该类的operator<
2.advance 函数可以取list中指定角标的元素
3.其中的包含多种for循环,可以借鉴一下。
4.list和vector的最大不同在于 他是线性列表,学过数据结构都知道,列表具有快速查询和增删的功能,且list不固定大小,这一点很重要,其特性和java相似。

猜你喜欢

转载自blog.csdn.net/qq_20330595/article/details/82286003