Android NDK开发之旅29--C++--list、set、map用法

Android NDK开发之旅 目录

1.list-基本使用

#include <iostream>
#include <list>
using namespace std;

void main() {

    list<int> lt;
    //从头部添加
    lt.push_front(10);
    lt.push_front(20);
    lt.push_front(30);
    //从尾部添加
    lt.push_back(40);
    lt.push_back(50);
    lt.push_back(60);

    //循环遍历
    for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {

        cout << *it << endl;
    }

    list<int>::iterator it = lt.begin();
    //连续相加允许(++)
    //支持'++'、'--'运算符
    it++;
    it--;
    cout << endl << "支持++、--运算符" << endl;
    cout << *it << endl;

    //注意:不支持间断
    //不支持'+'、'-'运算度
    // it = it - 1;  错误调用
    getchar();
}

执行代码

30
20
10
40
50
60

支持++、--运算符
30

2.list-删除

#include <iostream>
#include <list>
using namespace std;

void main() {

    list<int> lt;
    //从头部添加
    lt.push_front(10);
    lt.push_front(20);
    lt.push_front(30);
    //从尾部添加
    lt.push_back(40);
    lt.push_back(50);
    lt.push_back(60);

    cout << endl << "删除方式1:根据位置删除" << endl;
    //删除方式1
       list<int>::iterator it= lt.begin();
        it++;
        //删除:删除第二个元素
        lt.erase(it);

        //循环遍历
        for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {

            cout << *it << endl;
        }
        cout << endl << "删除方式2:直接根据内容删除" << endl;
    //删除方式2
    //直接根据内容删除
   lt.remove(30);

   //循环遍历
   for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {

       cout << *it << endl;
   }

   cout << endl << "删除方式3:区间删除" << endl;

    //"删除方式3:区间删除
    //开始位置
    list<int>::iterator it_begin = lt.begin();
    //结束位置
    list<int>::iterator it_end = lt.begin();
    it_end++;
    it_end++;
    //删除元素(如果已经被删除的元素不能够在删除)
    lt.erase(it_begin, it_end);

    //循环遍历
    for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {
    
        cout << *it << endl;
    }

    getchar();
}

执行代码

删除方式1:根据位置删除
30
10
40
50
60

删除方式2:直接根据内容删除
10
40
50
60

删除方式3:区间删除
50
60

3.list-插入

#include <iostream>
#include <list>
using namespace std;

void main() {

    list<int> lt;
    //从尾部添加
    lt.push_back(40);
    lt.push_back(50);
    lt.push_back(60);

    //插入
    lt.insert(lt.begin(), 30);
    //循环遍历
    for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {
        cout << *it << endl;
    }

    getchar();
}

执行代码

30
40
50
60

4.set-基本使用(元素唯一,默认从小到大排列)

#include <iostream>
#include <set>
using namespace std;

void main() {
    set<int> st;
    st.insert(40);
    st.insert(10);
    st.insert(30);
    st.insert(20);

    //删除
    set<int>::iterator it = st.begin();
    st.erase(it);

    for (set<int>::iterator it = st.begin(); it != st.end(); it++) {
        cout << *it << endl;

    }
    getchar();
}

执行代码

20
30
40

5set-从大到小排列

#include <iostream>
#include <set>
#include <functional> 
using namespace std;

void main() {

    set<int, greater<int> > st;
    st.insert(40);
    st.insert(10);
    st.insert(30);
    st.insert(20);

    for (set<int>::iterator it = st.begin(); it != st.end(); it++) {
    
        cout << *it << endl;
    }
    getchar();
}

执行代码

40
30
20
10

6.set-基本使用(元素唯一,默认从小到大排列)

#include <iostream>
#include <set>
#include <functional> 
using namespace std;


class Student {
private:
    char* name;
    int score;
public:
    Student(char* name, int score) {
        this->name = name;
        this->score = score;
    }
    int getScore() {
        return this->score;
    }
    void printStudent() {
        cout << "name:" << this->name << "  score:" << this->score << endl;
    }
};
//仿函数
struct Soft {
    //方式一:不写常量
    //    bool operator()(Student &left,Student &right){
    //        return left.getScore() < right.getScore();
    //    }
    //方式二:const修饰
    bool operator()(const Student &left, const Student &right) {
        //类型转换
        Student stu_left = const_cast<Student&>(left);
        Student stu_right = const_cast<Student&>(right);
        return stu_left.getScore() > stu_right.getScore();
    }
};

void main() {
    set<Student, Soft> st;
    st.insert(Student("Jack", 96));
    st.insert(Student("Pi", 63));
    st.insert(Student("Song", 77));
    st.insert(Student("Music", 88));
    st.insert(Student("Lucy", 56));

    for (set<Student>::iterator it = st.begin(); it != st.end(); it++) {
        Student stu = const_cast<Student&>(*it);
        stu.printStudent();
    }

    getchar();
}

执行代码

name:Jack  score:96
name:Music  score:88
name:Song  score:77
name:Pi  score:63
name:Lucy  score:56

7.set-查找

对诸如set、map这种关键字唯一的集合而言,lower_bound、upper_bound返回迭代器是相同,关键字val在集合中不存在,二者返回结果一样,都是按照集合实例化时给定的Compare比较,不在val之前的第一个元素(亦即之后或者等于,如果按照默认的比较类型less,函数返回的是≥val的最小的元素);如果关键在val在集合中存在,lower_bound返回val关键字本身的迭代器,upper_bound返回关键字val下一个元素迭代器。

例1

#include <iostream>  
#include <set>  
#include <functional> 
using namespace std;

typedef set<int> SET_INT;
int main()
{
    SET_INT s1;
    SET_INT::iterator i;
    s1.insert(5);
    s1.insert(10);
    s1.insert(15);
    s1.insert(20);
    s1.insert(25);

    cout << endl << "s1 -- starting at s1.lower_bound(12)" << endl;
    // prints: 15,20,25
    for (i = s1.lower_bound(12); i != s1.end(); i++)
        cout << "s1 has " << *i << " in its set." << endl;

    cout << endl << "s1 -- starting at s1.lower_bound(15)" << endl;
    // prints: 15,20,25
    for (i = s1.lower_bound(15); i != s1.end(); i++)
        cout << "s1 has " << *i << " in its set." << endl;

    cout << endl << "s1 -- starting at s1.upper_bound(12)" << endl;
    // prints: 15,20,25
    for (i = s1.upper_bound(12); i != s1.end(); i++)
        cout << "s1 has " << *i << " in its set." << endl;

    cout << endl << "s1 -- starting at s1.upper_bound(15)" << endl;
    // prints: 20,25
    for (i = s1.upper_bound(15); i != s1.end(); i++)
        cout << "s1 has " << *i << " in its set." << endl;

    cout << endl << "s1 -- starting s1.equal_range(12)" << endl;
    // does not print anything
    for (i = s1.equal_range(12).first; i != s1.equal_range(12).second; i++)
        cout << "s1 has " << *i << " in its set." << endl;

    cout << endl << "s1 -- starting s1.equal_range(15)" << endl;
    // prints: 15
    for (i = s1.equal_range(15).first; i != s1.equal_range(15).second; i++)
        cout << "s1 has " << *i << " in its set." << endl;


    getchar();
    return 0;
}

执行代码

s1 -- starting at s1.lower_bound(12)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.

s1 -- starting at s1.lower_bound(15)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.

s1 -- starting at s1.upper_bound(12)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.

s1 -- starting at s1.upper_bound(15)
s1 has 20 in its set.
s1 has 25 in its set.

s1 -- starting s1.equal_range(12)

s1 -- starting s1.equal_range(15)
s1 has 15 in its set.

例2

#include <iostream>  
#include <set>  
#include <functional> 
using namespace std;

/*Student结构体*/
struct Student {
    string name;
    int age;
    string sex;
};

