NDK 数据结构之队列与栈等的实现
NDK数据结构之队列与栈等的实现
com_tz_ndk_cpp_NDKCpp.h
/*DONOTEDITTHISFILE-itismachinegenerated*/ #include/*Headerforclasscom_tz_ndk_cpp_NDKCpp*/ #ifndef_Included_com_tz_ndk_cpp_NDKCpp #define_Included_com_tz_ndk_cpp_NDKCpp #ifdef__cplusplus extern"C"{ #endif /* *Class:com_tz_ndk_cpp_NDKCpp *Method:callCppTest *Signature:()V */ JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppQueue (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppQueuePriority (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppStack (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppList (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppListDelete (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppListInsert (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppSet (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppSetReverse (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppSetSort (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppSetFind (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppMultiSet (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppMap (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppMapDelete (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppMapFind (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppMultiMap (JNIEnv*,jobject); JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppVectorCopy (JNIEnv*,jobject); #ifdef__cplusplus } #endif #endif
com_tz_ndk_cpp_NDKCpp.cpp
#include#include #include #include"com_tz_ndk_cpp_NDKCpp.h" usingnamespacestd; //1.C++语言:queue队列-基本使用 #include JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppQueue (JNIEnv*,jobject){ //初始化 queue q; //添加元素 q.push('A'); q.push('B'); q.push('C'); //添加头部 //q.front()='z'; //添加尾部 //q.back()='D'; //删除操作 while(!q.empty()){ __android_log_print(ANDROID_LOG_INFO,"main","值:%c",q.front()); //删除 q.pop(); } } //2.C++语言:queue队列-优先级 JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppQueuePriority (JNIEnv*,jobject){ //2.1添加元素(默认是按照添加的顺序排列) //queue q; //q.push(10); //q.push(50); //q.push(20); //q.push(5); ////打印 //while(!q.empty()){ //__android_log_print(ANDROID_LOG_INFO,"main","值:%d",q.front()); //q.pop(); //} //2.2最大值优先级队列(从大到小排列) //priority_queue pq1; //pq1.push(10); //pq1.push(50); //pq1.push(20); //pq1.push(5); //while(!pq1.empty()){ //__android_log_print(ANDROID_LOG_INFO,"main","值:%d",pq1.top()); //pq1.pop(); //} //2.3最小值优先级队列 //注意:不同额编译器对语法检查有差别 //在AS中进行NDK开发>>符号认为运算符,所以为了避免出现这样的情况,请用空格分离'>>' priority_queue ,greater >pq1; pq1.push(10); pq1.push(50); pq1.push(20); pq1.push(5); while(!pq1.empty()){ __android_log_print(ANDROID_LOG_INFO,"main","值:%d",pq1.top()); pq1.pop(); } } //3.C++语言:stack栈-基本使用 #include JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppStack (JNIEnv*,jobject){ stack st; st.push(10); st.push(20); st.push(30); while(!st.empty()){ __android_log_print(ANDROID_LOG_INFO,"main","值:%d",st.top()); st.pop(); } } //4.C++语言:list-基本使用 #include JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppList (JNIEnv*,jobject){ 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 ::iteratorit=lt.begin();it!=lt.end();it++){ __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it); } list ::iteratorit=lt.begin(); //连续相加允许(++) //支持'++'、'--'运算符 it++; it--; //注意:不支持间断 //不支持'+'、'-'运算度 //it=it-1; } //5.C++语言:list-删除 JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppListDelete (JNIEnv*,jobject){ 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); //方式一 //list ::iteratorit=lt.begin(); //it++; ////删除:删除第二个元素 //lt.erase(it); //方式二 //删除第二个元素(直接根据内容删除) //lt.remove(20); //方式三:区间删除 //开始位置 list ::iteratorit_begin=lt.begin(); //结束位置 list ::iteratorit_end=lt.begin(); it_end++; it_end++; //删除元素(如果已经被删除的元素不能够在删除) lt.erase(it_begin,it_end); //循环遍历 for(list ::iteratorit=lt.begin();it!=lt.end();it++){ __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it); } } //6.C++语言:list-插入 JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppListInsert (JNIEnv*,jobject){ list lt; //从尾部添加 lt.push_back(40); lt.push_back(50); lt.push_back(60); //插入 lt.insert(lt.begin(),30); //循环遍历 for(list ::iteratorit=lt.begin();it!=lt.end();it++){ __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it); } } //7.C++语言:set-基本使用(元素唯一)-默认从小到大排列 //特点一:元素唯一 //特点二:默认从小到大排列 #include JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppSet (JNIEnv*,jobject){ set st; st.insert(40); st.insert(10); st.insert(30); st.insert(20); //删除 set ::iteratorit=st.begin(); st.erase(it); for(set ::iteratorit=st.begin();it!=st.end();it++){ __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it); } } //8.C++语言:set-基本使用(元素唯一)-从大到小排列 //set > JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppSetReverse (JNIEnv*,jobject){ set >st; st.insert(40); st.insert(10); st.insert(30); st.insert(20); for(set ::iteratorit=st.begin();it!=st.end();it++){ __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it); } } //9.C++语言:set-自定义排序规则 //需求:根据学生的成绩进行排序 classStudent{ private: char*name; intscore; public: Student(char*name,intscore){ this->name=name; this->score=score; } intgetScore(){ returnthis->score; } voidprintStudent(){ __android_log_print(ANDROID_LOG_INFO,"main","姓名:%s,成绩:%d",this->name,this->score); } }; //仿函数 structSoft{ //方式一:不写常量 //booloperator()(Student&left,Student&right){ //returnleft.getScore() (left); Studentstu_right=const_cast (right); returnstu_left.getScore() st; st.insert(Student("小宇",50)); st.insert(Student("梦想",59)); st.insert(Student("song",55)); st.insert(Student("远方",58)); st.insert(Student("石桥中化妖",40)); for(set ::iteratorit=st.begin();it!=st.end();it++){ Studentstu=const_cast (*it); stu.printStudent(); } } //10.C++语言:set-查找 JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppSetFind (JNIEnv*,jobject){ set st; st.insert(10); st.insert(20); st.insert(30); st.insert(40); st.insert(50); st.insert(60); st.insert(70); //方式一 //查找等于30元素 //st.find(2); //方式二 //查找等于或者小余35元素 //如果存在你输入的值30,那么就返回当前值30(例如:30) //如果不存在你查找的值31,那么返回大于31的最近的一个元素指针 set ::iteratorit_lower=st.lower_bound(31); __android_log_print(ANDROID_LOG_INFO,"main","查找结果:%d",*it_lower); //如果存在你查找的值30,那么就返回大于30最近的一个元素指针40 //如果不存在你查找的值31,那么就返回大于31最近的一个元素的指针40 set ::iteratorit_upper=st.upper_bound(31); __android_log_print(ANDROID_LOG_INFO,"main","查找结果:%d",*it_upper); //方式三:既要返回最小也要最大 pair ::iterator,set ::iterator>p=st.equal_range(30); //获取返回的元素 __android_log_print(ANDROID_LOG_INFO,"main","小:%d",*p.first); __android_log_print(ANDROID_LOG_INFO,"main","大:%d",*p.second); } //11.C++语言:multiset-基本使用 //允许存储重复元素 //默认升序排列 JNIEXPORTvoidJNICALLJava_com_tz_ndk_cpp_NDKCpp_callCppMultiSet (JNIEnv*,jobject){ //升序 //multiset mst; //mst.insert(10); //mst.insert(20); //mst.insert(30); //mst.insert(10); // //for(multiset ::iteratorit=mst.begin();it!=mst.end();it++){ //__android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it); //} //降序 //multiset >mst; //mst.insert(10); //mst.insert(20); //mst.insert(30); //mst.insert(10); // //for(multiset ::iteratorit=mst.begin();it!=mst.end();it++){ //__android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it); //} //自定义排序方式 multiset mst; mst.insert(Student("小宇",50)); mst.insert(Student("梦想",59)); mst.insert(Student("song",55)); mst.insert(Student("远方",58)); mst.insert(Student("石桥中化妖",40)); mst.insert(Student("Dream",40)); for(multiset ::iteratorit=mst.begin();it!=mst.end();it++){ Studentstu=const_cast (*it); stu.printStudent(); } } //12.C++语言:map-基本使用 #include
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。