杂记

shougou* a = new shougou[number];

sort函数:
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector
//以普通函数的方式实现自定义排序规则
bool mycomp(int i, int j) {
    return (i < j);
}
//以函数对象的方式实现自定义排序规则
class mycomp2 {
public:
    bool operator() (int i, int j) {
        return (i < j);
    }
};
int main() {
    std::vector<int> myvector{ 32, 71, 12, 45, 26, 80, 53, 33 };
    //调用第一种语法格式,对 32、71、12、45 进行排序
    std::sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71) 26 80 53 33
    //调用第二种语法格式,利用STL标准库提供的其它比较规则(比如 greater<T>)进行排序
    std::sort(myvector.begin(), myvector.begin() + 4, std::greater<int>()); //(71 45 32 12) 26 80 53 33
   
    //调用第二种语法格式,通过自定义比较规则进行排序
    std::sort(myvector.begin(), myvector.end(), mycomp2());//12 26 32 33 45 53 71 80
    //输出 myvector 容器中的元素
    for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) {
        std::cout << *it << ' ';
    }
    return 0;
}
结构体的一些东西的回顾:
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct shougou {
	int price;
	int number;
};
bool cmp(shougou x, shougou y)
{
	return (x.price < y.price);
}
int main()
{
	int total, number,revenue=0;
	cin >> total >> number;
	shougou* a = new shougou[number];
	for (int n = 0; n < number; n++)
	{
		cin >> a[n].price >> a[n].number;
	}
	sort(a, a+number,cmp);
	
	for (int i = 0; i < number; i++)
	{
		if (total - a[i].number < 0)
		{
			revenue += total * a[i].price;
			break;
		}
		else {
			total -= a[i].number;
			revenue += a[i].number * a[i].price;
		}
	}
	cout << revenue;
}
原文地址:https://www.cnblogs.com/lwt99/p/14076146.html