Android JNI语法之--C++--list、set、map用法

本文介绍了Android JNI环境下,C++中list、set、map等容器的基本操作,包括添加、删除、查找等,并通过示例代码展示了其用法。例如,list支持++、--运算符,set默认元素唯一并按升序排列,map提供了多种插入和查找方法等。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓兼职framework应用工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值