Android NDK Development Journey 29--C++--list, set, map usage

Keywords: Android less

Android NDK Development Tour Directory

1.list - Basic usage

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

void main() {

    list<int> lt;
    //Add from the top
    lt.push_front(10);
    lt.push_front(20);
    lt.push_front(30);
    //Add from the tail
    lt.push_back(40);
    lt.push_back(50);
    lt.push_back(60);

    //Cyclic traversal
    for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {

        cout << *it << endl;
    }

    list<int>::iterator it = lt.begin();
    //Continuous additive permissibility (++)
    //Supports'+','--'operators
    it++;
    it--;
    cout << endl << "Support++,--operator" << endl;
    cout << *it << endl;

    //Note: Discontinuity is not supported
    //Operational degrees of'+','-'are not supported
    // it = it - 1; error call
    getchar();
}

Execution code

30
20
10
40
50
60

Supports ++, - - Operators
30

2.list - delete

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

void main() {

    list<int> lt;
    //Add from the top
    lt.push_front(10);
    lt.push_front(20);
    lt.push_front(30);
    //Add from the tail
    lt.push_back(40);
    lt.push_back(50);
    lt.push_back(60);

    cout << endl << "Delete Mode 1: Delete by Location" << endl;
    //Delete mode 1
       list<int>::iterator it= lt.begin();
        it++;
        //Delete: Delete the second element
        lt.erase(it);

        //Cyclic traversal
        for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {

            cout << *it << endl;
        }
        cout << endl << "Delete mode 2: Delete directly according to content" << endl;
    //Delete mode 2
    //Delete directly according to content
   lt.remove(30);

   //Cyclic traversal
   for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {

       cout << *it << endl;
   }

   cout << endl << "Delete Mode 3: Interval Delete" << endl;

    //"Delete Mode 3: Interval Delete
    //Starting position
    list<int>::iterator it_begin = lt.begin();
    //End position
    list<int>::iterator it_end = lt.begin();
    it_end++;
    it_end++;
    //Delete elements (if deleted elements cannot be deleted)
    lt.erase(it_begin, it_end);

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

    getchar();
}

Execution code

Delete Mode 1: Delete by Location
30
10
40
50
60

Delete mode 2: Delete directly according to content
10
40
50
60

Delete Mode 3: Interval Delete
50
60

3.list-insert

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

void main() {

    list<int> lt;
    //Add from the tail
    lt.push_back(40);
    lt.push_back(50);
    lt.push_back(60);

    //insert
    lt.insert(lt.begin(), 30);
    //Cyclic traversal
    for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {
        cout << *it << endl;
    }

    getchar();
}

Execution code

30
40
50
60

4.set - Basic use (element unique, default from small to large)

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

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

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

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

    }
    getchar();
}

Execution code

20
30
40

5set - Ranking from Big to Small

#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();
}

Execution code

40
30
20
10

6.set - Basic use (element unique, default from small to large)

#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;
    }
};
//functor
struct Soft {
    //Way 1: Don't write constants
    //    bool operator()(Student &left,Student &right){
    //        return left.getScore() < right.getScore();
    //    }
    //Mode 2: const Modification
    bool operator()(const Student &left, const Student &right) {
        //Type conversion
        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();
}

Execution code

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

7.set- lookup

For a unique set of keywords such as set and map, the lower_bound and upper_bound return iterators are the same, and the key Val does not exist in the set. Both return the same result as that given by Compare when the set is instantiated. The first element not before val (that is, later or equal to, if less is the default comparison type, the function returns is The smallest element (> val); if the key exists in the set, lower_bound returns the iterator of the Val keyword itself, and upper_bound returns the iterator of the next element of the Val keyword.

Example 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;
}

Execution code

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.

Example 2

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

/*Student structural morphology*/
struct Student {
    string name;
    int age;
    string sex;
};

/*"Imitative function. Specify sorting criteria for Student set*/
class studentSortCriterion {
public:
    bool operator() (const Student &a, const Student &b) const {
        /*First compare names; if the names are the same, then compare ages. Small return 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);
    /*Constructing a test Student shows that even though stuTemp and stu1 are not actually the same object,
    *But when searched in set, the search is still successful. This is because of the studentSortCriterion defined.
    */
    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;
}

Execution code

Marry
Cannot fine the student!

8. Multset - Basic Use

