1. Most commonly used constants
int min = INT_MIN; // -2147483648
int max = INT_MAX; // 2147483647
const int INF = 0X3F3F3F3F; // 1061109567, 通常使用代替最大值,防止溢出
2. String judgment function
char c = 65;
char b = '4';
char a = 'D';
cout << std::isdigit(c) <<endl; // 0 判断C字符串是不是数字
cout << std::isalpha(c) <<endl; // 1 判断C字符串是不是字母
cout << std::isalnum(b) <<endl; // 4 判断C字符串是不是数字或者字母
cout << std::islower(a) <<endl; // 0 判断C字符串是不是小写
cout << std::isupper(a) <<endl; // 1 判断C字符串是不是大写
3. Conversion between string and value
int num = 100;
string str = to_string(num); // 整型转化为字符产
cout << str << endl; //100
int number = stoi(str); // 数字转化为整型 ,stol
cout << number << endl; //100
4. Dichotomy of iterators
vector<int> nums{
1, 2, 34, 44, 56, 99};
int k = lower_bound(nums.begin(), nums.end(), 56) - nums.begin(); // 4 第一个大于等于目标值的的迭代器的位置。
cout << k << endl;
int g = upper_bound(nums.begin(), nums.end(), 56) - nums.begin(); // 5 第一个大于目标值的的迭代器的位置。
cout << g << endl;
5. String to lowercase
string strTemp = "abcdEFGH";
transform(strTemp.begin(), strTemp.end(), strTemp.begin(), ::tolower);
cout << strTemp << endl; // abcdefgh
transform(strTemp.begin(), strTemp.end(), strTemp.begin(), ::toupper);
cout << strTemp << endl; // ABCDEFGH
6. Small root pile
priority_queue<pair<string,int>> pqGig; // 大根堆
for (int i = 0; i < 10; ++i) {
// emplace() 和 insert() 都能完成向 vector 容器中插入新元素,那么谁的运行效率更高呢?答案是 emplace()
pqGig.emplace(to_string(10 - i +100), i);
}
while (!pqGig.empty()){
pair<string, int> temp = pqGig.top();
cout << temp.first << ": " << temp.second <<endl;
pqGig.pop();
}
priority_queue<int ,vector<int>, greater<int>> pqLitter; // 小根堆
7. Quickly initialize arrays
vector<string> tmpStr(7, ""); //数组长度为7,全部初始化为""
vector<int> tmpInt(10, 0x3f3f3f3f); //数组长度为10,全部初始化为1061109567
cout << tmpStr.size() << endl; // 7
cout << tmpInt.size() << endl; // 10
for (int i = 0; i < tmpInt.size(); ++i) {
cout << tmpInt[i] << endl; // 1061109567
}
int tmp[10];
memset(tmp, 0, sizeof tmp); // 按字节初始化数组,元素为0
memset(tmp, -1, sizeof tmp); // 按字节初始化数组,元素为-1
memset(tmp, 0x3f, sizeof tmp);// 按字节初始化数组,元素为1061109567
8. Features of C++11
auto p = 1; // auto
string *pre = nullptr; //nullptr替代NULL
unordered_map<int, string> mp; //哈希表,内部无序map
unordered_set<string> st; //无序集合
9. String segmentation
string str1 = "word mine sd sd";
stringstream strStream(str1);
string tmp;
while (strStream >> tmp){
// 读入数据,赋值给tmp
cout << tmp << endl;
}
10. Rounding to retain decimals
char str[10];
double num = 22.234334;
sprintf(str, "%.2f", num);
string s = str;
cout << s << endl;
11. The string is split according to the format
string aa = "110, 119, 120, 113";
char strChar[1000];
memcpy(strChar, aa.c_str(), strlen(aa.c_str()));
int u, v, w, x;
sscanf(strChar,"%d, %d, %d, %d", &u, &v, &w, &x);
cout << u <<"-"<< v <<"-" << w <<"-" << x << endl;
12. Strings of the same characters
string tempStr(12, 'b');
13. Structure sorting
struct node{
int a, b;
// 从小到大排序
bool operator < (const node& node_)const{
if(a != node_.a) return a < node_.a;
return b < node_.b;
}
};
int main(){
vector<node> tt;
tt.push_back({
1,5});
tt.push_back({
2,3});
sort(tt.begin(), tt.end());
for(auto &node: tt){
cout<<node.a<<" "<<node.b<<endl;
}
return 0;
}
14. Two-dimensional array sorted by column
Sort by the first column, if the first column is equal, sort by the second column
vector<vector<int>> v;
sort(v.begin(),v.end(),[](vector<int>&v1,vector<int>&v2){
if(v1[0] != v2[0])return v1[0]<v2[0];else return v1[1]<v2[1];});
15. reverse() function
To include a header file #include, this function has no return value.
If it is a two-dimensional array, it is flipped according to the row, for example, the first row is flipped to the last row.
reverse[first,last);
16. The difference between .,->
Difference between instance and pointer.
For AB, A is an object or structure; dot (.): the left must be an entity.
A->B, then A is a pointer, -> is member extraction, A->B is to extract member B in A, A can only be a pointer to a class, structure, union; arrow (->): the left must be a pointer .
17. Sorting while retaining the original coordinates
sort(ids.begin(),ids.end(),[&](int i,int j){
return nums2[i] < nums2[j];});
At this time, ids[0] is the coordinate of the smallest number in the original array
or:
vector<pair<int,int>> v;
//第一个位置放数值,第二个位置放坐标
sort(v.begin(),v.end());
18. Usage of function function
std::function is a general polymorphic function wrapper that can call ordinary functions, Lambda functions, functors, bind objects, member functions of classes and pointers to data members. Function is defined in the header file named function.h . It is a template. When creating a function instance, the type must be specified, such as:
#include <functional>
function<int(int,int)> func//表示接受两个int,返回一个int的可调用函数
function<int(int,int)> func = [&](int a,int b){
};//可以等于某个匿名函数
Using the function function does not need to pass in the parameters in the main function, and can be directly captured by reference.