Android NDK开发之旅 目录

1.string类-初始化

#include //cout输出 str 引用sstream  Android里可以省略此引用  使用#include #include using namespace std;void main() {    //方式一    string str1 = "aaaa";    cout << "str1:"<< str1 << endl;    //方式二    string str21 = string("bbbba");    string str22("bbbbb");    cout << "str21:" << str21 << endl;    cout << "str21:" << str22 << endl;    //方式三    string str = "ccccc";    string str3 = str;    cout << "str3:" << str3 << endl;    //方式四    string str40 = "ddddd";    string str4(str40);    cout << "str4:" << str4 << endl;    //方式五:连续拼接6个'e'    string str5(6, 'e');    cout << "str5:" << str5 << endl;    //方式六:指针方式    string* str6 = new string("fffff");    cout << "str6:" << *str6 << endl;    //方式七:设置为NULL    //在C++中string是一个类,底层实现也是C语言中的char*    string*  str7 = NULL; //注意string  str7 = NULL 报错;     getchar();}

执行输出:

str1:aaaastr21:bbbbastr21:bbbbbstr3:cccccstr4:dddddstr5:eeeeeestr6:fffff

2.string类-遍历

#include   #include //cout输出 str 引用sstream  Android里可以省略此引用  使用#include #include using namespace std;void main() {    string str = "Android";    cout << "数组遍历方式" << endl;    //方式一:数组遍历(在AS中越界不会报错,内部做了处理)    for (int i = 0; i < str.length() ; ++i) {        //内部做了处理,如果下标越界,那么返回空'\0'        char c = str[i];            cout << "第" << i << "值:" << c << endl;    }    cout << endl << "迭代器方式"  << endl;    //方式二:迭代器    int i = 0;    for (string::iterator it = str.begin(); it != str.end(); it++) {        char c = *it;        cout << "第" << i << "值:" << c << endl;        i++;    }    cout << endl << "at函数遍历方式" << endl;    //方式三: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);            cout << "第" << i << "值:" << c << endl ;        }    }    catch (...) {        cout << "发生了异常......" << endl;    }    getchar();}

执行输出:

数组遍历方式第0值:A第1值:n第2值:d第3值:r第4值:o第5值:i第6值:d迭代器方式第0值:A第1值:n第2值:d第3值:r第4值:o第5值:i第6值:dat函数遍历方式第0值:A第1值:n第2值:d第3值:r第4值:o第5值:i第6值:d发生了异常......

3.string类和char之间的转换

#include   #include //cout输出 str 引用sstream  Android里可以省略此引用  使用#include #include using namespace std;void main() {    //3.1 string转成char*    string str = "Android";    const char* crr1 = str.c_str();    cout << crr1 << endl;    //3.2 string转成char[]数组    char crr2[20] = { 0 };    //第一个参数:拷贝目标    //第二个参数:拷贝的数量    //第三个参数:从哪里开始    str.copy(crr2, str.length(), 0);    cout << crr2 << endl;}

执行输出:

AndroidAndroid

4.string类-字符串查找

#include //cout输出 str 引用sstream  Android里可以省略此引用  使用#include #include using namespace std;void main() {    string str = "Android Developer love Android";    cout << "查找从指定位置开始的字符串起始位置" << endl;    //4.1 查找指定的字符串:find(顺时针)     //参数一:查找条件     //参数二:从那个位置开始查找,默认是0开始    int index = str.find("Android", 0);    cout << "值:" << index << endl;    //未找到返回-1    int index1 = str.find("IOS", 0);    cout << "值:" << index1 << endl;    cout << endl << "查找从末尾指定位置开始的字符串末尾位置" << endl;    //4.2 查找最后一个字符串:find_last_of(逆时针)    int index2 = str.find_last_of("Android", str.length());    cout << "值:" << index2 << endl;    cout << endl << "查找某个字符串出现的次数" << endl;    //4.3 查找某个字符串出现的次数    int index3 = str.find("Android", 0);    //计数    int count = 0;    //string::npos默认值是:-1    //判断是否存在    while (index3 != string::npos) {        count++;        index3 = index3 + 5;        index3 = str.find("Android", index3);    }    cout << "次数:" << count << endl;    getchar();}