  • Allow storage of duplicate elements
  • Default ascending arrangement
#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;
    }
};
//functor
struct Soft {
    //Way 1: Don't write constants
    //    bool operator()(Student &left,Student &right){
    //        return left.getScore() < right.getScore();
    //    }
    //Mode 2: const Modification
    bool operator()(const Student &left, const Student &right) {
        //Type conversion
        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 << "Default ascending order" << endl;

    //Ascending order
    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 << "Use greater Descending order" << endl;
    //Descending order
    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 << "Custom Sorting" << endl;
    //Custom Sorting Method
    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;
}

Execution code

Default ascending order
10
10
20
30

Use greater descending
30
20
10
10

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

9.map - Basic Use

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

void main() {

    map<int, string> mp;

    cout << endl << "Mode 1: Insert data pair" << endl;
    //Mode 1: insert data 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++) {
        //Get key: it - > first
        cout << "key: " << it->first << endl;
        //Get value: it - > Second
        cout << "value: " << it->second.c_str() << endl;
    }

    cout << endl << "Mode 2: pair" << endl;

    //Mode 2: If the key exists, it will not be added and not covered. If it does not exist, it will be added.
    pair<map<int, string>::iterator, bool> result = mp.insert(map<int, string>::value_type(04, "Month"));
    if (result.second) {
        cout << "Add success"<< endl;
    }
    else {
        cout << "Already exist,Add failure!" << endl;
    }

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

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

    cout << endl << "Mode 4:" << endl;
    //Mode 4: If the key exists, repetitive addition will override it. If not, add it directly.
    mp[5] = "Ding";
    mp[6] = "Coco";

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

    getchar();
}

Execution code

Mode 1: Insert data pair
key: 1
value: Lucy
key: 2
value: Cookie
key: 3
value: Sun
key: 4
value: Jack

//Mode 2: pair
//Existing, add failed!
key: 1
value: Lucy
key: 2
value: Cookie
key: 3
value: Sun
key: 4
value: Jack

//Mode 3: make_pair
key: 1
value: Lucy
key: 2
value: Cookie
key: 3
value: Sun
key: 4
value: Jack
key: 5
value: Liu

//Mode 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 - Delete

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

void main() {

    map<int, string> mp;

    //Mode 1: insert data 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"));
    //delete
    map<int, string>::iterator it = mp.begin();
    mp.erase(it);

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

Execution code

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

11.map-lookup (similar to 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 = "Surplus";
    }
    else
    {
        cout << "Can't find" << endl;
    }


    // The function returns a pair of iterators. The first iterator points to the location of the first occurrence of the element being looked for.
    // The second iterator points to the latter location of the last occurrence of the element being searched
    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();
}

Execution code

Can't find
key: 2
value: Cookie
key: 3
value: Sun

12.multimap - one to many

Use scenario: A user corresponds to multiple orders

#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 << " Order number:" << this->num << "  Commodity:"<< this->name  << endl;
    }
};
void main() {



    multimap<string, Order> mst;
    mst.insert(make_pair("Jack", Order("Men's coat", 01)));
    mst.insert(make_pair("Jack", Order("Outdoor shoes", 02)));

    mst.insert(make_pair("Lucy", Order("Ladies coat", 03)));
    mst.insert(make_pair("Lucy", Order("Women's high heels",02)));

    mst.insert(make_pair("Rose", Order("Ladies' gauze", 03)));
    mst.insert(make_pair("Rose", Order("Ladies cloth shoes", 02)));
    mst.insert(make_pair("Rose", Order("Ladies coat", 02)));
    mst.insert(make_pair("Rose", Order("Ladies trousers", 02)));

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

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

        cout << endl << "Get only Lucy Order" << endl;

    //Get the number of orders
    int count = mst.count("Lucy");
    //Print "Dream" Orders: Find
    multimap<string, Order>::iterator it = mst.find("Lucy");
    //Cyclic traversal printing
    //count
    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();
}

Execution code

key: Jack
 Order No: 1 Commodity: Men's coat
key: Jack
 Order No: 2 Commodity: Outdoor Running Shoes
key: Lucy
 Order number: 3 commodities: ladies'coats
key: Lucy
 Order Number: 2 Commodities: Women's High-heeled Shoes
key: Rose
 Order Number: 3 Commodities: Ladies'Yarns
key: Rose
 Order No: 2 Goods: Ladies'Cloth Shoes
key: Rose
 Order number: 2 commodities: ladies'coats
key: Rose
 Order number: 2 commodities: ladies'pants

Get Lucy orders only
key: Lucy
 Order number: 3 commodities: ladies'coats
key: Lucy
 Order Number: 2 Commodities: Women's High-heeled Shoes

Special thanks:

Dream

Posted by brandonr on Fri, 14 Dec 2018 11:48:03 -0800