using namespace std;
struct Node{
int num;
string name;
Node(int num, string name){
this->num = num;
this->name = name;
}
};
bool cmp(Node n1, Node n2){
return n1.num > n2.num; // 升序
}
// 有两个需要比较的字段时
struct node{
int x, y;
};
bool cmp2(node n1, node n2){
if(n1.x!=n2.x){
return n1.x < n2.x;
}
return n1.y > n2.y;
}
int main(){
int x = 10, y = 20;
printf("%d\n", max(x, y));
printf("%d\n", min(x, y));
swap(x, y);
printf("x: %d, y: %d\n", x, y);
int a[5] = {1, 2, 3, 4, 5};
// 不能在这之前对a数组调用reverse
// 全排列
do{
printf("%d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4]);
}while (next_permutation(a, a + 5));
vector<int> v = {53, 23, 3, 5, 36, 7};
// [it, it2)
reverse(a, a + 5);
reverse(v.begin(), v.end());
for (int i = 0; i < 5; ++i) {
printf("%d ", a[i]);
}
printf("\n");
for (int i = 0; i < v.size(); ++i) {
printf("%d ", v[i]);
}
printf("\n");
// 将[a, a+5)填充为123
fill(a, a + 5, 123);
for (int i = 0; i < 5; ++i) {
printf("%d ", a[i]);
}
printf("\n");
// [s, e)
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); ++i) {
printf("%d ", v[i]);
}
vector<Node> vn;
// 结构体对象的两种创建
Node * n1 = new Node(3, "xiaowang");
Node * n2 = new Node(4, "xiaoli");
Node * n3 = new Node(1, "xiaomei");
Node n4 = {83, "dd"};
vn.push_back(*n1);
vn.push_back(*n3);
vn.push_back(*n2);
vn.push_back(n4);
sort(vn.begin(), vn.end(), cmp);
printf("\n");
for (int i = 0; i < vn.size(); ++i) {
printf("%d %s\n", vn[i].num, vn[i].name.c_str());
}
// 必须有序
int arr[10] = {1, 2, 3, 4, 4, 5, 5, 6, 6, 10};
// [s, e) 范围内第一个值大于等于val的元素的位置,返回指针或迭代器
int * p1 = lower_bound(arr, arr + 10, 3);
// [s, e) 范围内第一个值大于val的元素的位置,返回指针或迭代器
int * p2 = upper_bound(arr, arr + 10, 3);
// 对应的索引为
printf("ind: %d ", p1 - arr);
printf("val: %d \n", *p1);
printf("ind: %d ", p2 - arr);
printf("val: %d ", *p2);
vector<int> vv = {1, 2, 3, 4, 5};
vector<int>::iterator it = upper_bound(vv.begin(), vv.end(), 3);
printf("\nval: %d ind: %d", *it, it - vv.begin());
return 0;
}