Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sem 2
- //Hello world
- // vector<int> v = {1, 2, 3};
- // cout << v << "\n";
- struct string {
- size_t capacity;
- union {
- struct {
- size_t size;
- char* data;
- } long_data;
- char[16] short_data;
- } data;
- };
- struct SelfCounting {
- SelfCounting() {
- data = 0;
- cout << "SelfCounting()\n";
- }
- SelfCounting(int d) {
- cout << "SelfCounting(int)\n";
- data = d;
- }
- SelfCounting(const SelfCounting& rhs) {
- cout << "SelfCounting(copy)\n";
- data = rhs.data;
- }
- int data;
- };
- ostream& operator<< (ostream& str, const SelfCounting& sc) {
- return str << sc.data;
- }
- SelfCounting by_val(SelfCounting obj) {
- cout << obj << "\n";
- return obj;
- }
- SelfCounting by_ref(const SelfCounting& obj) {
- cout << obj << "\n";
- return obj;
- }
- // itoa(45) -> string "45"
- // itoa(12345) -> "12345"
- // itoa(-443) -> "-443"
- // '0' + 5 == '5'
- string itoa(int value) {
- std::string result = "";
- result.reserve(15);
- bool minus_flag = false;
- if (value < 0) {
- minus_flag = true;
- value *= -1;
- }
- while (value > 0) {
- result += '0' + value % 10;
- value /= 10;
- }
- // return {result.rbegin(), result.rend()}
- if (minus_flag) {
- result += "-";
- }
- std::reverse(result.begin(), result.end());
- result.shrink_to_fit();
- return result;
- }
- template <typename T>
- ostream& operator<< (ostream& os, const vector<T>& v) {
- // [1, 2, 3]
- os << "[";
- for (std::size_t i = 0; i < v.size() - 1; ++i) {
- os << v[i] << ", ";
- }
- if (!v.empty()) {
- os << v[v.size() - 1];
- }
- return os << "]";
- }
- void dangling() {
- int c_v[3] = {1,2,3}
- vector<int> v = {1, 2, 3};
- v.reserve(4);
- int& f = v[0];
- f = 4;
- cout << v; // 4, 2, 3
- v.push_back(5);
- cout << f; //<
- int* p = &v[0];
- cout << *p;
- for (i = v.begin(); i != v.end(); ) {
- if (*i == 42)
- i = v.erase(i);
- else
- i++;
- }
- }
- void matrix_ops() {
- vector<vector<int>> matrix = {
- {1, 2, 3},
- {4, 5, 6},
- };
- cout << matrix << "\n";
- for (const auto& row : matrix) {
- for (int e : row)
- cout << e << " ";
- cout << "\n";
- }
- for (size_t i = 0; i < matrix.size(); ++i) {
- for (size_t j = 0; j < matrix[i].size(); ++j)
- cout << matrix[i][j] << " ";
- cout << "\n";
- }
- }
- void charmap() {
- for (int i = 0; i < 256; i++)
- cout << i << ": " << static_cast<char>(i) << "\n";
- char c;
- 'A' <= c && c <= 'Z';
- }
- void charmap_pretty() {
- using std::hex, std::cout;
- const size_t cols = 16;
- cout << " |";
- for (size_t c = 0; c < cols; c++)
- cout << hex << c << " ";
- cout << "\n--+-";
- for (size_t c = 0; c < cols; c++)
- cout << "--";
- for (unsigned char c = 0; c < 255; c++) {
- if ((c & 0xF) == 0) {
- cout << "\n" << hex << (c >> 4) << " | ";
- }
- cout << c << " ";
- }
- cout << "\n";
- }
- struct Date {
- int year;
- int month;
- uint8_t day;
- };
- void print_date(const Date& d) {
- cout << d.year << "-" << d.month << "-" << d.day << "\n";
- }
- string& concat(const string& l, const string& r) {
- string result = l;
- result += r;
- return result;
- }
- void structs() {
- Date ub;
- print_date(ub);
- print_date({2023});
- print_date({2023, 1});
- print_date({2023, 1, 20});
- print_date({.month = 1});
- print_date({.month = 1, .day = 20});
- }
- bool is_even(int i) {
- return i % 2 == 0;
- }
- bool date_year_less(const Date& l, const Date& r) {
- return l.year < r.year;
- }
- void evens(const vector<int>& data) {
- size_t count = std::count_if(data.begin(), data.end(), is_even);
- cout << count << "\n";
- }
- int do_pref_sum(int i) {
- static int p = 0;
- p += i;
- return p;
- }
- void prefix_sum(const vector<int> v) {
- vector<int> prefs;
- prefs.resize(v.size());
- std::transform(v.rbegin(), v.rend(), prefs.rbegin(), do_pref_sum);
- cout << v << "\n" << prefs << "\n";
- }
- int main(int, const char**) {
- prefix_sum({1, 2, 3});
- return 0;
- vector<Date> dates = {
- {2023, 1, 1},
- {2022, 1, 10},
- };
- cout << dates << "\n";
- std::sort(dates.begin(), dates.end(), date_year_less);
- cout << dates << "\n";
- return 0;
- }
- // vector<vector<int>> m;
- // cout << m << "\n";
- // [[1,2,3],[4,5,6]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement