您的位置:首页 > 编程语言 > C语言/C++

关于学习C++过程中需要的知识点拓展

2016-05-17 20:47 681 查看
1.At (int nIndex, ELT def = 0)函数的用法

可以使用[]或者at()方法来访问字符串中的字符,起始索引是0.最大有效索引是string.length()-1.(特别的,如果是const string类型的对象,那么最大有效索引是string.length(),最后一个字符是'\0’.)

[]和at()的区别在于[]不检查索引是否有效,而at()在遇到无效索引时会抛出out_of_range异常.
2.inline 内联函数的标志的作用及用法

1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高。

2. 很明显,类的内联函数也是一个真正的函数,编译器在调用一个内联函数时,会首先检查它的参数的类型,保证调用正确。然后进行一系列的相关检查,就像对待任何一个真正的函数一样。这样就消除了它的隐患和局限性。

3.
inline 可以作为某个类的成员函数,当然就可以在其中使用所在类的保护成员及私有成员

注意事项:首先,你可以使用inline函数完全取代表达式形式的宏定义。另外要注意,内联函数一般只会用在函数内容非常简单的时候,这是因为,内联函数的代码会在任何调用它的地方展开,如果函数太复杂,代码膨胀带来的恶果很可能会大于效率的提高带来的益处。内联函数最重要的使用地方是用于类的存取函数。

另外要注意,内联函数一般只会用在函数内容非常简单的时候,这是因为,内联函数的代码会在任何调用它的地方展开,如果函数太复杂,代码膨胀带来的恶果很可能会大于效率的提高带来的益处。内联函数最重要的使用地方是用于类的存取函数。

4.realloc用法

原型:extern void *realloc(void *ptr, size_t newsize);

用法:#include <stdlib.h>

功能:改变ptr所指内存区域的大小为newsize长度。

说明:如果重新分配成功则返回指向被分配内存的指针,否则返回空指针NULL。当内存不再使用时,应使用free()函数将内存块释放。

5.qsort的用法

void
qsort(void *base, int nelem, unsigned int width, int ( * pfCompare)( const void *, const void *));

使用该函数,可以对任何类型的一维数组排序。该函数参数中,base 是待排序数组的起始地址,nelem 是待排序数组的元素个数,width 是待排序数组
的每个元素的大小(以字节为单位),最后一个参数 pfCompare 是一个函数指针,它指向一个“比较函数”。

6.bsearch的用法

void * bsearch(const void * key, const void * base, size_t num, size_t size, int (*comparator) ( const void *, const void * )

采用二分法查找的方法,找到在指定数组中的是否存在需要查找的数据,并返回,

comparator函数用来确定是以什么样的方法来判定是都是想要找的数据,可以自己定制化

比如查找23,现有数据为24,33,24,87,查找各位为3的数字都有哪些?

int
compareG3(const void* a,const void* b)

{

unsinged
int *p1,*p2;

p1
= (unsigned int *)a;

p2
= (unsigned int *)b;

retrun
(%p1%10) -(%p2%10)

}

key
=23;

values=[24,33,24,87]

pItem = (int*) bsearch (&key, values,
4, sizeof (int), compareG3);

pItem则返回23

7.template<int
x> Test 用法

c++模板参数有两类,一种是类型,一种是数值。关于数值作为模板参数,template<int
x> 这样的模板要如何使用N这个值。一种用途是以int array
的方式创建长度为N的数组。如果这个N你让用户以函数参数的方式传入,那么你必须new这个数组,现在你可以在编译时就知道N

下面是非常完整的所有template的用法

Template
type parameter:

template<typename T>

struct Container {

T t;

};

// pass type "long" as argument.

Container<long> test;

Template integer parameter:

template<unsigned int S>

struct Vector {

unsigned char bytes[S];

};

// pass 3 as argument.

Vector<3> test;

Template pointer parameter (passing a pointer to a function)

template<void (*F)()>

struct FunctionWrapper {

static void call_it() { F(); }

};

// pass address of function do_it as argument.

void do_it() { }

FunctionWrapper<&do_it> test;

Template reference parameter (passing an integer)

template<int &A>

struct SillyExample {

static void do_it() { A = 10; }

};

// pass flag as argument

int flag;

SillyExample<flag> test;

Template template parameter.

template<template<typename T> class AllocatePolicy>

struct Pool {

void allocate(size_t n) {

int *p = AllocatePolicy<int>::allocate(n);

}

};

// pass the template "allocator" as argument.

template<typename T>

struct allocator { static T * allocate(size_t n) { return 0; } };

Pool<allocator> test;

A template without any parameters is not possible. But a template without any explicit argument is possible - it has default arguments:

template<unsigned int SIZE = 3>

struct Vector {

unsigned char buffer[SIZE];

};

Vector<> test;

Syntactically, template<> is reserved to mark an explicit template specialization, instead of a template without parameters:

template<>

struct Vector<3> {

// alternative definition for SIZE == 3

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: