C++ assert用法
#include <assert.h>
assert(expression);
expression为逻辑表达式;
当expression == false时; 终止错误,并在控制台输出 不满足的expression表达式及行号。
例子:
#include <iostream>
#include <assert.h>
int find(int array[], int length, int value)
{
if(NULL == array || 0 == length)
return false;
int* start = array;
int* end = array+length;
while(start<end){
if(value == *start)
return ((int)start - (int)array) /(sizeof(int));
start++;
}
return false;
}
int main()
{
int array[10] = {1,2,3,4,5, 6,7,8,9,10};
assert(false != find(array,10,12));
int index = find(array,10,1);
std::cout<<" index "<<index<<std::endl;
int i = 0;
std::cin>>i;
return 0;
}