Android NDK开发之旅 目录

1.list-基本使用

#include #include using namespace std;void main() {    list 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::iterator it = lt.begin(); it != lt.end(); it++) {        cout << *it << endl;    }    list::iterator it = lt.begin();    //连续相加允许(++)    //支持'++'、'--'运算符    it++;    it--;    cout << endl << "支持++、--运算符" << endl;    cout << *it << endl;    //注意:不支持间断    //不支持'+'、'-'运算度    // it = it - 1;  错误调用    getchar();}

执行代码

302010405060支持++、--运算符30

2.list-删除

#include #include using namespace std;void main() {    list 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::iterator it= lt.begin();        it++;        //删除:删除第二个元素        lt.erase(it);        //循环遍历        for (list::iterator it = lt.begin(); it != lt.end(); it++) {            cout << *it << endl;        }        cout << endl << "删除方式2:直接根据内容删除" << endl;    //删除方式2    //直接根据内容删除   lt.remove(30);   //循环遍历   for (list::iterator it = lt.begin(); it != lt.end(); it++) {       cout << *it << endl;   }   cout << endl << "删除方式3:区间删除" << endl;    //"删除方式3:区间删除    //开始位置    list::iterator it_begin = lt.begin();    //结束位置    list::iterator it_end = lt.begin();    it_end++;    it_end++;    //删除元素(如果已经被删除的元素不能够在删除)    lt.erase(it_begin, it_end);    //循环遍历    for (list::iterator it = lt.begin(); it != lt.end(); it++) {            cout << *it << endl;    }    getchar();}

执行代码

删除方式1:根据位置删除3010405060删除方式2:直接根据内容删除10405060删除方式3:区间删除5060

3.list-插入

#include #include using namespace std;void main() {    list lt;    //从尾部添加    lt.push_back(40);    lt.push_back(50);    lt.push_back(60);    //插入    lt.insert(lt.begin(), 30);    //循环遍历    for (list::iterator it = lt.begin(); it != lt.end(); it++) {        cout << *it << endl;    }    getchar();}

执行代码

30405060

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

#include #include using namespace std;void main() {    set st;    st.insert(40);    st.insert(10);    st.insert(30);    st.insert(20);    //删除    set::iterator it = st.begin();    st.erase(it);    for (set::iterator it = st.begin(); it != st.end(); it++) {        cout << *it << endl;    }    getchar();}

执行代码

203040

5set-从大到小排列

#include #include #include  using namespace std;void main() {    set > st;    st.insert(40);    st.insert(10);    st.insert(30);    st.insert(20);    for (set::iterator it = st.begin(); it != st.end(); it++) {            cout << *it << endl;    }    getchar();}

执行代码

40302010

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

#include #include #include  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(left);        Student stu_right = const_cast(right);        return stu_left.getScore() > stu_right.getScore();    }};void main() {    set 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::iterator it = st.begin(); it != st.end(); it++) {        Student stu = const_cast(*it);        stu.printStudent();    }    getchar();}

执行代码

name:Jack  score:96name:Music  score:88name:Song  score:77name:Pi  score:63name: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   #include   #include  using namespace std;typedef set 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   #include   #include  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 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::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::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;}

执行代码

MarryCannot fine the student!

8.multiset-基本使用

  • 允许存储重复元素
  • 默认升序排列
#include #include #include  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(left);        Student stu_right = const_cast(right);        return stu_left.getScore() > stu_right.getScore();    }};void main() {    cout << endl << "默认升序" << endl;    //升序    multiset mst;    mst.insert(10);    mst.insert(20);    mst.insert(30);    mst.insert(10);    for (multiset::iterator it = mst.begin(); it != mst.end(); it++) {        cout << *it << endl;    }    cout << endl << "使用greater降序" << endl;    //降序    multiset > mst2;    mst2.insert(10);    mst2.insert(20);    mst2.insert(30);    mst2.insert(10);    for (multiset::iterator it = mst2.begin(); it != mst2.end(); it++) {        cout << *it << endl;    }    cout << endl << "自定义排序" << endl;    //自定义排序方式    multiset 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::iterator it = mst3.begin(); it != mst3.end(); it++) {        Student stu = const_cast(*it);        stu.printStudent();    }    getchar();    return;}

执行代码

默认升序10102030使用greater降序30201010自定义排序name:Jack  score:96name:Music  score:88name:Song  score:77name:Pi  score:63name:Lucy  score:56

9.map-基本使用

#include #include #include #include  using namespace std;void main() {    map mp;    cout << endl << "方式1:插入数据pair" << endl;    //方式1:插入数据pair    mp.insert(pair(01, "Lucy"));    mp.insert(pair(02, "Cookie"));    mp.insert(pair(03, "Sun"));    mp.insert(pair(04, "Jack"));    for (map::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::iterator, bool> result = mp.insert(map::value_type(04, "Month"));    if (result.second) {        cout << "添加成功"<< endl;    }    else {        cout << "已存在,添加失败!" << endl;    }    for (map::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::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::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:插入数据pairkey:1value:Lucykey:2value:Cookiekey:3value:Sunkey:4value:Jack方式2:pair已存在,添加失败!key:1value:Lucykey:2value:Cookiekey:3value:Sunkey:4value:Jack方式3:make_pairkey:1value:Lucykey:2value:Cookiekey:3value:Sunkey:4value:Jackkey:5value:Liu方式4:key:1value:Lucykey:2value:Cookiekey:3value:Sunkey:4value:Jackkey:5value:Dingkey:6value:Coco

10.map-删除

#include #include #include #include  using namespace std;void main() {    map mp;    //方式1:插入数据pair    mp.insert(pair(01, "Lucy"));    mp.insert(pair(02, "Cookie"));    mp.insert(pair(03, "Sun"));    mp.insert(pair(04, "Jack"));    //删除    map::iterator it = mp.begin();    mp.erase(it);    for (map::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:2value:Cookiekey:3value:Sunkey:4value:Jack

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

#include #include #include #include  using namespace std;void main() {    map mp;    mp.insert(pair(01, "Lucy"));    mp.insert(pair(02, "Cookie"));    mp.insert(pair(03, "Sun"));    mp.insert(pair(04, "Jack"));    map::iterator it;    map::iterator flag = mp.end();    it = mp.find(5);    if (it != flag)    {        (*it).second = "剩余";    }    else    {        cout << "没有找到" << endl;    }    // 该函数返回的是一对迭代器,第一个迭代器指向所查找元素的第一次出现的位置,    // 第二个迭代器指向所查找元素最后一次出现位置的后一个位置    pair::iterator, map::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: 2value: Cookiekey: 3value: Sun

12.multimap-一对多

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

#include #include #include #include  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 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::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(it->second);            order.printOrder();        }        cout << endl << "只获取Lucy订单" << endl;    //获取订单的数量    int count = mst.count("Lucy");    //打印"梦想"订单:找到    multimap::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(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

更多相关文章

  1. Android打包剔除指定权限
  2. getReadableDatabase() 和 getWritableDatabase()
  3. ubuntu android 环境变量的配置
  4. Android中取消系统标题栏的几种方式
  5. Android常用的多渠道打包方式整理(不断更新...)
  6. android半透叠加对照表
  7. Python +appium 封装desired_caps模块
  8. Android实现学生管理系统
  9. Android(安卓)静默安装的几种方式

随机推荐

  1. 带有括号的某些字符串导致Ajax POST操作
  2. inarray(),如何正确使用它?
  3. 3.29 学前端 jquery之操作元素之属性操作
  4. iPad上的jQuery点击事件无法正常工作
  5. 如何使用jQuery UI Datepicker作为Django
  6. 如何使用带有“IF”条件的按钮改变DIV的
  7. jQuery返回一个没有逗号的字符串的前5个
  8. 超好用的一个JQUERY分页器
  9. Jssor滑块不适用于AngularJS
  10. 如何使用javascript/jquery获取文本框数