【 C/C++学习笔记整理】--2.break与return0、常用函数的用法

  • 5.break和return 0  的区别

break 是跳出循环,执行循环体的外的程序;return 0  是结束程序,返回到main函数

6.sort()函数的用法

   sort(begin,end,cmp),cmp参数可以没有,如果没有默认升序排序。

使用比较器   cmp,降序排列。

bool cmp(int a,int b)
{
    return a>b;
}

或者#include<functional>   functional提供了一堆基于模板的比较函数对象。它们是(看名字就知道意思了):equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>

7.#define N 10000005
typedef long long ll;
ll a[N];  //定义一个大数组时,要在main函数之前定义

8.查找一个数组的最大最小值,使用  

 max=(*max_element(b,b+n));
 min=(*min_element(b,b+n));

9.输入整数个数不确定时,scanf 函数有返回值

while(scanf("%d",%x)==1){

}

int main()
{
	//freopen("data.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	vector<int> v;
	int n=0,k=0;
	int i;
	cin>>i;
	v.push_back(i);
	while(cin.get()!='\n'){
		cin>>i;
		v.push_back(i);
	}
	for(int i=0;i<v.size();i++){
		cout<<v[i]<<endl;}
	return 0;
}

将int型转换成double型数据    

int a,b;

(double)a/b;

不使用标准输入输出,使用文件的方式进行输入输出。只需在main()函数里加入

freopen("input.txt","r",stdin);

freopen("output","w",stdout);

猜你喜欢

转载自blog.csdn.net/wxq_1993/article/details/85292202