[4] c ++ NDK Series 1 base string, c compatible, references, namespace

Finally began to organize c ++ foundation of the

1. Output

// C使用printf向终端输出信息
// C++提供了 标准输出流 
#include <iostream>
using namespace std;

   // 1. c++输出
    int time = 8;
    cout << "dds:" << time << "点," << "hello world" << endl;

2. function symbol compatible

Most C code can be used directly in C ++, but there are still areas requiring attention.

//如果需要在C++中调用C实现的库中的方法
extern "C" //指示编译器这部分代码使用C的方式进行编译而不是C++
    
void func(int x, int y);

For funcfunctions are compiled C compiler library function name may be func(no parameter symbol), and the C ++ compiler will produce a similar funciiname or the like.

Therefore, for the C library

#ifdef __cplusplus
extern "C"{
#endif
void func(int x,int y);
#ifdef __cplusplus    
}
#endif

//__cplusplus 是由c++编译器定义的宏,用于表示当前处于c++环境

3. references

Reference is C ++ definition of a new type of pointer reference, and two things are needed here to distinguish

//声明形参为引用
void change(int& i) {
	i = 10;
}
int i = 1;

change(i);

printf("%d\n",i); //i == 10

4. String

NULL character string is actually using '\0'a one-dimensional array of characters terminated.

//字符数组 = 字符串
char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
//自动加入\0
char str2[] = "Hello";

String Manipulation

function description
strcpy(s1, s2); Copy the string to the string s2 s1.
strcat(s1, s2); Connection String s1 s2 to the end of the string.
strlen(s1); It returns the length of the string s1.
strcmp(s1, s2); If s1 and s2 are the same, then return 0; if s1 <s2 is less than 0 is returned; if s1> s2 is greater than 0 is returned
strchr(s1, ch); It returns a pointer to the first occurrence of the character string s1 the location of ch.
strstr(s1, s2); It returns a pointer to the first occurrence of the string s1 s2 string position.

C ++ string class

C ++ standard library provides a string class type that supports all of the above operations, and also added other more.

#include <string>
//string 定义在 std命令空间中
usning namespace std;
string str1 = "Hello";
string str2 = "World";

str1.append(str2);
//获得c 风格字符串
const char *s1 = str1.c_str();
//字符串长度
str1.size();
//长度是否为0
str1.empty();

......

5. namespace

namespace namespace equivalent package

namespace A{
    void a(){}
}

错误 : a();
// :: 域操作符
正确: A::a();


//当然也能够嵌套
namespace A {
	namespace B{
		void a() {};
	}
}
A::B::a();

//还能够使用using 关键字
using namespace A;
using namespace A::B;

Code

https://github.com/ddssingsong/AnyNdk

Guess you like

Origin blog.csdn.net/u011077027/article/details/93030939