C++ 萬能標頭檔案#include “bits/stdc++.h”
C++ 萬能標頭檔案#include “bits/stdc++.h”
資料來源: https://www.796t.com/content/1547093362.html
最近在打cf時賽後翻閱別人的程式碼總是會發現一個陌生而奇怪的標頭檔案#include<bits/stdc++.h>
奇怪之處就在於基本上所有的程式碼只要用了這個標頭檔案就不再寫其他標頭檔案了。
百度過後彷彿打開了新世界的大門,標頭檔案居然還可以這樣用!!!
#include<bits/stdc++.h>包含了目前c++所包含的所有標頭檔案!!!!
從此開啟開掛般的人生啊!!
現在再看下面這一堆亂七八糟的標頭檔案顯得莫名的冗雜: [C++ 常用include 列表整理清單]
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> using namespace std; int main(){ return 0; }
再看我們開掛以後: [codeblocks 20.0測試 OK]
#include<bits/stdc++.h>//#include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; return 0; }
簡潔明瞭啊是不是 一眼望穿啊是不是 心動了是不是 大腦充血了是不是 幸福昏厥了是不是 再也不用擔心CE了是不是!!!
談一下朋友們擔心的相容性問題,一早起來跑了幾個oj親測相容性還是蠻強的,看到去年的介紹部落格表示hdu不支援不過現在親測已經支援了,請在hdu肆無忌憚的開掛吧!!
12 thoughts on “C++ 萬能標頭檔案#include “bits/stdc++.h””
1. 找出向量的大小(C/C++)
#include
using namespace std;
int main()
{
vector arr1 = {1, 2, 3, 4};
vector arr2 = {};
vector arr3 = {1.2, 3.8, 3.0, 2.7, 6.6};
cout << "Size of arr1: " << arr1.size() << endl;
cout << "Size of arr2: " << arr2.size() << endl;
cout << "Size of arr3: " << arr3.size() << endl;
return 0;
}
2. 随机排列数组 (C/C++ 亂數排列陣列)
#include
using namespace std;
int main()
{
vector arr = {1, 2, 3, 4};
unsigned seed = 0;
cout << "Original array:";
for (int ele: arr)
{
cout << ele << " ";
}
cout << endl;
shuffle(arr.begin(), arr.end(), default_random_engine(seed));
cout << "Shuffled array:";
for (int ele: arr)
{
cout << ele << " ";
}
return 0;
}
3. 在C++ 中交換兩個變量 (C/C++)
#include
using namespace std;
int main()
{
int x = 5, y = 10;
string str1 = "MakeUseOf", str2 = "MUO";
cout << "Before Swapping: " << endl;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
swap(x, y);
swap(str1, str2);
cout << "After Swapping: " << endl;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
return 0;
}
4. 求一個數的位數之和 (C/C++)
您可以使用以下過程找到數字的數字總和:
初始化一個sum 變量來存儲結果。
通過對10 執行取模運算來找到數字的餘數。
將餘數與總和相加。
將數字除以10。
當數字大於10 時,從步驟2 開始重複該過程。
#include
using namespace std;
int main()
{
int num = 4635, sum = 0, temp;
while (num != 0)
{
temp = num%10;
sum = sum+temp;
num = num/10;
}
cout << "Sum: " << sum << endl;
return 0;
}
5. 將一個向量複製到另一個向量 (C/C++)
在C++ 中有多種方法可以將一個向量複製到另一個向量。您可以使用賦值運算符或將向量作為構造函數傳遞來執行相同操作。
#include
using namespace std;
void printVector(vector vec)
{
for (auto ele: vec)
{
cout << ele << " ";
}
cout << endl;
}
int main()
{
vector vec = {1, 2, 3, 4, 5};
printVector(vec);
// Method 1: Using Assignment Operator
vector newVec1 = vec;
printVector(newVec1);
// Method 2: By passing vector as constructor
vector newVec2(vec);
printVector(newVec2);
return 0;
}
6. 找出數組的最大和最小元素 (C/C++)
您可以分別使用max_element()和min_element()函數從數組中找到最大和最小元素。
#include
using namespace std;
int main()
{
int arr[] = {23, 56, 99, 15, 56};
int size = sizeof(arr)/sizeof(arr[0]);
cout << "Max element: " << *max_element(arr, arr+size) << endl;
cout << "Min element: " << *min_element(arr, arr+size) << endl;
return 0;
}
7. 在集合中插入元素 (C/C++)
您可以使用insert()函數在集合中插入元素。此函數接受元素作為將插入集合中的參數。
#include
using namespace std;
int main()
{
set st;
st.insert("Make");
st.insert("Use");
st.insert("Of");
st.insert("Of");
for (auto it = st.begin(); it != st.end(); it++)
{
cout << *it << " ";
}
return 0;
}
8.從字符串(字串)中刪除重複項 (C/C++)
您可以使用以下方法從字符串中刪除重複字符:
#include
using namespace std;
void removeDuplicateCharacters(char str[], int size)
{
int newIndex=0;
// Traversing through all the characters
for (int i = 0; i < size; i++)
{
int j;
// Traversing loop from the first character to current character
for (j = 0; j < i; j++)
{
if (str[i] == str[j])
{
break;
}
}
if (j == i)
{
str[newIndex++] = str[i];
}
}
// After removing duplicates, we make
// the vacant part of string to null
str[newIndex] = '\0';
}
int main()
{
char str[] = "MakeUseOf";
int size = strlen(str);
cout << "Original String: " << endl;
cout << str << endl;
removeDuplicateCharacters(str, size);
cout << "New String: " << endl;
cout << str << endl;
return 0;
}
9. 求字符串(字串)的長度 (C/C++)
您可以使用length()函數找到C++ 字符串的長度。或者,您也可以使用size()函數(它是length()函數的別名)。
#include
using namespace std;
int main()
{
string str1 = "MakeUseOf";
cout << "Length of " << str1 << " : " << str1.length() << endl;
string str2 = "lorem ipsum";
cout << "Length of " << str2 << " : " << str2.size() << endl;
return 0;
}
10. 從數組(陣列)中刪除一個元素 (C/C++)
#include
using namespace std;
int deleteElementFromArray(int arr[], int size, int elementToBeDeleted)
{
int i, j;
// Search if elementToBeDeleted is present
// in the array or not
for (i = 0; i < size; i++)
{
if (arr[i] == elementToBeDeleted)
{
break;
}
}
// If elementToBeDeleted is found in the array
if (i < size)
{
// We need to reduce the size of the array
// and shift the rest elements
size = size - 1;
for (j = i; j < size; j++)
{
arr[j] = arr[j+1];
}
}
// New array size is returned
return size;
}
void printArrayElements(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr)/sizeof(arr[0]);
cout << "Original Array: " << endl;
printArrayElements(arr, size);
int elementToBeDeleted = 3;
size = deleteElementFromArray(arr, size, elementToBeDeleted);
cout << "New array: " << endl;
printArrayElements(arr, size);
return 0;
}
11. 遍歷向量 (C/C++)
您可以通過多種方式遍歷向量。以下是三種最常用的遍歷向量的方法:
使用範圍for(range for)
#include
using namespace std;
int main()
{
vector vec = {1, 2, 3, 4, 5};
// Method 1: Using range for
for (auto element: vec)
{
cout << element << " ";
}
return 0;
}
使用索引
#include
using namespace std;
int main()
{
vector vec = {1, 2, 3, 4, 5};
// Method 2: Using indexing
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
return 0;
}
使用迭代器的引用
#include
using namespace std;
int main()
{
vector vec = {1, 2, 3, 4, 5};
// Method 3: Using reference of the iterator
for (auto it = begin(vec); it != end(vec); it++)
{
cout << *it << " ";
}
return 0;
}
11 個C++ 代碼片段助您解決日常編程問題 (C/C++ 演算法)
https://mp.weixin.qq.com/s/7rWx1-5l3-h8T0JNr-eVjA
上面程式碼出處