头文件
#include <bits/stdc++.h>
#include <algorithm.h>
#include <limits.h>
#include <cmath.h>
sort 从大到小排序
sort(stus.begin(), stus.end(), [](Student& x, Student& y) {
return x.age > y.age;
});
bool cmp (Student& x, Student& y) {
return x.age > y.age;
}
sort(stus.begin(), stus.end(), cmp);
struct cmp {
bool operator()(const Student& x, const Student& y) {
return x.age > y.age;
}
};
sort(stus.begin(), stus.end(), cmp());
priority_queue 从大到小排序
auto cmp = [](Student& x, Student& y) {
return x.age > y.age;
};
priority_queue<Student, vector<Student>, decltype(cmp)> pq(cmp);
struct cmp {
bool operator()(Student& x, Student& y) {
return x.age < y.age;
}
};
priority_queue<Student, vector<Student>, cmp> pq;
将字符串按点划分为不同部分并获取数字
string str = "192.168.1.31";
int pos = 0;
while ((pos = str.find('.', 0)) != string::npos) {
cout << stoi(line.substr(0, pos)) << endl;
str.erase(0, pos + 1);
}
cout << stoi(line.substr(0, pos)) << endl;
按行获取数据
string line;
while (getline(cin, line)) {
int pos = 0;
while ((pos = line.find(' ')) != string::npos) {
cout << stoi(line.substr(0, pos)) << endl;
line.erase(0, pos + 1);
}
cout << stoi(line.substr(0, pos)) << endl << endl;
}
将 8 进制变为 16 进制
string eightToTen(string eight) {
int ten;
int len = eight.size();
for (int i = 0; i < len; ++i) {
int digit = eight[i] - '0';
ten += digit * pow(8, len - 1 - i);
}
return to_string(ten);
}
string tenToHex(string s_ten) {
int ten = stoi(s_ten);
string Hex;
while (ten > 0) {
int rem = ten % 16;
string digit;
if (rem < 10) {
digit = '0' + rem;
} else {
digit = 'A' + rem - 10;
}
Hex = digit + Hex;
ten /= 16;
}
return Hex;
}