c++中“计时器”、“sort”、“namespace”函数的用法
来源:互联网 发布:java oa 工作流 编辑:程序博客网 时间:2024/06/11 17:42
1. C++ 中的“计时器”
头文件: #include<windows.h>
DWORD dwStartTime = GetTickCount(); // 用dwStartTime记录起始时间
…………………… (程序段)
cout<<"耗时"<<GetTickCount( )-dwStartTime<<"ms"<<endl; //(输出编译器运行程序段时的时间)
注:poj中不支持 #include<windows.h>
2. C++ sort()函数的用法
template<class RanIt>
void sort(RanIt first, RanIt last); //--> 1)
template<class RanIt, class Pred>
void sort(RanIt first, RanIt last, Pred pr); //--> 2)
头文件:
#include <algorithm>
using namespace std;
1.默认的sort函数是按升序排。对应于1)
sort(a,a+n); //两个参数分别为待排序数组的首地址和尾地址
2.可以自己写一个cmp函数,按特定意图进行排序。对应于2)
例如:
int cmp( const int &a, const int &b ){
if( a > b )
return 1;
else
return 0;
}
sort(a,a+n,cmp);
是对数组a降序排序
又如:
int cmp( const POINT &a, const POINT &b ){
if( a.x < b.x )
return 1;
else
if( a.x == b.x ){
if( a.y < b.y )
return 1;
else
return 0;
}
else
return 0;
}
sort(a,a+n,cmp);
是先按x升序排序,若x值相等则按y升序排
与此类似的还有C中的qsort,以下同附上qsort的使用方法:
#include <stdlib.h>
格式 qsort(array_name,data_number,sizeof(data_type),compare_function_name) (void*)bsearch (pointer_to_key_word,array_name,find_number,
sizeof(data_type),compare_function_name)
e.g.
int Cmp(const void*a,const void *b)
{
int*pa=(int*)a,*pb=(int*)b;
if(*pa>*pb) return 1;
else if (*pa==*pb) return 0;
else return -1;
}
qsort(data,N,sizeof(int),Cmp); // 对int型数组进行快速排序(非降序排列)
p=(int*)bsearch(&a,data,n,sizeof(int),Cmp);
3. C++中命名空间 namespace 的用法
std::cout<<std::hex<<3.4<<std::endl;
cout << hex << 3.4 << endl;
#include<iostream>
#include<sstream>
#include<string>
using
namespace
std;
//one.h
charfunc(
char
);
classString{...};
//somelib.h
classString{...};
- c++中“计时器”、“sort”、“namespace”函数的用法
- C++sort函数的用法
- C++sort函数的用法
- C++sort函数的用法
- C++sort函数的用法
- C++sort函数的用法
- C++sort函数的用法
- C++sort函数的用法
- C++sort()函数的用法
- C++sort函数的用法
- C++sort函数的用法
- C++sort函数的用法
- C++sort()函数的用法
- C++sort()函数的用法
- C++sort函数的用法
- c++sort函数的用法
- C++:STL中sort()函数的用法总结
- 浅析C/C++中sort函数的用法
- 正弦内插算法
- HDU 3476 Cyclic Nacklice
- DDD实践问题之 - 关于论坛的帖子回复统计信息的更新的思考
- [深入理解Android卷二 全文-第六章]深入理解ActivityManagerService
- order 根据不同条件排序
- c++中“计时器”、“sort”、“namespace”函数的用法
- hkpDynamics->hkpRigidBody
- ./configure,make,make install的作用
- Why Coding Like this -------map函数揭秘
- android事件传递流程 onTouchEvent onInterceptTouchEvent()
- lua 高效hashmap (没调试)
- Why Coding Like this -------Filter函数揭秘
- hdu 1283 最简单的计算机
- UVA10177数出二维三维四维中的正方和矩方数