/*“仿函数"。为Student set指定排序准则*/
class studentSortCriterion {
public:
    bool operator() (const Student &a, const Student &b) const {
        /*先比较名字;若名字相同,则比较年龄。小的返回true*/
        if (a.name < b.name)
            return true;
        else if (a.name == b.name) {
            if (a.age < b.age)
                return true;
            else
                return false;
        }
        else
            return false;
    }
};

int main()
{
    set<Student, studentSortCriterion> stuSet;

    Student stu1, stu2;
    stu1.name = "Jack";
    stu1.age = 13;
    stu1.sex = "male";

    stu2.name = "Marry";
    stu2.age = 23;
    stu2.sex = "female";

    Student stu3;
    stu3.name = "Lucy";
    stu3.age = 23;
    stu3.sex = "female";

    stuSet.insert(stu1);
    stuSet.insert(stu2);
    stuSet.insert(stu3);
    /*构造一个测试的Student,可以看到,即使stuTemp与stu1实际上并不是同一个对象,
    *但当在set中查找时,仍会查找成功。这是因为已定义的studentSortCriterion的缘故。
    */
    Student stuTemp;
    stuTemp.name = "Marry";
    stuTemp.age = 23;

    set<Student, studentSortCriterion>::iterator iter;
    iter = stuSet.find(stuTemp);
    if (iter != stuSet.end()) {
        cout << (*iter).name.c_str() << endl;
    }
    else {
        cout << "Cannot fine the student!" << endl;
    }

    Student stuTemp2;
    stuTemp.name = "Lili";
    stuTemp.age = 13;
    set<Student, studentSortCriterion>::iterator iter2;
    iter2 = stuSet.find(stuTemp2);
    if (iter2 != stuSet.end()) {
        cout << (*iter).name.c_str() << endl;
    }
    else {
        cout << "Cannot fine the student!" << endl;
    }

    getchar();
    return 0;
}

执行代码

Marry
Cannot fine the student!

8.multiset-基本使用

  • 允许存储重复元素
  • 默认升序排列
#include <iostream>
#include <set>
#include <functional> 
using namespace std;


class Student {
private:
    char* name;
    int score;
public:
    Student(char* name, int score) {
        this->name = name;
        this->score = score;
    }
    int getScore() {
        return this->score;
    }
    void printStudent() {
        cout << "name:" << this->name << "  score:" << this->score << endl;
    }
};
//仿函数
struct Soft {
    //方式一:不写常量
    //    bool operator()(Student &left,Student &right){
    //        return left.getScore() < right.getScore();
    //    }
    //方式二:const修饰
    bool operator()(const Student &left, const Student &right) {
        //类型转换
        Student stu_left = const_cast<Student&>(left);
        Student stu_right = const_cast<Student&>(right);
        return stu_left.getScore() > stu_right.getScore();
    }
};

void main() {

    cout << endl << "默认升序" << endl;

    //升序
    multiset<int> mst;
    mst.insert(10);
    mst.insert(20);
    mst.insert(30);
    mst.insert(10);

    for (multiset<int>::iterator it = mst.begin(); it != mst.end(); it++) {

        cout << *it << endl;
    }
    cout << endl << "使用greater降序" << endl;
    //降序
    multiset<int, greater<int> > mst2;
    mst2.insert(10);
    mst2.insert(20);
    mst2.insert(30);
    mst2.insert(10);

    for (multiset<int>::iterator it = mst2.begin(); it != mst2.end(); it++) {
        cout << *it << endl;
    }
    cout << endl << "自定义排序" << endl;
    //自定义排序方式
    multiset<Student, Soft> mst3;
    mst3.insert(Student("Jack", 96));
    mst3.insert(Student("Pi", 63));
    mst3.insert(Student("Song", 77));
    mst3.insert(Student("Music", 88));
    mst3.insert(Student("Lucy", 56));

    for (multiset<Student>::iterator it = mst3.begin(); it != mst3.end(); it++) {
        Student stu = const_cast<Student&>(*it);
        stu.printStudent();
    }

    getchar();
    return;
}

