assert 是一个断言,是宏,不是函数。它的作用是,如果条件为假,则终止程序。可以通过#define NDEBUG来关闭assert,但是需要在源代码的最开头。
先看个例子:
#include<iostream>
#include <thread>
#include<stdlib.h>
#include<windows.h>
#include<assert.h>
using namespace std;
bool m_bFlag = false;
void fun1()
{
int i = 1;
while (!m_bFlag)
{
cout << "hello world " << i++ << endl;
}
}
void fun2()
{
m_bFlag = true;
}
void main()
{
int a = 7;
a = 9;
assert(a == 7);
cout << "hello world !";
system("pause");
}
结果:
那现在有个问题,如何去掉断言呢,答案为:
#define NDEBUG
#include
#include
#include<stdlib.h>
#include<windows.h>
#include<assert.h>
using namespace std;
bool m_bFlag = false;
void fun1()
{
int i = 1;
while (!m_bFlag)
{
cout << "hello world " << i++ << endl;
}
}
void fun2()
{
m_bFlag = true;
}
void main()
{
int a = 7;
a = 9;
assert(a == 7);
cout << "hello world !";
system("pause");
}
在第一行加入#define NDEBUG,请看结果:
因篇幅问题不能全部显示,请点此查看更多更全内容