C++ STL 体系结构与内核分析(二)容器

容器结构与分类

循序式:array固定死了/vector自动扩充/Deque双队列,先进先出/list-双向环状链表/Forward-List-单向链表可以放更多的元素/

大量查找需要Associative Containera:使用红黑树存储,左右自己平衡。set key和value不分,放的元素不能重叠/map包括value和key,放的元素不能重叠/Multiset或者Multimap可以重叠,内容可以重复/Unordered Containers:使用hashTable 来存储,一个栏代表一条链表,元素碰撞解决目前最多采用的方法。

Test-array

#define _CRT_SECURE_NO_WARNINGS
#include <array>
#include <iostream>
#include <ctime> 
#include <cstdlib> //qsort, bsearch, NULL
using namespace std;

namespace jj01
{
	const long ASIZE = 50000L;

	//用户输入需要查找的long类型元素
	long get_a_target_long()
	{
		long target = 0;

		cout << "target (0~" << RAND_MAX << "): ";
		cin >> target;
		return target;
	}

	//比较传入参数大小,a > b则返回1,否则返回-1
	int compareLongs(const void* a, const void* b)
	{
		return (*(long*)a - *(long*)b);
	}

	void test_array()
	{
		cout << "\ntest_array().......... \n";

		array<long, ASIZE> c;//50000个long类型元素

		clock_t timeStart = clock();//开始计时
		for (long i = 0; i< ASIZE; ++i) {//为array随机生成50000个long类型元素
			c[i] = rand();
		}
		cout << "milli-seconds : " << (clock() - timeStart) << endl;	//打印初始化array时间
		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), compareLongs);//排序
		long* pItem = (long*)::bsearch(&target, (c.data()), ASIZE, sizeof(long), compareLongs);//二分法查找
		cout << "qsort()+bsearch(), milli-seconds : " << (clock() - timeStart) << endl;	// 打印排序查找时间
		if (pItem != NULL)//判断是否找到
			cout << "found, " << *pItem << endl;
		else
			cout << "not found! " << endl;
	}
}

int main(void)
{
	jj01::test_array();
	return 0;
}

Test-vector

#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <stdexcept>
#include <string>
#include <cstdlib>//abort()
#include <cstdio>//sprintf()
/*
sprintf()主要功能是把格式化的数据写入某个字符串中,即发送格式化输出到 string 
所指向的字符串。sprintf 是个变参函数。使用sprintf 对于写入buffer
的字符数是没有限制的,这就存在了buffer溢出的可能性。解决这个问题,可
以考虑使用 snprintf函数,该函数可对写入字符数做出限制。
*/
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;

namespace jj02
{
	const int ASIZE = 50000L;//随机生成字符个数
	//用户输入需要查找的string类型元素
	string get_a_target_string()
	{
		long target = 0;
		char buf[10];

		cout << "target (0~" << RAND_MAX << ");";
		cin >> target;
		sprintf(buf, "%ld", target);
		return string(buf);
	}
	//字符串比较
	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;
	}

	void test_vector(const long& value)
	{
		cout << "\ntest_vector()...... \n";

		vector<string> c;//定义一个vector容器存放string1类型数据
		char buf[10];

		clock_t timeStart = clock();
		//将value个string送入vector中
		for (long i = 0; i < value; ++i)
		{
			try{//这里使用try...catch是为了避免value太大导致内存不够出现异常
				sprintf(buf, "%d", rand());
				c.push_back(string(buf));//将string送入vector中
			}
			catch(exception& p){
				cout << "i=" << i << " " << p.what() << endl;
				//曾經最高 i=58389486 then std::bad_alloc
				abort();
			}
		}
		cout << "milli-seconds : " << (clock() - timeStart) << endl;//打印初始化array时间
		cout << "vector.max_size()= " << c.max_size() << endl;	//1073747823
		cout << "vector.size()= " << c.size() << endl; // 打印vector长度
		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 << endl;//打印vector容量

		string target = get_a_target_string();//获得用户输入字符串
		////该代码块测试stl的find函数,查找用户输入字符串,并打印查找所花费时间
		{
			timeStart = clock();
			sort(c.begin(), c.end());//排序
			cout << "sort(), milli-second : " << (clock() - timeStart) << endl;//打印排序时间

			timeStart = clock();
			string* pItem = (string*)::bsearch(&target, (c.data()),//二分查找
				c.size(), sizeof(string), compareStrings);
			cout << "bsearch(), milli-seconds : " << (clock() - timeStart) << endl;//打印二分查找时间

			if (pItem != NULL)
				cout << "found, " << *pItem << endl << endl;
			else
				cout << "not found! " << endl << endl;

		}
	}
}

int main()
{
	jj02::test_vector(jj02::ASIZE);
	return 0;
}

输出:

test_vector()...... 
milli-seconds : 15311
vector.max_size()= 768614336404564650
vector.size()= 50000
vector.front()= 16807
vector.back()= 872041851
vector.data()= 0x10681f000
vector.capacity()= 65536

target (0~2147483647);

Test-list

#define _CRT_SECURE_NO_WARNINGS
#include <list>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio>  //snprintf()
#include <algorithm> //find()
#include <iostream>
#include <ctime> 
using namespace std;

namespace jj03
{
	const long ASIZE = 50000L;//随机生成字符数个数

	string get_a_target_string()
	{
		long target = 0;
		char buf[10];

		cout << "target (0~" << RAND_MAX << "): ";
		cin >> target;
		sprintf(buf, "%ld", target);
		return string(buf);
	}

	void test_list(const long& value)
	{
		cout << "\ntest_list()...... \n";

		list<string> c;
		char buf[10];

		clock_t timeStart = clock();
		for (long i = 0; i < value; ++i)
		{
			try{
				sprintf(buf, "%d", rand());
				c.push_back(string(buf));
			}
			catch (exception& p) {
				cout << "i=" << 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);//调用stl标准库算法find()
		cout << "std::find(), milli-seconds : " << (clock() - timeStart) << endl;

		if (pItem != c.end())
			cout << "found, " << *pItem << endl;
		else
			cout << "not found! " << endl;

		timeStart = clock();
		c.sort();//调用list内置sort()算法
		cout << "c.sort(), milli-seconds : " << (clock() - timeStart) << endl;

		c.clear();
	}
}

int main()
{
	jj03::test_list(jj03::ASIZE);
	return 0;
}

输出:

test_list()...... 
milli-seconds : 24671
list.size()= 50000
list.max_size()= 461168601842738790
list.front()= 16807
list.back()= 872041851
target (0~2147483647):

Test-deque

使用关联式容器multiset

multi-map-红黑树

unordered_multiset

容器set

使用容器map

分配器:内存分配

每一个容器使用的不同分配器(vector,list, set, unordered_set, unorder_map),如下图所示:

发布了176 篇原创文章 · 获赞 21 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_27262727/article/details/105144813