#include <iostream> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <assert> using namespace std;
int add(int a, int b) { return a + b; }
class Rectangle { public: int width, height;
Rectangle(int w, int h) { width = w; height = h; }
int area() { return width * height; } };
int main() { int n; cout << "Enter a number: "; cin >> n; cout << "You entered: " << n << endl;
int arr[5] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; ++i) { cout << arr[i] << " "; }
vector<int> vec = {1, 2, 3, 4, 5}; vec.push_back(6); for (int i = 0; i < vec.size(); ++i) { cout << vec[i] << " "; }
string str = "Hello, World!"; cout << str << endl; str += " How are you?";
Rectangle rect(10, 5); cout << "Area: " << rect.area() << endl;
stack<int> s; s.push(1); s.push(2); while (!s.empty()) { cout << s.top() << " "; s.pop(); }
queue<int> q; q.push(1); q.push(2); while (!q.empty()) { cout << q.front() << " "; q.pop(); }
priority_queue<int> pq; pq.push(3); pq.push(1); while (!pq.empty()) { cout << pq.top() << " "; pq.pop(); }
set<int> s = {3, 1, 2, 2}; for (auto it = s.begin(); it != s.end(); ++it) { cout << *it << " "; }
map<string, int> m; m["apple"] = 3; m["banana"] = 2; for (auto it = m.begin(); it != m.end(); ++it) { cout << it->first << ": " << it->second << endl; }
int a = 10; int* p = &a; int* p = a;
int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; cout << *(ptr + 3) << " ";
int *ptr = new int; *ptr = 10; delete ptr;
unique_ptr<int> ptr = make_unique<int>(10); cout << "Value: " << *ptr << endl;
shared_ptr<int> ptr1 = make_shared<int>(10); shared_ptr<int> ptr2 = ptr1;
cout << "Value: " << *ptr1 << endl; assert(ptr1.use_count() == 2); const int *ptr1 = &x; int *const ptr2 = &x; const int *const ptr3 = &x;
int (*func_ptr)(int, int) = add; int result = func_ptr(3, 4);
for_each(nums.begin(), nums.end(), [&sum](int n) { sum += n; });
#include <stdexcept> try { int a = 10; int b = 0; if (b == 0) { throw runtime_error("Division by zero"); } cout << "Result: " << a / b << endl; } catch (const exception &e) { cout << "Error: " << e.what() << endl; }
return 0; }
|