Блог пользователя ritesh17rb

Автор ritesh17rb, история, 5 месяцев назад, По-английски

Repo: https://github.com/MAZHARMIK/Cpp-STL-Quick-Help/blob/main/README.md

Repo: https://github.com/MAZHARMIK/Cpp-STL-Quick-Help/blob/main/README.md

Welcome to a quick reference guide for essential C++ Standard Template Library (STL) functions. These functions, with easy-to-understand comments and examples, are especially helpful for solving various LeetCode problems. Let’s dive in!

Priority Queues Usage of priority_queue (heap):

Max-Heap:

priority_queue pq; Min-Heap using in-built comparator:

priority_queue<int, vector, greater> pq; Custom Comparator (Structure):

struct comp { bool operator()(int &a, int &b) { return a < b; // max-heap } }; priority_queue<int, vector, comp> pq; Lambda Function Comparator:

auto comp = [](int &a, int &b) { return a < b; // max-heap }; priority_queue<int, vector, decltype(comp)> pq(comp); Using std::move Efficiently transfer resources without copying:

string source = "MIK"; string target = std::move(source); cout << "source = " << source << ", target = " << target; // source = , target = MIK Using std::accumulate Calculate the sum of elements in a container:

vector nums{1, 3, 2, 5}; int sum = accumulate(begin(nums), end(nums), 0); cout << sum; // Output: 11 Min and Max Elements Find the minimum and maximum elements:

vector nums{1, 3, 2, 5}; int minVal = *min_element(begin(nums), end(nums)); // 1 int maxVal = *max_element(begin(nums), end(nums)); // 5 upper_bound and lower_bound Find positions in a sorted container:

vector vec{10, 20, 30, 30, 20, 10, 10, 20}; auto up = upper_bound(begin(vec), end(vec), 20); // first element > 20 auto low = lower_bound(begin(vec), end(vec), 20); // first element >= 20 Using std::rotate Rotate elements in a vector:

vector vec{1, 2, 3, 4}; rotate(vec.begin(), vec.begin() + 2, vec.end()); // Left Rotate by 2 rotate(vec.begin(), vec.begin() + vec.size() — 2, vec.end()); // Right Rotate by 2 Check String Rotation Check if a rotation of string s can become string t:

string s = "abcde"; string t = "cdeab"; cout << (s.length() == t.length() && (s+s).find(t) != string::npos); Using std::next_permutation Get the next lexicographical permutation:

vector vec{1, 2, 3, 4}; if(next_permutation(begin(vec), end(vec))) { cout << "Next permutation: "; for(int x : vec) cout << x << " "; // 1 2 4 3 } std::stringstream Convert string to number or count words:

string s = "12345"; stringstream ss(s); int x = 0; ss >> x; cout << x; // Output: 12345 Using std::transform Convert string to lowercase:

string line = "Hello World"; transform(begin(line), end(line), begin(line), ::tolower); cout << line; // hello world Using std::regex_replace Replace all vowels in a string:

string s = "mika"; auto rgx = regex("[aeiouAEIOU]"); cout << regex_replace(s, rgx, ""); // mk Using std::count_if Count elements satisfying a condition:

vector vec{1, 3, 2, 0, 5, 0}; auto lambda = [&](const int &i) { return i == 0; }; cout << count_if(begin(vec), end(vec), lambda); // Output: 2 Using std::copy_if Copy elements that satisfy a condition:

vector from_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; vector to_vec; copy_if(begin(from_vec), end(from_vec), back_inserter(to_vec), [](int n){ return n % 2 == 0; }); for(int it : to_vec) cout << it << " "; // Output: 2 4 6 8 10 By understanding and utilizing these essential C++ STL functions, you'll be able to tackle a wide range of competitive programming problems more efficiently. Happy coding!

Полный текст и комментарии »

  • Проголосовать: нравится
  • +18
  • Проголосовать: не нравится