您的位置:首页 > 编程语言 > C语言/C++

★一些实用的编程代码 C/C++

2012-11-16 22:08 357 查看
1.数组初始化     http://baike.baidu.com/view/982208.htm

int b[5000]; 

memset(b,0,sizeof(b)); 

2.sort、qsort       http://wenku.baidu.com/view/e064166daf1ffc4ffe47ac67.html

形式:

sort(t,t+len); 

sort(a,a+100,cmp);

qsort(a,n+1,sizeof(a[0]),cmp); 

3.获取数组长度  

C、C++中没有提供直接获取数组长度的函数(.length好像不行)    http://blog.csdn.net/shininghunter/article/details/7288021

为了获取一维数组长度,可用如下:length=sizeof(Array)/sizeof(int)

举例:数组排序后输出

#include <stdio.h>
#include<iostream>
#include <algorithm>
#include<string.h>
using namespace std;

int main()
{
int a[]={212,313,2,33,64,98,9,45,12,36,59,23,56,34,88,62,100,13,3113,646};
int length=sizeof(a)/sizeof(int) ;   //获取数组长度
sort(a,a+length);        //这里用到了sort
cout<<"排序后的结果为:【";
for (int i=0;i<length ; i++ )
{
if (i<length-1)
{
cout<<a[i]<<",";
}
else
cout<<a[i];
}
cout<<"】"<<endl;
return 0;
}

4.要求:“包含多组测试数据,请处理至文件结束(EOF)”

int main()
{
int n,d;
while(scanf("%d%d",&n,&d)!=EOF)//或者语句改成“while(~scanf("%d%d",&n,&k))  ”也行
{

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: