C++获取时间
时间:2019-01-17
本文章向大家介绍C++获取时间,主要包括C++获取时间使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
// ConsoleApplication5.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <deque>
#include <stdexcept>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>
using namespace std;
long get_a_target_long()
{
long target = 0;
cout << "target (0~" << RAND_MAX << "): ";
cin >> target;
return target;
}
string get_a_target_string()
{
long target = 0;
char buf[10];
cout << "target (0~" << RAND_MAX << "): ";
cin >> target;
snprintf(buf, 10, "%d", target);
return string(buf);
}
int compareLongs(const void* a, const void* b)
{
return (*(long*)a - *(long*)b);
}
int compareStrings(const void* a, const void* b)
{
if (*(string*)a > *(string*)b)
return 1;
else if (*(string*)a < *(string*)b)
return -1;
else
return 0;
}
#include <deque>
#include <stdexcept>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>
#include<algorithm>
using namespace std;
const long ASIZE = 100000L;
void test_deque(const long& value)
{
deque<string> c;
char buf[10];
clock_t timeStart = clock();
for (long i = 0; i < value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.push_back(string(buf));
}
catch(exception &p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds:" << (clock() - timeStart) << endl;
cout << "deque.size()=" << c.size() << endl;
cout << "deque.front()=" << c.front() << endl;
cout << "deque.back()=" << c.back() << endl;
cout << "deque.max_size()=" << c.max_size() << endl;
string target = get_a_target_string();
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target);
cout << "std::find(), milli-seconds:" << (clock() - timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
timeStart = clock();
sort(c.begin(), c.end());
cout << "sort,milli-seconds:" << (clock() - timeStart) << endl;
c.clear();
}
//////////////////////////multiset////////////////////////////////
#include <set>
#include <stdexcept>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>
#include <string>
void test_multiset(const long &value)
{
multiset<string> c;
char buf[10];
clock_t timeStart = clock();
for (long i = 0; i < value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.insert(string(buf));
}
catch (exception &p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds:" << (clock() - timeStart) << endl;
cout << "multiset.size()=" << c.size() << endl;
cout << "multiset.ma_size()=" << c.max_size() << endl;
string target = get_a_target_string();
{
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target);
cout << "std::find(), milli-seconds:" << (clock() - timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
}
{
timeStart = clock();
auto pItem = c.find(target);
cout << "c.find(), milli-seconds:" << (clock() - timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
}
c.clear();
}
//////////////////////////multimap////////////////////////////
#include <map>
#include <stdexcept>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>
void test_multimap(const long &value)
{
multimap<long, string> c;
char buf[10];
clock_t timeStart = clock();
for (long i = 0; i < value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
//multimap不可食用[]做insertion
c.insert(pair<long, string>(i, buf));
}
catch(exception &p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds:" << (clock() - timeStart) << endl;
cout << "multimap.size()=" << c.size() << endl;
cout << "multima.max_size()=" << c.max_size() << endl;
long target = get_a_target_long();
timeStart = clock();
auto pItem = c.find(target);
if (pItem != c.end())
cout << "found, milli-seconds: " << (clock() - timeStart) << endl;
else
cout << "not found!" << endl;
c.clear();
}
///////////////////////////////unordered set
#include <unordered_set>
#include <stdexcept>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>
void test_unordered_multiset(const long &value)
{
unordered_multiset<string> c;
char buf[10];
clock_t timeStart = clock();
for (long i = 0; i < value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.insert(string(buf));
}
catch (exception &p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds:" << (clock() - timeStart) << endl;
cout << "unordered_multiset.size()=" << c.size() << endl;
cout << "unordered_multiset.max_size()=" << c.max_size() << endl;
cout << "unordered_multiset.bucket_count()=" << c.bucket_count() << endl;
cout << "unordered_multiset.load_factor()=" << c.load_factor() << endl;
cout << "unordered_multiset.max_load_factor()=" << c.max_load_factor() << endl;
cout << "unordered_multiset.max_bucket_count()=" << c.max_bucket_count() << endl;
for (unsigned i = 0; i < 20; ++i)
cout << "bucket #" << i << "has " << c.bucket_size(i) << "elemets.\n";
string target = get_a_target_string();
{
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target);
cout << "std::find(), milli-seconds:" << (clock() - timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
}
{
timeStart = clock();
auto pItem = c.find(target);
cout << "c.find(), milli-seconds: " << (clock() - timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
}
c.clear();
}
/////////////////////////////////////////////////
#include <unordered_map>
#include <stdexcept>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>
void test_unordered_multimap(const long&value)
{
unordered_multimap<long, string> c;
char buf[10];
clock_t timeStart = clock();
for (long i = 0; i < value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.insert(pair<long, string>(i, buf));
}
catch (exception &p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds:" << (clock() - timeStart) << endl;
cout << "unodered_multimap.size()=" << c.size() << endl;
cout << "unordered_multimap.max_size()=" << c.max_size() << endl;
long target = get_a_target_long();
timeStart = clock();
auto pItem = c.find(target);
cout << "c.find(), milli-seconds : " << (clock() - timeStart) << endl;
if (pItem != c.end())
cout << "found, value=" << (*pItem).second << endl;
else
cout << "not found! " << endl;
}
#include <windows.h>
#include <stdio.h>
int main(void)
{
SYSTEMTIME sys;
GetLocalTime(&sys);
printf("%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n"
, sys.wYear, sys.wMonth, sys.wDay
, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds
, sys.wDayOfWeek);
return 0;
}
- 掌握机器学习数学基础之概率统计(三)
- IBatisNet 之 自动生成主关键字
- IBatisNet之获取和操作SQL语句
- 大数据研究学者谈城市运行安全:要将应急处置转化为风险管理
- Castle.MVC框架介绍
- 在 .Net 设定 proxy 的方法
- MVC结构简介
- 优酷、爱奇艺、摩拜……多家网络平台被曝注册容易注销难!面临个人隐私泄露风险
- WordPress中借助.htaccess屏蔽某个IP或某个IP段(防垃圾评论)
- ASP.NET 调味品:AJAX
- CTreeCtrl 控件使用总结
- 高盛成立交易部门,涉足比特币和加密货币交易
- WordPress主题开发:添加主题更新提醒功能
- WordPress主题开发:添加主题更新提醒功能
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法