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;
}