1. 程式人生 > >【 C/C++學習筆記整理】--2.break與return0、常用函式的用法

【 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);