C++标准库与内核分析

时间:2022-07-06 19:48:05
#include<iostream>
#include<string>
#include<cstdlib>
using std::cin;
using std::cout;
using std::string;
string get_a_target_string()
{
	char buf[10];
	long target = 0;
	cout << "target (0~" << RAND_MAX << "):";
	cin >> target;
	snprintf(buf, 10, "%d", target);
	return string(buf);

}
long get_a_target_long()
{
	long target = 0;
	cout << "target (0~" << RAND_MAX << "):";
	cin >> target;
	return target;
}

int compartLongs(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<array>
#include<iostream>
#include<ctime>
#include<cstdlib>
namespace jj01
{
	//array数据结构为一个数组
	using namespace std;
	void test_array()
	{
		cout << "\ntest_array().............\n";
		const long ASIZE = 100000;
		array<long,ASIZE> c;
		clock_t timeStart = clock();
		for (long i = 0; i < ASIZE; i++)
		{
			c[i] = rand();
		}
		cout << "milll-seconds: " << (clock()-timeStart) << endl;
		cout << "array.size()= " << c.size() << endl;
		cout << "array.front()= " << c.front() << endl;
		cout << "array.back()= " << c.back() << endl;
		cout << "array.data()= " << c.data() << endl;
		long target = get_a_target_long();
		timeStart = clock();
		qsort(c.data(), ASIZE, sizeof(long), compartLongs);
		long* pItem = (long*)bsearch(&target, (c.data()), ASIZE, sizeof(long), compartLongs);
		cout << "qsort()+bsearch(),milli-seconds : " << (clock() - timeStart) << endl;
		if (pItem!=NULL)
		{
			cout << "found, " << *pItem << endl;
		}
		else
		{
			cout << "not found!" << endl;
		}

	}
}
#include<vector>
#include<stdexcept>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<ctime>
#include<algorithm>
namespace jj02
{
	using namespace std;
	//vector的空间是两倍增长的尾部可以插入元素的数组
	void test_vector(long& value)
	{
		cout << "\ntest_vector()............\n";
		vector<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 (const std::exception& p)
			{
				cout << "i=" << i << " " << p.what() << endl;
				abort();
			}
		}
		cout << "milll-seconds: " << (clock()-timeStart) << endl;
		cout << "vector.size()= " << c.size() << endl;   //size()真正元素的个数
		cout << "vector.front()= " << c.front() << endl;   //vector的第一个元素
		cout << "vector.back()= " << c.back() << endl;  //vector的最后一个元素
		cout << "vector.data()= " << c.data() << endl;    //呼叫整个vector的起始空间
		cout << "vector.capacity()= " << c.capacity() << endl;//capacity()表示真正容量有多大
		string target = get_a_target_string();
		{
			timeStart = clock();
			auto pItem = find(c.begin(), c.end(), target);
			cout << "::find(),mill-second:" << (clock() - timeStart) << endl;
			if (pItem != c.end())
				cout << "found, " << *pItem << endl;
			else
				cout << "not found" << endl;
		}
		timeStart = clock();
		sort(c.begin(), c.end());
		string* pItem = (string*)bsearch(&target, (c.data()), c.size(), sizeof(string), compareStrings);
		cout << "sort()+bsearch(),milli-seconds: " << (clock()-timeStart) << endl;
		if (pItem != NULL)
		{
			cout << "found, " << *pItem << endl;
		}
		else
		{
			cout << "not found!" << endl;
		}
	} 
}

