一、C++结构体

#include <iostream>
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<<'('<<A.x<<','<<A.y<<')'<<endl;
}
int main()
{
Point a(1,2);
Point b(2,3);
cout<<a+b<<endl;
}

注意

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

二、模板

#include <iostream>
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<<'('<<A.x<<','<<A.y<<')'<<endl;
}
// template sum
template<typename T>
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<<sum(points,points+4)<<endl;
}

三、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<cstdio>
#include<algorithm>
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

vector的使用方法请参见此链接

最新文章

  1. python urllib
  2. 前端MVVM框架avalon揭秘 - HTML编译器
  3. Dynamo和Bigtable对比
  4. Java for LeetCode 029 Divide Two Integers
  5. hql语句关联查询(select new )
  6. QString::toWCharArray可以拷贝到宽字符串里
  7. Part 57 to 58 Why should you override ToString and Equal Method
  8. asp.net mvc 生成条形码
  9. ul li 好友列表
  10. MySQL数据转移至MSSQL详解
  11. dp + 组合数 Codeforces Beta Round #9 (Div. 2 Only) D
  12. 懒人的小技巧, 批处理修改IP
  13. 结对编程1 (四则运算基于GUI)
  14. 简单实现弹出弹框页面背景半透明灰,弹框内容可滚动原页面内容不可滚动的效果(JQuery)
  15. 删除链表的倒数第N个节点
  16. Mac谷歌浏览器跨域
  17. python自动化开发-[第十七天]-django的ORM与其他
  18. Vue.js学习使用心得(三)
  19. J2EE中几个常用的名词解释
  20. 【C】C语言中的_exit()与exit()

热门文章

  1. 软件项目技术点(1)——Tween算法及缓动效果
  2. JavaScript中实现DI的原理
  3. makefile 通配符了解% $@ $^ $&lt;
  4. spfa 的算法实现之一
  5. SiP封装成超越摩尔定律的要塞,日月光/安靠/长电科技谁将赢取IC封装的未来
  6. 微软与Node.js的开源之旅
  7. cookie的初识和运用(js和jq)
  8. java之Pattern.compile相关正则表达式
  9. 安装python File &quot;/usr/bin/pip&quot;, line 11, in &lt;module&gt; sys.exit(main()) File &quot;/usr/lib/python3.4/site-packages/pip/__init__.py&quot;, line 215, in main locale.setlocale(locale.LC_ALL, &#39;&#39;) File &quot;/u
  10. mysql主从同步与防火墙端口的设定