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: callCppStringInit
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringInit
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppStringTraversing
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringTraversing
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppStringToChar
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringToChar
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppStringFind
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringFind
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppStringReplace
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringReplace
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppStringDelete
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringDelete
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppStringInsert
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringInsert
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppStringUpperAndLower
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringUpperAndLower
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppVectorInit
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorInit
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppVectorUpAndDel
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorUpAndDel
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppVectorIterator
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorIterator
(JNIEnv *, jobject);
/*
* Class: com_tz_ndk_cpp_NDKCpp
* Method: callCppDeque
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppDeque
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
com_tz_ndk_cpp_NDKCpp.cpp
//
// Created by Dream on 2016/11/17.
//
#include <iostream>
#include <android/log.h>
#include "com_tz_ndk_cpp_NDKCpp.h"
using namespace std;
// 01.C++语言:string类-初始化
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringInit
(JNIEnv *, jobject){
//方式一
// string str = "Dream";
//方式二
// string str = string("Dream");
// string str("Dream");
//方式三
// string str1 = "Dream";
// string str2 = str1;
//方式四
// string str1 = "Dream";
// string str2(str1);
//方式五:连续拼接6个'A'
// string str(6,'A');
//结果: str = "AAAAAA"
//方式六:指针方式
// string* str = new string("Dream");
//方式七:设置为NULL
//在C++中string是一个类,底层实现也是C语言中的char*
// string str = NULL;
}
// 02.C++语言:string类-遍历
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringTraversing
(JNIEnv *, jobject){
string str = "Dream";
//方式一:数组遍历(在AS中越界不会报错,内部做了处理)
// for (int i = 0; i < str.length() + 10000; ++i) {
// //内部做了处理,如果下标越界,那么返回空'\0'
// char c = str[i];
// __android_log_print(ANDROID_LOG_INFO,"main","第%d值: %c",i,c);
// }
//方式二:迭代器
// int i = 0;
// for (string::iterator it = str.begin(); it != str.end();it++){
// char c = *it;
// __android_log_print(ANDROID_LOG_INFO,"main","第%d值: %c",i,c);
// i++;
// }
//方式三:at函数遍历(越界,抛异常)
//在Java中charAt(i),C++中at(i)
//捕获异常:兼容Android NDK做一些配置
//在Android NDK开发中r5之前默认不支持C++中的try catch
//但是在Android NDK R5开始支持C++中的try catch
//但是Android NDK编译器默认异常控制特效关闭状态,所以手动的打开异常控制
//打开配置:我们需要在gradle配置文件中配置(cppFlags = "-fexceptions")即可
try {
for (int i = 0; i < str.length() + 3; ++i) {
char c = str.at(i);
__android_log_print(ANDROID_LOG_INFO,"main","第%d值: %c",i,c);
}
}catch (...){
__android_log_print(ANDROID_LOG_INFO,"main","发生了异常......");
}
}
// 03.C++语言:string类和char之间的转换
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringToChar
(JNIEnv *, jobject){
//3.1 string转成char*
// string str = "Dream";
// const char* crr = str.c_str();
// __android_log_print(ANDROID_LOG_INFO,"main","值: %s",crr);
//3.2 string转成char[]数组
string str = "Dream";
char crr[20] = {0};
//第一个参数:拷贝目标
//第二个参数:拷贝的数量
//第三个参数:从哪里开始
str.copy(crr,str.length(),0);
__android_log_print(ANDROID_LOG_INFO,"main","值: %s",crr);
}
// 04.C++语言:string类-字符串查找
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringFind
(JNIEnv *, jobject){
//4.1 查找指定的字符串:find(顺时针)
// string str = "Dream a Dream";
// //参数一:查找条件
// //参数二:从那个位置开始查找,默认是0开始
// int index = str.find("Dream",0);
// __android_log_print(ANDROID_LOG_INFO,"main","值: %d",index);
//4.2 查找最后一个字符串:find_last_of(逆时针)
// string str = "Dream a Dream";
// int index = str.find_last_of("Dream",str.length());
// __android_log_print(ANDROID_LOG_INFO,"main","值: %d",index);
//4.3 查找某个字符串出现的次数
string str = "Dream a Dream";
int index = str.find("Dream",0);
//计数
int count = 0;
//string::npos默认值是:-1
//判断是否存在
while (index != string::npos){
count++;
index = index + 5;
index = str.find("Dream",index);
}
__android_log_print(ANDROID_LOG_INFO,"main","次数: %d",count);
}
// 05.C++语言:string类-字符串替换
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringReplace
(JNIEnv *, jobject){
//5.1 替换一个
// string str = "Dream a Dream";
// str.replace(0,5,"Hello");
// __android_log_print(ANDROID_LOG_INFO,"main","值: %s",str.c_str());
//5.2 替换所有
string str = "Dream a Dream";
int index = str.find("Dream",0);
while (index != string::npos){
str.replace(index,5,"Hello");
index = index + 5;
//继续往后查询
index = str.find("Dream",index);
}
__android_log_print(ANDROID_LOG_INFO,"main","值: %s",str.c_str());
}
// 06.C++语言:string类-字符串删除
//STL (Standard Template Library)标准模版库(C++中)
//类似于java.lang.util等等......
#include <algorithm>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringDelete
(JNIEnv *, jobject){
//6.1 删除单个erase函数
// string str = "Dream a Dream";
// //可以erase(0)代表删除整个字符串,因为删除数量number默认是字符串的长度
// //erase()也是干掉
// //参数有默认值
str.erase();
// //正确写法
// string::iterator index = find(str.begin(),str.end(),'D');
// str.erase(index);
// __android_log_print(ANDROID_LOG_INFO,"main","值: %s",str.c_str());
//6.2 删除多个
string str = "Dream a Dream";
//只一个指针
string::iterator index = find(str.begin(),str.end(),'D');
str.erase(index);
while (index != str.end()){
if(*index == 'D'){
str.erase(index);
}
//指针位移
index++;
}
__android_log_print(ANDROID_LOG_INFO,"main","值: %s",str.c_str());
}
// 07.C++语言:string类-字符串插入
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringInsert
(JNIEnv *, jobject){
//从头开始插入
// string str = "Dream a Dream";
// str.insert(0,"I have ");
// __android_log_print(ANDROID_LOG_INFO,"main","值: %s",str.c_str());
//从最后开始插入
string str = "Dream a Dream";
str.insert(str.length(),"I have ");
__android_log_print(ANDROID_LOG_INFO,"main","值: %s",str.c_str());
}
// 08.C++语言:string类-大小写转换
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStringUpperAndLower
(JNIEnv *, jobject){
//8.1 转小写
// string str = "Dream a Dream";
//参数一:从那一个位置开始
//参数二:到那一个位置结束
//参数三:目标对象
//参数四:格式(大写、小写)
//注意:参数四加'::'
//因为:在全局命名空间中这个tolower不是宏定义,
// 而是一个具体有实现的函数,所以需要明确命名空间
//在不同编译环境下有差别
//例如:在VS 2013中不需要"::",但是在Android NDK环境下需要加"::"
// transform(str.begin(),str.end(),str.begin(),::tolower);
// __android_log_print(ANDROID_LOG_INFO,"main","值: %s",str.c_str());
//8.2 转大写
string str = "Dream a Dream";
transform(str.begin(),str.end(),str.begin(),::toupper);
__android_log_print(ANDROID_LOG_INFO,"main","值: %s",str.c_str());
}
// 09.C++语言:vector类(容器)-初始化-添加和插入
#include <vector>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorInit
(JNIEnv *, jobject){
//方式一
vector<char> vt;
vt.push_back('A');
vt.push_back('B');
vt.push_back('C');
//方式二
// vector<char> vt1 = vt;
//方式三:复制(指定复制多少内容)
//以下复制所有
// vector<char> vt2(vt.begin(),vt.end());
//指定复制内容(复制一部分)
// vector<char> vt2(vt.begin(),vt.begin() + 2);
//遍历容器
// for (int i = 0; i < vt.size(); ++i) {
// __android_log_print(ANDROID_LOG_INFO,"main","值: %c",vt[i]);
// }
//插入内容
vt.insert(vt.end(),'D');
for (int i = 0; i < vt.size(); ++i) {
__android_log_print(ANDROID_LOG_INFO,"main","值: %c",vt[i]);
}
}
// 10.C++语言:vector类(容器)-修改和删除
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorUpAndDel
(JNIEnv *, jobject){
vector<char> vt;
vt.push_back('A');
vt.push_back('B');
vt.push_back('C');
//10.1 修改
// 访问头部
// vt.front() = 'a';
// //访问尾部
// vt.back() = 'c';
// for (int i = 0; i < vt.size(); ++i) {
// __android_log_print(ANDROID_LOG_INFO,"main","值: %c",vt[i]);
// }
//采用下标的方式修改
// vt[0] = 'a';
// vt[vt.size()-1] = 'c';
// for (int i = 0; i < vt.size(); ++i) {
// __android_log_print(ANDROID_LOG_INFO,"main","值: %c",vt[i]);
// }
//10.2 删除元素
//删除尾部
// vt.pop_back();
//删除所有
// while (vt.size() > 0){
// vt.pop_back();
// }
// for (int i = 0; i < vt.size(); ++i) {
// __android_log_print(ANDROID_LOG_INFO,"main","值: %c",vt[i]);
// }
//指定删除元素
// vt.erase(vt.begin()+1);
// for (int i = 0; i < vt.size(); ++i) {
// __android_log_print(ANDROID_LOG_INFO,"main","值: %c",vt[i]);
// }
//指定区间删除
vt.erase(vt.begin(),vt.begin()+2);
for (int i = 0; i < vt.size(); ++i) {
__android_log_print(ANDROID_LOG_INFO,"main","值: %c",vt[i]);
}
}
// 11.C++语言:vector类(容器)-遍历
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorIterator
(JNIEnv *, jobject){
vector<char> vt;
vt.push_back('A');
vt.push_back('B');
vt.push_back('C');
//方式一
// for (int i = 0; i < vt.size(); ++i) {
// __android_log_print(ANDROID_LOG_INFO,"main","值: %c",vt[i]);
// }
//方式二:迭代器遍历-正向
// for (vector<char>::iterator it = vt.begin();it < vt.end();it++){
// __android_log_print(ANDROID_LOG_INFO,"main","值: %c",*it);
// }
//方式三:迭代器遍历-逆向
// for (vector<char>::reverse_iterator it = vt.rbegin();it < vt.rend();it++){
// __android_log_print(ANDROID_LOG_INFO,"main","值: %c",*it);
// }
//循环删除(指定删除元素)
for (vector<char>::iterator it = vt.begin();it < vt.end();it++){
if(*it == 'A'){
vt.erase(it);
}
}
for (vector<char>::iterator it = vt.begin();it < vt.end();it++){
__android_log_print(ANDROID_LOG_INFO,"main","值: %c",*it);
}
}
// 12.C++语言:deque类(双向队列)
#include <deque>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppDeque
(JNIEnv *, jobject){
deque<char> dq;
//12.1 添加元素
//头部添加
dq.push_front('A');
dq.push_front('B');
dq.push_front('C');
//尾部添加
dq.push_back('D');
dq.push_back('E');
dq.push_back('F');
//修改值
// dq.back() = 'z';
// dq.front() = 'a';
for (deque<char>::iterator it = dq.begin();it < dq.end();it++){
__android_log_print(ANDROID_LOG_INFO,"main","值: %c",*it);
}
//
// //12.2 删除
// //删除第一个
// dq.pop_front();
// //删除最后一个
// dq.pop_back();
//12.3 查找一个元素位置(查找'D')
//首先获取元素下标指针
deque<char>::iterator p_index = find(dq.begin(),dq.end(),'D');
if(p_index != dq.end()){
//这个方法就是计算两个指针直接的距离
int index = distance(dq.begin(),p_index);
__android_log_print(ANDROID_LOG_INFO,"main","值: %d",index);
}
}
整理自示例代码