#include<iostream>
#include<list>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<ctime>
namespace jj03
{
	//list数据结构为一个双向链表
	using namespace std;
	void test_list(long& value)
	{
		cout << "\ntest_list().........\n";
		list<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 (const std::exception& p)
			{
				cout << "i=" << " " << p.what() << endl;
				abort();
			}
		}
		cout << "milli-seconds:" << (clock() - timeStart) << endl;
		cout << "list.size():" << c.size() << endl;
		cout << "list.max_size():" << c.max_size() << endl;
		cout << "list.front():" << c.front() << endl;
		cout << "list.back():" << c.back() << endl;
		string target = get_a_target_string();
		timeStart = clock();
		auto pItem = find(c.begin(), c.end(), target);
		cout << "::find(),milli-seconds:" << (clock()-timeStart) << endl;
		if (pItem != c.end())
		{
			cout << "found, " << *pItem << endl;
		}
		else
		{
			cout << "not found!" << endl;
		}
		timeStart = clock();
		c.sort();   //list容器里面有自带的排序算法,所以我们选择自带的排序算法,效率比较高
		cout << "c.sort(),mill-seconds:" << (clock() - timeStart) << endl;
	}
}
#include<string>
#include<forward_list>
#include<ctime>
#include<cstdlib>
namespace jj04
{
	//forward_list数据结构为单向链表
	using namespace std;
	void test_forward_list(long& value)
	{
		cout << "\ntest_forward_list()............\n";
		forward_list<string> c;
		char buf[10];
		clock_t timeStart = clock();
		for (long i = 0; i < value; i++)
		{
			try
			{
				snprintf(buf, 10, "%d", rand());
				c.push_front(string(buf));  //注意forward_list只有push_front()放元素,没有push_back()方法
			}
			catch (const std::exception& p)
			{
				cout << "i=" << i << " " << p.what() << endl;
				abort();
			}
		}
		cout << "milli-seconds():" << (clock() - timeStart) << endl;
		cout << "forward_list.max_size():" << c.max_size() << endl; //最多可以放多少个元素
		cout << "forward_list.front():" << c.front() << endl;
		//注意,forward_list没有back()方法,也没有size()方法
		string target = get_a_target_string();
		timeStart = clock();
		auto pItem = find(c.begin(), c.end(), target);
		cout <<"::find(),milli-seconds:" << (clock() - timeStart) << endl;
		//这样写是不对的,因为pItem是一个迭代器,或者是泛型指针,但是不是指针,所以不可以这样写
		//if (pItem != NULL)
		if (pItem!=c.end())
		{
			cout << "found," << *pItem << endl;
		}
		else
		{
			cout << "not found!" << endl;
		}
		timeStart = clock();
		c.sort();
		cout << "c.sort(),milli-seconds:" << (clock() - timeStart) << endl;
	}
}
#include<deque>
namespace jj05
{
	//deque数据结构为双端队列
	using namespace std;
	void test_deque(long& value)
	{
		cout << "\ntest_deque()...............";
		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 (const std::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 << "::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;

	}
}

#include<stack>
namespace jj06
{
	using namespace std;
	void test_stack(long& value)
	{
		cout << "\ntest_stack()..............\n";
		stack<string> c;
		char buf[10];
		clock_t timeStart = clock();
		for (long i = 0; i < value; i++)
		{
			try
			{
				snprintf(buf, 10, "%d", rand());
				c.push(string(buf));
			}
			catch (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what() << endl;
				abort();
			}
		}
		//因为这里是栈,所以不存在查找,排序以及插入等功能
		cout << "milli-seconds:" << (clock() - timeStart) << endl;
		cout << "stack.size():" << c.size() << endl;
		cout << "stack.top():" << c.top() << endl;
		c.pop();
		cout << "stack.size():" << c.size() << endl;
		cout << "stack.top():" << c.top() << endl;
	}
}
#include<queue>
namespace jj07
{
	//queue数据结构为deque
	using namespace std;
	void test_queue(long& value)
	{
		cout << "test_queue()..............\n";
		queue<string> c;
		char buf[10];
		clock_t timeStart = clock();
		for (long i = 0; i < value; i++)
		{
			try
			{
				snprintf(buf, 10, "%d", rand());
				c.push(string(buf));
			}
			catch (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what() << endl;
				abort();
			}
		}
		cout << "milli-seconds:" << (clock() - timeStart) << endl;
		cout << "queue.size():" << c.size() << endl;
		cout << "queue.front():" << c.front() << endl;
		cout << "queue.back():" << c.back() << endl;
		c.pop();
		cout << "queue.size():" << c.size() << endl;
		cout << "queue.front():" << c.front() << endl;
		cout << "queue.back():" << c.back() << endl;
	}
}
#include<unordered_map>
namespace jj08
{
	//unordered_map的数据结构为哈希表
	using namespace std;
	void test_unordered_map(long& value)
	{
		cout << "\ntest_inordefed_map()..............";
		unordered_map<long, string> c;
		char buf[10];
		clock_t timeStart = clock();
		for (long i = 0; i < value; i++)
		{
			try
			{
				snprintf(buf, 10, "%d", rand());
				c[i] = string(buf);   //注意unordered_map插入元素的方法,i表示key,buf表示value
			}
			catch (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what() << endl;
				abort();
			}
		}
		cout << "milli-seconds:" << (clock() - timeStart) << endl;
		cout << "unordered_map.size():" << c.size() << endl;
		cout << "unordered_map.max_size():" << c.max_size() << endl;
		long target = get_a_target_long();
		timeStart = clock();
		//注意这里是调用unordered_map自己的查找方法,有自己的方法
		//一定要用自己的查找方法
		auto pItem = c.find(target);
		cout << "c.find(),milli-seconds:" << (clock() - timeStart) << endl;
		if (pItem!=c.end())
		{
			//注意这里取(*pItem).second表示取value
			cout << "found:" << (*pItem).second << endl;
		}
		else
		{
			cout << "not found!" << endl;
		}

	}
}
#include<unordered_set>
namespace jj09
{
	//unordered_set数据结构为哈希表
	using namespace std;
	void test_unordered_set(long& value)
	{
		cout << "\ntest_unordered_set()............\n";
		unordered_set<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 (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what();
				abort();
			}
		}
		cout << "milli-seconds:" << (clock() - timeStart) << endl;
		cout << "unordered_set.size()" << c.size() << endl;
		cout << "unordered_set.max_size()" << c.max_size() << endl;
		//输出篮子的个数
		cout << "unordered_set.bucker_count()" << c.bucket_count() << endl;
		//输出填充因子
		cout << "unordered_set.load_factor()" << c.load_factor() << endl;
		cout << "unordered_set.max_load_factor()"<<c.max_load_factor()<<endl;
		cout << "unordered_set.max_bucket_count()"<<c.max_bucket_count()<<endl;
		for (unsigned i = 0; i < 20; i++)
		{
			cout << "bucket:" << i << " has " << c.bucket_size(i) << " elements.\n";
		}
	}
}
#include<map>
namespace jj10
{
	using namespace std;
	void test_map(long& value)
	{
		cout << "\ntest_map().............\n";
		map<long, string> c;
		char buf[10];
		clock_t timeStart = clock();
		for (long i = 0; i < value; i++)
		{
			try
			{
				snprintf(buf, 10, "%d", rand());
				c[i] = string(buf);
			}
			catch (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what() << endl;
				abort();
			}
		}
		cout << "milli-seconds:" << (clock() - timeStart) << endl;
		cout << "map.size():" << c.size() << endl;
		cout << "map.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<set>
namespace jj11
{
	using namespace std;
	void test_multiset(long& value)
	{
		cout << "\ntest_multiset()..............\n";
		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 (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what() << endl;
				abort();
			}
		}
		cout << "milli-seconds:" << (clock() - timeStart) << endl;
		cout << "multiset.size():" << c.size() << endl;
		cout << "multiset.max_size():" << c.max_size() << endl;
		string target = get_a_target_string();
		{
			timeStart = clock();
			auto pItem = find(c.begin(), c.end(), target);
			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;
			}
		}
	}
}
#include<map>
namespace jj12
{
	using namespace std;
	void test_multimap(long& value)
	{
		cout << "\ntest_multimap()............\n";
		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 (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what() << endl;
				abort();
			}
		}
		cout << "milli-seconds:" << (clock() - timeStart) << endl;
		cout << "multimap.size():" << c.size() << endl;
		cout << "multimap.maxsize():" << 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:" << (*pItem).second << endl;  //因为这是一个pair,第一个参数是key,第二个参数是value
		}
		else
		{
			cout << "not found!" << endl;
		}
	}
}
namespace jj13
{
	using namespace std;
	void test_unordered_multiset(long& value)
	{
		cout << "\ntest_unordered_multiset().............\n";
		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 (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what();
				abort();
			}
		}
		cout << "milli-second:" << (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.bucket_count()" << c.max_bucket_count() << endl;
		for (unsigned i = 0; i < 20; i++)
		{
			cout << "bucket #" << i << " has " << c.bucket_size(i) << " elements \n";
		}
	}
}
namespace jj14
{
	using namespace std;
	void test_set(long& value)
	{
		cout << "\ntest_set()...........\n";
		set<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 (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what() << endl;
				abort();
			}
		}
		cout << "mili-seconds:" << (clock() - timeStart) << endl;
		cout << "set.size():" << c.size() << endl;
		cout << "set.max_size():" << c.max_size() << endl;
		string target = get_a_target_string();
		{
			auto pItem = find(c.begin(), c.end(), target);
			cout << "::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;
			}
		}
	}
}
namespace jj15
{
	using namespace std;
	void test_unordered_multimap(long& value)
	{
		cout << "\ntest_unordered_multimap()............\n";
		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());
				//multimap不可使用[]进行insertion
				c.insert(pair<long, string>(i, buf));
			}
			catch (const std::exception& p)
			{
				cout << "i:" << i << " " << p.what() << endl;
				abort();
			}
		}
		cout << "milli-seconds:" << (clock() - timeStart) << endl;
		cout << "unordered_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).first << endl;
		}
		else
		{
			cout << "not found!" << endl;
		}
	}
}
int main()
{
	long value;
	int select=0;
	cout << "please enter a select to test: 0 to quit: ";
    //cout << "select:";
	cin >> select;
	while (select!=0)
	{
		if (select!=1)
		{
			cout << "how many elements:";
			cin >> value;
		}
		switch (select)
		{
		case 1:
			jj01::test_array();
			break;
		case 2:
			jj02::test_vector(value);
			break;
		case 3:
			jj03::test_list(value);
			break;
		case 4:
			jj04::test_forward_list(value);
			break;
		case 5:
			jj05::test_deque(value);
			break;
		case 6:
			jj06::test_stack(value);
			break;
		case 7:
			jj07::test_queue(value);
			break;
		case 8:
			jj08::test_unordered_map(value);
			break;
		case 9:
			jj09::test_unordered_set(value);
			break;
		case 10:
			jj10::test_map(value);
			break;
		case 11:
			jj11::test_multiset(value);
			break;
		case 12:
			jj12::test_multimap(value);
			break;
		case 13:
			jj13::test_unordered_multiset(value);
			break;
		case 14:
			jj14::test_set(value);
			break;
		case 15:
			jj15::test_unordered_multimap(value);
			break;

		default:	
			break;
		}
		cout << "\nanother test? (0 to quit): ";
		cin >> select;

	}
	system("pause");
	return 0;
	
}