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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容