执行输出:

查找从指定位置开始的字符串起始位置值:0值:-1查找从末尾指定位置开始的字符串末尾位置值:29查找某个字符串出现的次数次数:2

5.string类-字符串替换

#include #include using namespace std;void main() {    //5.1 替换一个        string str0 = "Android Developer love Android";        str0.replace(0,3,"Hello");        cout << str0.c_str() << endl;        string str1 = "Android Developer love Android";        str1.replace(0, 7, "Hello");        cout  << str1 << endl;    //5.2 替换所有    string str = "Android Developer love Android";    int index = str.find("Android", 0);    while (index != string::npos) {        str.replace(index, 7, "Hello");        index = index + 7;        //继续往后查询        index = str.find("Android", index);    }    cout  << str.c_str() << endl;    getchar();}

执行输出:

Helloroid Developer love AndroidHello Developer love AndroidHello Developer love Hello

6.string类-字符串删除

#include #include using namespace std;void main() {    //6.1 删除单个erase函数    string str = "Android Developer love Android";    //可以erase(0)代表删除整个字符串,因为删除数量number默认是字符串的长度    //erase()也是干掉    //参数有默认值    //str.erase();    //正确写法    string::iterator index0 = find(str.begin(), str.end(), 'A');    str.erase(index0);    cout << str.c_str() << endl;    //6.2 删除多个    string str1 = "Android Developer love Android";    //只一个指针    string::iterator index = find(str1.begin(), str1.end(), 'A');    str1.erase(index);    while (index != str1.end()) {        if (*index == 'A') {            str1.erase(index);        }        //指针位移        index++;    }    cout << str1.c_str() << endl;    getchar();}

执行输出:

ndroid Developer love Androidndroid Developer love ndroid

7.string类-字符串插入

#include using namespace std;void main() {    //从头开始插入    string str1 = "love Android";    str1.insert(0, "I ");    cout << str1.c_str() << endl;    //从最后开始插入    string str = "Android Developer love";    str.insert(str.length(), " Android");    cout << str.c_str() << endl;    getchar();}

执行输出:

I love AndroidAndroid Developer love Android

8.string类-大小写转换

#include ;#include   using namespace std;void main() {    //8.1 转小写    string str = "Android Developer love Android";    //参数一:从那一个位置开始    //参数二:到那一个位置结束    //参数三:目标对象    //参数四:格式(大写、小写)    //注意:参数四加'::'    //因为:在全局命名空间中这个tolower不是宏定义,    //     而是一个具体有实现的函数,所以需要明确命名空间    //在不同编译环境下有差别    //例如:在VS 中不需要"::",但是在Android NDK环境下需要加"::"    transform(str.begin(), str.end(), str.begin(), ::tolower);    cout << str.c_str() << endl;    //8.2 转大写    string str1 = "Android Developer love Android";    transform(str1.begin(), str1.end(), str1.begin(), ::toupper);    cout << str1.c_str() << endl;    getchar();}

执行输出:

android developer love androidANDROID DEVELOPER LOVE ANDROID
特别感谢:

Dream

更多相关文章

  1. Http的请求方式
  2. 自定义加载动画的两种实现方式
  3. android之AIDL跨进程通信详解 (四)AIDL中RemoteCallbackList的使
  4. Android异步更新UI的方式之使用Handler消息传递机制
  5. android跟服务器使用json传递数据
  6. Android异步更新UI的方式之使用Handler的post(Runnabel r)方法
  7. 【android】与pc机进行UDP通信(二)
  8. Android(安卓)TextView实现下划线的几种方式
  9. Android(安卓)中String.xml文件中的错误

随机推荐

  1. RelativeLayout 常用XML 属性
  2. android Wifi自动连接
  3. Android使用ViewFlipper做页面切换,与手势
  4. Android发展史
  5. 写一个没有Activity的 HelloWorld for an
  6. 通过xml加载菜单Menus
  7. Android图形系统分析与移植--五、Android
  8. android设置在ListView中让TextView滚动
  9. Android 源代码结构
  10. android 电容屏(一):电容屏基本原理篇