CCF C++

一、vector
  1. 找到vector中的某个元素。vector是没有find函数的,但是可以借助algorithm头文件中的find函数来实现这一功能。如果需要查找的元素是一个结构体,可以在结构体中重载“==”
#include<iostream> 
#include<vector>
#include<algorithm>
using namespace std;

struct Point {
	int x;
	int y;
	bool operator == (const Point& p) {  //重载运算符,以比较复杂结构体
		return (this->x == p.x) && (this->y == p.y);
	}
};
vector<Point> points;
bool findPoint(int x, int y) {
	Point p = { x,y };
	vector<Point>::iterator it = find(points.begin(), points.end(),p);
	if (it != points.end()) {
		return true;
	}
	else {
		return false;
	}
}
  1. 对vector中的元素进行排序
    借助于algorithm头文件中的sort函数,并可以自定义排序方法
#include<vector>
#include<algorithm>
using namespace std;

struct Point {
	int id;
	int distance;
};
vector<Point> points;
int x[200], y[200];
//排序方法
bool cmp(const Point& p1, const Point& p2) {
	if (p1.distance == p2.distance) {
		return p1.id < p2.id;
	}
	return p1.distance < p2.distance;
}
//排序
sort(points.begin(), points.end(), cmp);
	

二、输出格式

保留n位小数

#include<iomanip>
#include<iostream>
using namespace std;
cout<<fixed<<setprecision(n) << mid;
三、pair
pair<int, int> p = make_pair(2, 3);
cout << p.first << p.second << endl;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值