C++ Tutorial/Development/assert

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Use assert inside a for loop to check an array

#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;
int main() {
  int a[1000];
  int i;
  
  for (i = 0; i < 1000; ++i) 
    a[i] = 1000 - i - 1;
  sort(&a[0], &a[1000]);
  for (i = 0; i < 1000; ++i) 
    assert (a[i] == i);
  
  return 0;
}

Use assert to check

#include <iostream>
#include <cassert>
#include <deque>
#include <algorithm>  // For find
using namespace std;

int main()
{
  char x[5] = {"a", "r", "e", "q", "t"};
  deque<char> deque1(&x[0], &x[5]);
  // Search for the first occurrence of the letter e:
  deque<char>::iterator where = find(deque1.begin(), deque1.end(), "e");
  assert (*where == "e" );
  cout << "Ok." << endl;
  return 0;
}
Ok.

Using the STL generic reverse algorithm with a string and assert the correctness

#include <iostream>
#include <string>
#include <cassert> 
#include <algorithm> // For reverse algorithm
using namespace std;
int main()
{
  string string1 = "abc";
  reverse(string1.begin(), string1.end());
  assert (string1 == "cba");
  
  return 0;
}