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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容