-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_cpp.js
More file actions
32 lines (28 loc) · 7.72 KB
/
Copy pathupdate_cpp.js
File metadata and controls
32 lines (28 loc) · 7.72 KB
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
30
31
32
require('dotenv').config();
const mongoose = require('mongoose');
const Course = require('./backend/models/Course');
mongoose.connect(process.env.MONGODB_URI)
.then(async () => {
console.log('DB Connected for updating C++');
const cppNotes = [
"# **C++ Masterclass: Complete Notes**\n\nWelcome to the complete C++ guide outlining core language features.",
"## **1. Variables & Operators**\nVariables store data, while operators perform operations.\n```cpp\n#include <iostream>\nusing namespace std;\n\nint main() {\n int age = 25; // Variable\n int nextYear = age + 1; // + is an arithmetic operator\n \n // Logical and Relational\n bool isAdult = (age >= 18) && (nextYear > age);\n \n cout << \"Age: \" << age << \"\\n\";\n return 0;\n}\n```",
"## **2. If-Else Statements**\nUsed for control flow and decision making based on boolean logic.\n```cpp\nint score = 85;\nif (score >= 90) {\n cout << \"A Grade\";\n} else if (score >= 80) {\n cout << \"B Grade\";\n} else {\n cout << \"C Grade\";\n}\n```",
"## **3. Loops (For, While, Do-While)**\nLoops execute a block of code repeatedly as long as a condition is met.\n```cpp\n// For Loop (ideal when exact iterations are known)\nfor(int i = 0; i < 3; i++) {\n cout << i << \" \";\n}\n\n// While Loop (ideal for condition-based stopping)\nint j = 0;\nwhile(j < 3) {\n cout << j << \" \";\n j++;\n}\n\n// Do-While Loop (Executes code at least once!)\nint k = 0;\ndo {\n cout << k << \" \";\n k++;\n} while(k < 3);\n```",
"## **4. Arrays**\nFixed-size sequential collections of elements of the same type stored contiguously in memory.\n```cpp\nint numbers[5] = {10, 20, 30, 40, 50};\ncout << \"First Element: \" << numbers[0]; // Prints 10\n\n// Iterating through the array\nfor(int i = 0; i < 5; i++) {\n cout << numbers[i] << \" \";\n}\n```",
"## **5. Vectors (Dynamic Arrays)**\nThe modern C++ alternative to raw arrays! Vectors resize dynamically and handle memory safely (requires `<vector>`).\n```cpp\n#include <vector>\n#include <iostream>\n#include <algorithm> // For STL algorithms\nusing namespace std;\n\nint main() {\n // 1. Initialization\n vector<int> v = {10, 20, 30};\n\n // 2. Insertions & Deletions\n v.push_back(40); // Adds 40 to the end, O(1)\n v.pop_back(); // Removes the last element, O(1)\n v.insert(v.begin() + 1, 15); // Inserts 15 at index 1, O(N)\n v.erase(v.begin() + 2); // Erases element at index 2, O(N)\n\n // 3. Traversing\n // Method A: Index based\n for(int i = 0; i < v.size(); i++) cout << v[i] << \" \";\n // Method B: Range-based Loop (Modern C++)\n for(int x : v) cout << x << \" \";\n // Method C: Iterators\n for(auto it = v.begin(); it != v.end(); it++) {\n cout << *it << \" \";\n }\n\n // 4. Common STL Functions\n sort(v.begin(), v.end()); // Ascending sort\n sort(v.rbegin(), v.rend()); // Descending sort\n reverse(v.begin(), v.end()); // Reverses the vector entirely\n}\n```",
"## **6. Maps (Ordered and Unordered)**\nMaps store elements in key-value pairs.\n\n**Ordered Map (`<map>`)**: Uses a Binary Search Tree (Red-Black). Keys are sorted automatically. Operations take `O(log N)`.\n**Unordered Map (`<unordered_map>`)**: Uses a Hash Table. Keys are NOT sorted. Operations take `O(1)` on average!\n```cpp\n#include <map>\n#include <unordered_map>\n#include <iostream>\nusing namespace std;\n\nint main() {\n // Ordered Map (Sorted by Key implicitly)\n map<string, int> ages;\n ages[\"Alice\"] = 28;\n ages[\"Charlie\"] = 40;\n ages[\"Bob\"] = 34; // Will store in order: Alice, Bob, Charlie\n\n // Unordered Map (O(1) Hash Table lookup)\n unordered_map<string, int> fastMap;\n fastMap[\"Data\"] = 100;\n\n // Searching & Retrieving\n if (ages.find(\"Bob\") != ages.end()) {\n cout << \"Bob's age: \" << ages[\"Bob\"] << \"\\n\";\n }\n\n // Traversing a Map\n for (auto const& [key, val] : ages) {\n cout << key << \": \" << val << \"\\n\";\n }\n\n // Deleting\n ages.erase(\"Alice\"); // Removes Alice\n}\n```",
"## **7. Sets**\nCollections of unique elements. Like Maps, there are Ordered and Unordered Sets.\n```cpp\n#include <set>\n#include <iostream>\nusing namespace std;\n\nint main() {\n set<int> bst;\n bst.insert(20);\n bst.insert(10);\n bst.insert(30);\n bst.insert(20); // Ignored, as elements must be unique\n\n // Searching and Presence\n if (bst.find(10) != bst.end()) cout << \"10 is present!\\n\";\n // .count() returns 1 if present in set, 0 if missing\n if (bst.count(30)) cout << \"30 is there!\\n\";\n\n // Deletion\n bst.erase(20); // Removes 20 from set\n\n // Lower & Upper Bound (Only in Ordered Set)\n // lower_bound returns iterator to first element >= val\n auto lb = bst.lower_bound(15); \n // upper_bound returns iterator to first element > val\n auto ub = bst.upper_bound(25);\n\n // Traversing\n // Prints 10 30 in sorted order naturally\n for(auto it = bst.begin(); it != bst.end(); it++) {\n cout << *it << \" \"; \n }\n}\n```",
"## **8. Custom Data Structures (Linked Lists)**\nC++ offers low-level memory access via pointers, making it ideal to build custom data structures like Linked Lists from scratch.\n```cpp\n#include <iostream>\nusing namespace std;\n\n// 1. Structure Definition\nstruct Node {\n int data;\n Node* next;\n \n Node(int val) {\n data = val;\n next = nullptr;\n }\n};\n\n// 2. Traversal\nvoid traverse(Node* head) {\n Node* temp = head;\n while(temp != nullptr) {\n cout << temp->data << \" -> \";\n temp = temp->next;\n }\n cout << \"NULL\\n\";\n}\n\n// 3. Insert Node (at the end)\nvoid insertEnd(Node*& head, int val) {\n Node* newNode = new Node(val);\n if(head == nullptr) {\n head = newNode;\n return;\n }\n Node* temp = head;\n while(temp->next != nullptr) {\n temp = temp->next;\n }\n temp->next = newNode;\n}\n\n// 4. Delete Node (by value)\nvoid deleteNode(Node*& head, int key) {\n if(head == nullptr) return; // Empty List\n \n // If head holds the key\n if(head->data == key) {\n Node* toDelete = head;\n head = head->next;\n delete toDelete; // Crucial in C++ to prevent memory leaks!\n return;\n }\n \n Node* temp = head;\n while(temp->next != nullptr && temp->next->data != key) {\n temp = temp->next;\n }\n \n if(temp->next == nullptr) return; // Key not found\n \n Node* toDelete = temp->next;\n temp->next = temp->next->next;\n delete toDelete;\n}\n\n// 5. Finding the Middle Element (Tortoise & Hare approach)\nNode* getMiddle(Node* head) {\n Node* slow = head;\n Node* fast = head;\n // Fast pointer moves 2x the speed of slow pointer\n while(fast != nullptr && fast->next != nullptr) {\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow; // Will hold middle element\n}\n\nint main() {\n Node* head = nullptr;\n \n insertEnd(head, 10);\n insertEnd(head, 20);\n insertEnd(head, 30);\n insertEnd(head, 40);\n insertEnd(head, 50);\n \n // Outputs: 10 -> 20 -> 30 -> 40 -> 50 -> NULL\n traverse(head); \n \n // Test Middle Access\n Node* mid = getMiddle(head);\n if(mid) cout << \"Middle is: \" << mid->data << \"\\n\";\n \n // Test Deletion\n deleteNode(head, 30);\n traverse(head); // 10 -> 20 -> 40 -> 50 -> NULL\n \n return 0;\n}\n```"
];
await Course.updateOne(
{ title: 'C++ Masterclass' },
{ $set: { notes: cppNotes } }
);
console.log('Expanded C++ LinkedList Notes Updated in DB!');
process.exit();
})
.catch(err => {
console.error(err);
process.exit(1);
});