博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ ACM基础
阅读量:4547 次
发布时间:2019-06-08

本文共 3496 字,大约阅读时间需要 11 分钟。

一、C++结构体

#include 
using namespace std;struct Point{ int x; int y; Point(int x=0,int y=0):x(x),y(y){}};Point operator +(const Point &A,const Point &B){ return Point(A.x+B.x,A.y+B.y);}ostream& operator <<(ostream &out,const Point A){ out<<'('<
<<','<
<<')'<

注意

  1. 在C++中定义struct类型,可以直接用,可以不再用typedef取别名
  2. 对结构体编写重构函数参考实例如上
  3. 结构体可以有成员函数,而且有它独有的初始化方式,
  4. C++中的函数参数可以拥有默认值
  5. C++结构体可以有多个构造函数
  6. 以上,同样在class中适用

二、模板

#include 
using namespace std;struct Point{ int x; int y; Point(int x=0,int y=0):x(x),y(y){}};Point operator +(const Point &A,const Point &B){ return Point(A.x+B.x,A.y+B.y);}ostream& operator <<(ostream &out,const Point A){ out<<'('<
<<','<
<<')'<
T sum(T *p,T *q){ T result=0;//initiate 0***** while(p!=q){ result=result+*p; p++; } return result;}int main(){ Point points[4]={Point(0,0),Point(1,1),Point(2,2),Point(3,3)}; cout<
<

三、STL

3.1 排序与检索

例题1

现有N个大理石,每个大理石上写了一个非负整数、首先把各数从小到大排序;然后回答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上写着x。排序后的大理石从左到右编号为1~N。

(在样例中,为了节约篇幅,所有大理石的数合并到一行,所有问题也合并到一行。)
样例输入:

4 1

2 3 5 1
5
5 2
1 3 3 3 1
2 3

样例输出:

CASE# 1:

5 found at 4
CASE# 2:
2 not found
3 found at 3

#define LOCAL#include
#include
using namespace std;const int MAX=100;int main(){ #ifdef LOCAL freopen("data.in","r",stdin); freopen("data.out","w",stdout); #endif // LOCAL int n,q; int object; int time=0; int array[MAX]; while(scanf("%d%d",&n,&q)==2 && n){ printf("CASE# %d:\n",++time); //input n numbers int i=n; while(i--)scanf("%d",&array[i]); sort(array,array+n);//use sort c++ gives while(q--){ scanf("%d",&object); //find int p=lower_bound(array,array+n,object)-array;//to find the index of the object in array if(array[p] == object) printf("%d found at %d\n",object,p+1); else printf("%d not found\n",object); } } return 0;}

以上注意:

  1. 重定向操作,在本地可以这么做,放到oj上要改变写法,所以用ifdef的预定义方式。
  2. sort是一个模板函数,可以接收任何有<运算的类型
  3. 可以对普通数组array进行排序,也可以对vector进行排序,vector的排序方式为sort(v.begin(),v.end());
  4. lower_bound()函数接受三个参数,一二两个是数组的起始位置,第三个是待查找的元素,表示在数组中寻找第一次大于或者等于待查元素的位置。
  5. scanf()的返回值:正确输入的变量个数,注意正确包括类型正确,个数正确。

3.2 vector

3.3 set

例题

输入一个文本,找出所有不同的单词,按字典序从小到大进行输出,单词不区分大小写。

样例输入:

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the
road. the sign read:"Disneyland Left."
So they went home.

样例输出:

a

adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when

#define LOCAL#include
#include
#include
#include
using namespace std;set
dict;int main(){ #ifdef LOCAL freopen("data.in","r",stdin); freopen("data.out","w",stdout); #endif string line,x; while(cin>>line){ //1.to lower case for(int i=0;i
>x) dict.insert(x); } for(set
::iterator it=dict.begin();it!=dict.end();++it){ cout<<*it<<'\n'; } return 0;}

代码中用到了一些自带的函数如isapha()、tolower()等,具体使用参见

注意:

  1. 要对输入样例进行处理,非字母要变成空格,大写字母变成小写字母。
  2. 在将string进行插入到set的时候就已经按照字典序插入的了,因为string类型的<定义,就是定义为字典序的。

四、总结

vector、set和map的速度都很快,其中vector的速度接近数组,而set和map的速度也远远超过“用一个vector保存所有,然后逐个查找”的速度,set和map每次插入,查找和删除时间和元素个数的对数呈线性关系。但有些对时间要求很高的题目中,STL可能成为性能瓶颈。

转载于:https://www.cnblogs.com/MarkKobs-blog/p/10451268.html

你可能感兴趣的文章
正则表达式限制文本框只能输入数字,小数点,英文字母,汉字
查看>>
一个改变this指向bind的函数,vue源代码
查看>>
浅谈redux 中间件的原理
查看>>
01背包问题-动态规划算法
查看>>
我要成为前端工程师!给 JavaScript 新手的建议与学习资源整理
查看>>
ubuntu android
查看>>
一个叫<NameValuePair>的东西~~~
查看>>
ssh三大框架实现(一)---登陆功能
查看>>
Java设计模式之工厂设计模式
查看>>
The Number of Inversions(逆序数)
查看>>
转发帖子链[CodeForces-522A]
查看>>
n个灯,k个人的开灯问题
查看>>
android studio 问题1
查看>>
oracle查重和oracle分页
查看>>
Javascript库
查看>>
几个基本算法
查看>>
Windows 7 (x64) 系统下安装与配置 Windows Live Writer 2012 16.4.3528.0331 图文详细教程
查看>>
关于利用ajax时,设置访问延时的方法
查看>>
jQuery如何获取动态添加的元素
查看>>
百度权重、360权重、Google PR值详解
查看>>