Implementation of queue and stack of NDK data structure
- 2020-05-30 20:53:37
- OfStack
Implementation of queue and stack of NDK data structure
com_tz_ndk_cpp_NDKCpp.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_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
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppQueue
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppQueuePriority
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStack
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppList
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppListDelete
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppListInsert
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSet
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetReverse
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetSort
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetFind
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMultiSet
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMap
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMapDelete
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMapFind
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMultiMap
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorCopy
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
com_tz_ndk_cpp_NDKCpp.cpp
#include <iostream>
#include <string>
#include <android/log.h>
#include "com_tz_ndk_cpp_NDKCpp.h"
using namespace std;
//1.C++ Language: queue The queue - The basic use
#include <queue>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppQueue
(JNIEnv *, jobject){
// Initialize the
queue<char> q;
// Add elements
q.push('A');
q.push('B');
q.push('C');
// Add the head
// q.front() = 'z';
// Add a tail
// q.back() = 'D';
// Delete operation
while (!q.empty()){
__android_log_print(ANDROID_LOG_INFO,"main"," value : %c",q.front());
// delete
q.pop();
}
}
//2.C++ Language: queue The queue - priority
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppQueuePriority
(JNIEnv *, jobject){
//2.1 Add elements ( The default is in the order of addition )
// queue<int> q;
// q.push(10);
// q.push(50);
// q.push(20);
// q.push(5);
// // print
// while (!q.empty()){
// __android_log_print(ANDROID_LOG_INFO,"main"," value : %d",q.front());
// q.pop();
// }
//2.2 Maximum priority queue ( From big to small )
// priority_queue<int> pq1;
// pq1.push(10);
// pq1.push(50);
// pq1.push(20);
// pq1.push(5);
// while (!pq1.empty()){
// __android_log_print(ANDROID_LOG_INFO,"main"," value : %d",pq1.top());
// pq1.pop();
// }
//2.3 Minimum priority queue
// Note: different compilers have different syntax checks
// in AS In the NDK The development of >> The symbol considers the operator , So to avoid this, separate with Spaces '> >'
priority_queue<int,vector<int>,greater<int> > pq1;
pq1.push(10);
pq1.push(50);
pq1.push(20);
pq1.push(5);
while (!pq1.empty()){
__android_log_print(ANDROID_LOG_INFO,"main"," value : %d",pq1.top());
pq1.pop();
}
}
//3.C++ Language: stack The stack - The basic use
#include <stack>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStack
(JNIEnv *, jobject){
stack<int> st;
st.push(10);
st.push(20);
st.push(30);
while (!st.empty()){
__android_log_print(ANDROID_LOG_INFO,"main"," value : %d",st.top());
st.pop();
}
}
//4.C++ Language: list- The basic use
#include <list>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppList
(JNIEnv *, jobject){
list<int> lt;
// Add from the head
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);
// To iterate over
for (list<int>::iterator it = lt.begin() ; it != lt.end() ; it++){
__android_log_print(ANDROID_LOG_INFO,"main"," Value: %d",*it);
}
list<int>::iterator it = lt.begin();
// Continuous addition allowed (++)
// support '++' , '--' The operator
it++;
it--;
// Note: unsupported discontinuous
// Does not support '+' , '-' Computing degree
// it = it - 1;
}
//5.C++ Language: list- delete
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppListDelete
(JNIEnv *, jobject){
list<int> lt;
// Add from the head
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);
// way 1
// list<int>::iterator it = lt.begin();
// it++;
// // Delete: delete the first 2 An element
// lt.erase(it);
// way 2
// Delete the first 2 An element ( Delete directly based on content )
// lt.remove(20);
// way 3: Interval to delete
// The starting position
list<int>::iterator it_begin = lt.begin();
// End position
list<int>::iterator it_end = lt.begin();
it_end++;
it_end++;
// Remove elements ( If an element that has been deleted cannot be deleted again )
lt.erase(it_begin,it_end);
// To iterate over
for (list<int>::iterator it = lt.begin() ; it != lt.end() ; it++){
__android_log_print(ANDROID_LOG_INFO,"main"," Value: %d",*it);
}
}
//6.C++ Language: list- insert
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppListInsert
(JNIEnv *, jobject){
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);
// To iterate over
for (list<int>::iterator it = lt.begin() ; it != lt.end() ; it++){
__android_log_print(ANDROID_LOG_INFO,"main"," Value: %d",*it);
}
}
//7.C++ Language: set- The basic use ( Elements only 1)- Default from small to large
// The characteristics of 1 : the element is 1
// The characteristics of 2 : default from small to large
#include <set>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSet
(JNIEnv *, jobject){
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++){
__android_log_print(ANDROID_LOG_INFO,"main"," Value: %d",*it);
}
}
//8.C++ Language: set- The basic use ( Elements only 1)- From big to small
//set<int,greater<int>>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetReverse
(JNIEnv *, jobject){
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++){
__android_log_print(ANDROID_LOG_INFO,"main"," Value: %d",*it);
}
}
//9.C++ Language: set- Custom collation
// Requirements: rank students according to their grades
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(){
__android_log_print(ANDROID_LOG_INFO,"main"," The name : %s, results : %d",this->name,this->score);
}
};
// Copy function
struct Soft{
// way 1 : no constants
// bool operator()(Student &left,Student &right){
// return left.getScore() < right.getScore();
// }
// way 2 : const modified
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();
}
};
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetSort
(JNIEnv *, jobject){
set<Student,Soft> st;
st.insert(Student(" xiaoyu ",50));
st.insert(Student(" dream ",59));
st.insert(Student("song",55));
st.insert(Student(" The distance ",58));
st.insert(Student(" The stone bridge monster ",40));
for (set<Student>::iterator it = st.begin() ; it != st.end() ; it++){
Student stu = const_cast<Student&>(*it);
stu.printStudent();
}
}
//10.C++ Language: set- To find the
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetFind
(JNIEnv *, jobject){
set<int> st;
st.insert(10);
st.insert(20);
st.insert(30);
st.insert(40);
st.insert(50);
st.insert(60);
st.insert(70);
// way 1
// Look for equal 30 The element
//st.find(2);
// way 2
// I'm going to look for alpha or beta 35 The element
// If there is a value that you enter 30 , then return the current value 30( Such as: 30)
// If the value you are looking for does not exist 31 , then return greater than 31 The most recent 1 Element pointer
set<int>::iterator it_lower = st.lower_bound(31);
__android_log_print(ANDROID_LOG_INFO,"main"," Search results : %d",*it_lower);
// If there is a value you are looking for 30, So it returns greater than 30 The recent 1 Element pointer 40
// If the value you are looking for does not exist 31 , then return greater than 31 The recent 1 A pointer to an element 40
set<int>::iterator it_upper = st.upper_bound(31);
__android_log_print(ANDROID_LOG_INFO,"main"," Search results : %d",*it_upper);
// way 3: You want to return both the minimum and the maximum
pair<set<int>::iterator,set<int>::iterator> p = st.equal_range(30);
// Gets the returned element
__android_log_print(ANDROID_LOG_INFO,"main"," small : %d",*p.first);
__android_log_print(ANDROID_LOG_INFO,"main"," big : %d",*p.second);
}
//11.C++ Language: multiset- The basic use
// Allows you to store duplicate elements
// Default ascending order
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMultiSet
(JNIEnv *, jobject){
// ascending
// 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++){
// __android_log_print(ANDROID_LOG_INFO,"main"," value : %d",*it);
// }
// Descending order
// multiset<int,greater<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++){
// __android_log_print(ANDROID_LOG_INFO,"main"," value : %d",*it);
// }
// Custom sorting
multiset<Student,Soft> mst;
mst.insert(Student(" xiaoyu ",50));
mst.insert(Student(" dream ",59));
mst.insert(Student("song",55));
mst.insert(Student(" The distance ",58));
mst.insert(Student(" The stone bridge monster ",40));
mst.insert(Student("Dream",40));
for (multiset<Student>::iterator it = mst.begin() ; it != mst.end() ; it++){
Student stu = const_cast<Student&>(*it);
stu.printStudent();
}
}
//12.C++ Language: map- The basic use
#include <map>
#include <string>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMap
(JNIEnv *, jobject){
map<int,string> mp;
// way 1 : insert data pair
mp.insert(pair<int,string>(01," Chen disillusioned "));
mp.insert(pair<int,string>(02,"Mr.Sunday"));
mp.insert(pair<int,string>(03,"Studio"));
mp.insert(pair<int,string>(04," 余祚宁 "));
// way 2 If: key Exists, then do not add and do not overwrite, if does not exist, then add
pair<map<int,string>::iterator, bool> result = mp.insert(map<int,string>::value_type(04," meet 98"));
if(result.second){
__android_log_print(ANDROID_LOG_INFO,"main"," Add a success !");
}else{
__android_log_print(ANDROID_LOG_INFO,"main"," existing , Add failure !");
}
// way 3 :
mp.insert(make_pair(05," Set the set "));
// way 4 If: key Existing, repeated addition will overwrite, if it does not exist, then add directly
mp[5] = " The stone bridge monster ";
mp[6] = " Set the set ";
for (map<int,string>::iterator it = mp.begin() ; it != mp.end() ; it++){
// To obtain key:it->first
__android_log_print(ANDROID_LOG_INFO,"main","key: %d",it->first);
// To obtain value:it->second
__android_log_print(ANDROID_LOG_INFO,"main","value: %s",it->second.c_str());
}
}
//13.C++ Language: map- delete
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMapDelete
(JNIEnv *, jobject){
map<int,string> mp;
mp.insert(pair<int,string>(01," Chen disillusioned "));
mp.insert(pair<int,string>(02,"Mr.Sunday"));
mp.insert(pair<int,string>(03,"Studio"));
mp.insert(pair<int,string>(04," 余祚宁 "));
// delete
map<int,string>::iterator it = mp.begin();
mp.erase(it);
// print
for (map<int,string>::iterator it = mp.begin() ; it != mp.end() ; it++){
// To obtain key:it->first
__android_log_print(ANDROID_LOG_INFO,"main","key: %d",it->first);
// To obtain value:it->second
__android_log_print(ANDROID_LOG_INFO,"main","value: %s",it->second.c_str());
}
}
//14.C++ Language: map- To find the (equal_range)
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMapFind
(JNIEnv *, jobject){
map<int,string> mp;
mp.insert(pair<int,string>(01," Chen disillusioned "));
mp.insert(pair<int,string>(02,"Mr.Sunday"));
mp.insert(pair<int,string>(03,"Studio"));
mp.insert(pair<int,string>(04," 余祚宁 "));
// Get greater than or equal to 2 The elements of the
pair<map<int,string>::iterator,map<int,string>::iterator> p = mp.equal_range(2);
// Determine if an element exists
if(p.first != mp.end()){
// Is equal to the 2
// To obtain key:it->first
__android_log_print(ANDROID_LOG_INFO,"main","key: %d",p.first->first);
// To obtain value:it->second
__android_log_print(ANDROID_LOG_INFO,"main","value: %s",p.first->second.c_str());
// Is greater than 2 The element
// To obtain key:it->first
__android_log_print(ANDROID_LOG_INFO,"main","key: %d",p.second->first);
// To obtain value:it->second
__android_log_print(ANDROID_LOG_INFO,"main","value: %s",p.second->second.c_str());
}
}
//15.C++ Language: multimap-1 For more than
// Requirements: 1 Each user corresponds to multiple orders
class Order{
private:
char* name;
int num;
public:
Order(char* name,int num){
this->name = name;
this->num = num;
}
void printOrder(){
__android_log_print(ANDROID_LOG_INFO,"main"," Order name: %s, The order no. : %d",this->name,this->num);
}
};
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMultiMap
(JNIEnv *, jobject){
multimap<string,Order> mst;
mst.insert(make_pair(" Chen disillusioned ",Order(" Men's jacket ",01)));
mst.insert(make_pair(" Chen disillusioned ",Order(" Outdoor shoes ",02)));
mst.insert(make_pair(" dream ",Order(" Ms coat ",03)));
mst.insert(make_pair(" dream ",Order(" High heels for ladies ",02)));
mst.insert(make_pair("Dream",Order(" Ms gauze clothing ",03)));
mst.insert(make_pair("Dream",Order(" Ms cloth shoes ",02)));
mst.insert(make_pair("Dream",Order(" Ms coat ",02)));
mst.insert(make_pair("Dream",Order(" Ms pants ",02)));
// traverse
// for (multimap<string,Order>::iterator it = mst.begin() ; it != mst.end() ; it++){
// // To obtain key:it->first
// __android_log_print(ANDROID_LOG_INFO,"main","key: %s",it->first.c_str());
// // To obtain value:it->second
// Order order = const_cast<Order&>(it->second);
// order.printOrder();
// }
// Requirements: just get " dream " The order
// Get the quantity of the order
int count = mst.count(" dream ");
// print " dream " The order : find
multimap<string,Order>::iterator it = mst.find(" dream ");
// Loop through the print
// count
int i = 0;
while (it != mst.end() && i < count){
__android_log_print(ANDROID_LOG_INFO,"main","key: %s",it->first.c_str());
Order order = const_cast<Order&>(it->second);
order.printOrder();
i++;
it++;
}
}
//16.C++ Language: vector- Shallow copy and deep copy
class User{
private:
char* name;
int age;
public:
// Shallow copy ( The default is shallow copy )
User(char* name,int age){
// Dynamic allocation of memory
this->name = new char[strlen(name)+1];
strcpy(this->name,name);
this->age = age;
}
~User(){
if(this->name != NULL){
delete[] this->name;
this->name = NULL;
this->age = 0;
}
}
void printUser(){
__android_log_print(ANDROID_LOG_INFO,"main"," Name: %s, age : %d",this->name,this->age);
}
// Deep copy
User(const User &user){
// Free memory first
if(this->name != NULL){
delete[] this->name;
this->name = NULL;
this->age = 0;
}
// Dynamic allocation of memory
this->name = new char[strlen(user.name)+1];
strcpy(this->name,user.name);
this->age = user.age;
}
User& operator=(User &user){
if(this->name != NULL){
delete[] this->name;
this->name = NULL;
this->age = 0;
}
// Dynamic allocation of memory
this->name = new char[strlen(user.name)+1];
strcpy(this->name,user.name);
this->age = user.age;
return *this;
}
};
//class User{
//private:
// string* name;
// int age;
//public:
// // Shallow copy ( The default is shallow copy )
// User(string name,int age){
// // Dynamic allocation of memory
// this->name = new string(const_cast<string&>(name));
// this->age = age;
// }
// ~User(){
// if(this->name != NULL){
// delete this->name;
// this->name = NULL;
// this->age = 0;
// }
// }
// void printUser(){
// __android_log_print(ANDROID_LOG_INFO,"main"," Name: %s, age : %d",this->name,this->age);
// }
//
// // Deep copy
// User(const User &user){
// // Free memory first
// if(this->name != NULL){
// delete this->name;
// this->name = NULL;
// this->age = 0;
// }
// // Dynamic allocation of memory
// this->name = new string(const_cast<string&>(user.name));
// this->age = age;
// }
//
// User& operator=(User &user){
// if(this->name != NULL){
// delete this->name;
// this->name = NULL;
// this->age = 0;
// }
// // Dynamic allocation of memory
// this->name = new string(const_cast<string&>(user.name));
// this->age = age;
// return *this;
// }
//
////};
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorCopy
(JNIEnv *, jobject){
vector<User> vt;
User user(" The stone bridge monster ",18);
// As a formal parameter, the copy function is called, and the default is shallow copy
vt.push_back(user);
// Solution: deep copy
for (vector<User>::iterator it = vt.begin() ; it != vt.end() ; it++){
User order = const_cast<User&>(*it);
order.printUser();
}
// Homework: string How to copy deep ( A bit of trouble 1 point )
}
If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!