首先,您需要了解模板,宏和向量,然後再進行下一階段!
- 模板是通用編程的基礎,它涉及以獨立於任何特定類型的方式編寫代碼。
- 宏是已命名的代碼片段。每當使用該名稱時,它就會被宏的內容替換。
- 向量與動態數組相同,具有在插入或刪除元素時自動調整自身大小的能力,並且容器自動處理其存儲。
我們可以使用這些強大的工具以有效的方式編寫代碼。
編程競賽中可以使用的一些很酷的技巧如下:
1. 使用基於範圍的for循環:這是C ++ 11中非常酷的功能,如果您要從頭到尾進行迭代,則最好使用。 這段代碼顯示瞭如何使用range循環遍歷數組和向量:
<code>// C++ program to demonstrate range based for // loops for accessing vector and array elements #include<iostream> #include <vector> using namespace std; int main() { // Create a vector object that // contains 5 elements vectorvec = {0, 1, 2, 3, 4}; // Type inference by reference using auto. // Range based loops are preferred when no // modification is needed in value for (const auto &value : vec) cout << value << ' '; cout << '\\n'; // Basic 5 element integer array int array[]= {1, 2, 3, 4, 5}; for (const auto &value: array) cout << value << " "; return 0; } /<vector>/<iostream>/<code>
輸出:
<code>0 1 2 3 41 2 3 4 5/<code>
2. 初始化列表:此類型用於訪問C ++初始化列表中的值。在這裡,這種類型的對象由編譯器根據初始化列表聲明自動構造,該列表是用大括號括起來的逗號分隔元素的列表。
<code>#include<iostream> template<typename> void printList(std::initializer_listtext) { for (const auto & value: text) std::cout << value << " "; } // Driver program int main() { // Initialization list printList( {"One", "Two", "Three"} ); return 0; } /<typename>/<iostream>/<code>
輸出:
<code>One Two Three/<code>
3. 分配最大值或最小值:這一點有助於避免在編寫max()或min()函數時花費過多的精力。
<code>#include<iostream> // Call by reference is used in x template<typename> static inline void amin(T &x, U y) { if (y < x) x = y; } // call by reference is used in x template<typename> static inline void amax(T &x, U y) { if (x < y) x = y; } // Driver program to find the Maximum and Minimum value int main() { int max_val = 0, min_val = 1e5; int array[]= {4, -5, 6, -9, 2, 11}; for (auto const &val: array) // Same as max_val = max (max_val, val) // Same as min_val = min (min_val,val) amax(max_val, val), amin (min_val, val); std::cout << "Max value = " << max_val << "\\n" << "Min value = " << min_val; return 0; } /<typename>/<typename>/<iostream>/<code>
輸出:
<code>Max value = 11Min value = -9/<code>
4. C / C ++中的快速輸入/輸出:在編程競賽中,您必須儘可能快地閱讀輸入/輸出,以節省寶貴的時間。
<code>#include <bits> template<typename> void scan(T &x) { x = 0; bool neg = 0; register T c = getchar(); if (c == '-') neg = 1, c = getchar(); while ((c < 48) || (c > 57)) c = getchar(); for ( ; c < 48||c > 57 ; c = getchar()); for ( ; c > 47 && c < 58; c = getchar() ) x= (x << 3) + ( x << 1 ) + ( c & 15 ); if (neg) x *= -1; } template<typename> void print(T n) { bool neg = 0; if (n < 0) n *= -1, neg = 1; char snum[65]; int i = 0; do { snum[i++] = n % 10 + '0'; n /= 10; } while (n); --i; if (neg) putchar('-'); while (i >= 0) putchar(snum[i--]); putchar('\\n'); } // Driver Program int main() { int value; // Taking input scan(value); // Printing output print(value); return 0; } /<typename>/<typename>/<bits>/<code>
<code>輸入:756輸出:756/<code>
5. 使用宏作為循環:也許,使用這樣的宏不太好,因為它會降低代碼的可讀性,但是對於編寫快速的代碼,您可以冒這個風險!
<code>#include <bits> using namespace std; #define rep(i,n) for (i = 0; i < n; ++i) #define REP(i,k,n) for (i = k; i <= n; ++i) #define REPR(i,k,n) for (i = k; i >= n; --i) // Driver program to test above Macros int main() { int i; int array[] = {4, 5, 6, 9, 22, 11}; int size= sizeof(array)/sizeof(array[0]); // Default 0 index based loop rep(i, size) cout << array[i] << " "; cout</<bits>/<code>
輸出:
<code>4 5 6 9 22 115 6 9 22 1111 22 9 6 5 4/<code>
一些更重要的要點可以進一步縮短您的時間:
6. 使用“ bits/stdc++.h ”:無需添加大量的#include行,而只需使用#include <bits>文件就包含了編程競賽中所需的所有頭文件,從而節省了很多時間 。/<bits>
7. 容器:使用各種容器,如vector、list、map等,使人們能夠使用預定義的功能並顯著減少代碼的大小。
8. 快速cin和cout:如果將cin和cout用於I / O,只需在main()之後添加以下行。
<code>std::ios_base::sync_with_stdio(false);/<code>
9. auto:使用自動聲明數據類型可以節省編程比賽期間的大量時間。將變量定義為auto時,編譯器會在編譯時確定其類型。
10. 庫和預定義函數:在任何適用的地方使用內置函數,例如__gcd(A,B),swap,_builtin_popcount(R),_ builtin_clz(R)等。嘗試學習C++算法庫中可用的不同函數,它們在程序中大多數時候都很有用。
最終,通過使用這些巧妙的技巧,您可以輕鬆地用最少的時間和代碼行來編寫代碼。
閱讀更多 少兒編程Prog61 的文章