1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cassert>
#include <iostream>
#include <vector>
class SafeArray {
private:
std::vector<int> data;
public:
void push_back(int value) {
data.push_back(value);
}
int& at(size_t index) {
assert(index < data.size() && "Index out of bounds");
return data[index];
}
};
int main() {
SafeArray arr;
arr.push_back(10);
arr.push_back(20);
std::cout << "Element at index 1: " << arr.at(1) << std::endl;
std::cout << "Element at index 2: " << arr.at(2) << std::endl;
return 0;
}
Back to assertions