C++中assert语句的使用

#include <iostream>
#include <assert.h>

using namespace std;

const int N_PRIMES = 7;
int primes[N_PRIMES] = {2,3,5,7, 11, 13, 17};

int main()
{
    int index = 10;
    assert(index<N_PRIMES);//这里assert括号内为false,程序异常终止。
    assert(index>=0);
    cout << "The tenth prime is " << primes[index] << endl;
    return 0;
}

assert()用于判断是否为false,若为false,声明失败并发出一条信息,而后程序异常终止。ios

相关文章
相关标签/搜索