执行代码

默认升序
10
10
20
30

使用greater降序
30
20
10
10

自定义排序
name:Jack  score:96
name:Music  score:88
name:Song  score:77
name:Pi  score:63
name:Lucy  score:56

9.map-基本使用

#include <iostream>
#include <map>
#include <string>
#include <functional> 
using namespace std;

void main() {

    map<int, string> mp;

    cout << endl << "方式1:插入数据pair" << endl;
    //方式1:插入数据pair
    mp.insert(pair<int, string>(01, "Lucy"));
    mp.insert(pair<int, string>(02, "Cookie"));
    mp.insert(pair<int, string>(03, "Sun"));
    mp.insert(pair<int, string>(04, "Jack"));

    for (map<int, string>::iterator it = mp.begin(); it != mp.end(); it++) {
        //获取key:it->first
        cout << "key:" << it->first << endl;
        //获取value:it->second
        cout << "value:" << it->second.c_str() << endl;
    }

    cout << endl << "方式2:pair" << endl;

    //方式二:如果key存在,那么就不添加同时不覆盖,如果不存在,就添加
    pair<map<int, string>::iterator, bool> result = mp.insert(map<int, string>::value_type(04, "Month"));
    if (result.second) {
        cout << "添加成功"<< endl;
    }
    else {
        cout << "已存在,添加失败!" << endl;
    }

    for (map<int, string>::iterator it = mp.begin(); it != mp.end(); it++) {
        //获取key:it->first
        cout << "key:" << it->first << endl;
        //获取value:it->second
        cout << "value:" << it->second.c_str() << endl;
    }

    cout << endl << "方式3:make_pair" << endl;
    //方式3:make_pair
    mp.insert(make_pair(05, "Liu"));
    for (map<int, string>::iterator it = mp.begin(); it != mp.end(); it++) {
        //获取key:it->first
        cout << "key:" << it->first << endl;
        //获取value:it->second
        cout << "value:" << it->second.c_str() << endl;
    }

    cout << endl << "方式4:" << endl;
    //方式四:如果key存在,重复添加会覆盖,如果不存在,那就直接添加
    mp[5] = "Ding";
    mp[6] = "Coco";

    for (map<int, string>::iterator it = mp.begin(); it != mp.end(); it++) {
        //获取key:it->first
        cout  << "key:" << it->first<< endl;
        //获取value:it->second
        cout  << "value:" << it->second.c_str() << endl;
    }

    getchar();
}

执行代码

方式1:插入数据pair
key:1
value:Lucy
key:2
value:Cookie
key:3
value:Sun
key:4
value:Jack

方式2:pair
已存在,添加失败!
key:1
value:Lucy
key:2
value:Cookie
key:3
value:Sun
key:4
value:Jack

方式3:make_pair
key:1
value:Lucy
key:2
value:Cookie
key:3
value:Sun
key:4
value:Jack
key:5
value:Liu

方式4:
key:1
value:Lucy
key:2
value:Cookie
key:3
value:Sun
key:4
value:Jack
key:5
value:Ding
key:6
value:Coco

10.map-删除

#include <iostream>
#include <map>
#include <string>
#include <functional> 
using namespace std;

void main() {

    map<int, string> mp;

    //方式1:插入数据pair
    mp.insert(pair<int, string>(01, "Lucy"));
    mp.insert(pair<int, string>(02, "Cookie"));
    mp.insert(pair<int, string>(03, "Sun"));
    mp.insert(pair<int, string>(04, "Jack"));
    //删除
    map<int, string>::iterator it = mp.begin();
    mp.erase(it);

    for (map<int, string>::iterator it = mp.begin(); it != mp.end(); it++) {
        //获取key:it->first
        cout << "key:" << it->first << endl;
        //获取value:it->second
        cout << "value:" << it->second.c_str() << endl;
    }
    getchar();
}

执行代码

key:2
value:Cookie
key:3
value:Sun
key:4
value:Jack

11.map-查找(与set类似)


#include <iostream>
#include <map>
#include <string>
#include <functional> 
using namespace std;

void main() {

    map<int, string> mp;


    mp.insert(pair<int, string>(01, "Lucy"));
    mp.insert(pair<int, string>(02, "Cookie"));
    mp.insert(pair<int, string>(03, "Sun"));
    mp.insert(pair<int, string>(04, "Jack"));


    map<int, string>::iterator it;
    map<int, string>::iterator flag = mp.end();
    it = mp.find(5);

    if (it != flag)
    {
        (*it).second = "剩余";
    }
    else
    {
        cout << "没有找到" << endl;
    }


    // 该函数返回的是一对迭代器,第一个迭代器指向所查找元素的第一次出现的位置,
    // 第二个迭代器指向所查找元素最后一次出现位置的后一个位置
    pair<map<int, string>::iterator, map<int, string>::iterator> p = mp.equal_range(2);

    if (p.first != mp.end())
    {
        cout << "key: " << p.first->first << endl;
        cout << "value: " << p.first->second.c_str() << endl;
    
    }

    if (p.second != mp.end())
    {
        cout << "key: " << p.second->first << endl;
        cout << "value: " << p.second->second.c_str() << endl;

    }

    getchar();
}

执行代码

没有找到
key: 2
value: Cookie
key: 3
value: Sun

12.multimap-一对多

使用场景:一个用户对应多个订单

#include <iostream>
#include <map>
#include <string>
#include <functional> 
using namespace std;

class Order {
private:
    char* name;
    int num;
public:
    Order(char* name, int num) {
        this->name = name;
        this->num = num;
    }
    void printOrder() {

        cout << " 订单号:" << this->num << "  商品:"<< this->name  << endl;
    }
};
void main() {



    multimap<string, Order> mst;
    mst.insert(make_pair("Jack", Order("男士外套", 01)));
    mst.insert(make_pair("Jack", Order("户外跑鞋", 02)));

    mst.insert(make_pair("Lucy", Order("女士外套", 03)));
    mst.insert(make_pair("Lucy", Order("女士高跟鞋",02)));

    mst.insert(make_pair("Rose", Order("女士纱衣", 03)));
    mst.insert(make_pair("Rose", Order("女士布鞋", 02)));
    mst.insert(make_pair("Rose", Order("女士外套", 02)));
    mst.insert(make_pair("Rose", Order("女士裤子", 02)));

    //遍历
        for (multimap<string,Order>::iterator it = mst.begin() ; it != mst.end() ; it++){
            //获取key:it->first
            cout << "key: " << it->first.c_str() << endl;

            //获取value:it->second
            Order order = const_cast<Order&>(it->second);
            order.printOrder();
        }

        cout << endl << "只获取Lucy订单" << endl;

    //获取订单的数量
    int count = mst.count("Lucy");
    //打印"梦想"订单:找到
    multimap<string, Order>::iterator it = mst.find("Lucy");
    //循环遍历打印
    //计数
    int i = 0;
    while (it != mst.end() && i < count) {

        cout << "key: " << it->first.c_str() << endl;
        Order order = const_cast<Order&>(it->second);
        order.printOrder();
        i++;
        it++;
    }
    getchar();
}

执行代码

key: Jack
 订单号:1  商品:男士外套
key: Jack
 订单号:2  商品:户外跑鞋
key: Lucy
 订单号:3  商品:女士外套
key: Lucy
 订单号:2  商品:女士高跟鞋
key: Rose
 订单号:3  商品:女士纱衣
key: Rose
 订单号:2  商品:女士布鞋
key: Rose
 订单号:2  商品:女士外套
key: Rose
 订单号:2  商品:女士裤子

只获取Lucy订单
key: Lucy
 订单号:3  商品:女士外套
key: Lucy
 订单号:2  商品:女士高跟鞋

特别感谢:

Dream

推荐阅读更多精彩内容