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

相關文章
相關標籤/搜索