侯捷STL 5. 容器之分类与各种测试 (三)

1. 测试 list

1.1. 测试代码

#include<list>
#include<stdexcept>
#include<string>
#include<cstdlib>//abort()
#include<cstdio>
#include<iostream>
#include<ctime>
#include<algorithm>
namespace tlist
{
	void test_list()
	{
		srand((unsigned int)time(NULL));

		long value = 0;
		cout << "how many element:";
		cin >> value;

		cout << "\ntest_list()............. \n";

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

		clock_t timeStart = clock();

		for (long i = 0; i < value; i++)
		{
			try
			{
				//将后面的内容 即 " " 内的内容写入缓冲区,写入个数10个
				snprintf(buf, 10, "%d", rand());
				c.push_back(string(buf));
			}
			catch (exception& p)
			{
				//当内存溢出时的i
				cout << "i =" << i << " " << p.what() << endl;
				//abort() 中止程序执行,直接从调用的地方跳出
				abort();
			}
		}

		cout << "milli - second : " << (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 - second : " << (clock() - timeStart) << endl;

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

1.2. 测试结果

1.3. list 解读

  • list 不同于 vector,二者成长机制不同
  • list 是一个一个节点添加
  • vector是两倍增长,在另一个地方找两倍大的空间,再转移数据,成长过程缓慢
  • 容器中有 sort() 函数,而 algorithm 中也有 sort()

2. 测试 forward_list

2.1. 测试代码

#include<forward_list>
#include<stdexcept>
#include<string>
#include<cstdlib>//abort()
#include<cstdio>
#include<iostream>
#include<ctime>
#include<algorithm>
namespace tfwlist
{
	void test_forward_list()
	{
		srand((unsigned int)time(NULL));

		long value = 0;
		cout << "how many element:";
		cin >> 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
			{
				//将后面的内容 即 " " 内的内容写入缓冲区,写入个数10个
				snprintf(buf, 10, "%d", rand());
				c.push_front(string(buf));
			}
			catch (exception& p)
			{
				//当内存溢出时的i
				cout << "i =" << i << " " << p.what() << endl;
				//abort() 中止程序执行,直接从调用的地方跳出
				abort();
			}
		}

		cout << "milli - second : " << (clock() - timeStart) << endl;
		cout << "forward_list.max_size() = " << c.max_size() << endl;
		cout << "forward_list.front() = " << c.front() << endl;
		//forward_list 没有以下两个接口
		//cout << "forward_list.back() = " << c.back() << endl;
		//cout << "forward_list.size() = " << c.size() << endl;

		string target = get_a_target_string();

		timeStart = clock();
		auto pItem = ::find(c.begin(), c.end(), target);
		cout << "::find(),milli - second : " << (clock() - timeStart) << endl;

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

2.2. 测试结果

2.3. forward_list 解读

  • forward_list 不同于 list
  • forward_list 是单向的,list 是双向的
  • forward_list 只有 push_front,而 list 二者都有
  • forward_list 没有 back() 和 size() 接口

3. 测试 slist (非标准库中的)

  • slist 与 forward_list 类似,结构是一样的,都是单向量表
  • 但是他是在gcc环境中的,头文件是<ext\slist>

3.1. 测试代码

#include<ext\slist>
#include<stdexcept>
#include<string>
#include<cstdlib>//abort()
#include<cstdio>
#include<iostream>
#include<ctime>
#include<algorithm>
namespace tslist
{
	void test_slist()
	{
		srand((unsigned int)time(NULL));

		long value = 0;
		cout << "how many element:";
		cin >> value;

		cout << "\ntest_slist()............. \n";

		__gnu_cxx::slist<string> c;
		char buf[10];

		clock_t timeStart = clock();

		for (long i = 0; i < value; i++)
		{
			try
			{
				//将后面的内容 即 " " 内的内容写入缓冲区,写入个数10个
				snprintf(buf, 10, "%d", rand());
				c.push_front(string(buf));
			}
			catch (exception& p)
			{
				//当内存溢出时的i
				cout << "i =" << i << " " << p.what() << endl;
				//abort() 中止程序执行,直接从调用的地方跳出
				abort();
			}
		}

		cout << "milli - second : " << (clock() - timeStart) << endl;
}

3.2. 测试结果

3.3. slist 解读

forward_list一样,区别是forward_list 是标准库中的,而slist不是

4. 测试 Deque(双端队列)

4.1. 测试代码

#include<deque>
#include<stdexcept>
#include<string>
#include<cstdlib>//abort()
#include<cstdio>
#include<iostream>
#include<ctime>
#include<algorithm>
namespace tdeque
{
	void test_deque()
	{
		srand((unsigned int)time(NULL));

		long value = 0;
		cout << "how many element:";
		cin >> value;

		cout << "\ntest_deque()............. \n";

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

		clock_t timeStart = clock();

		for (long i = 0; i < value; i++)
		{
			try
			{
				//将后面的内容 即 " " 内的内容写入缓冲区,写入个数10个
				snprintf(buf, 10, "%d", rand());
				c.push_front(string(buf));
			}
			catch (exception& p)
			{
				//当内存溢出时的i
				cout << "i =" << i << " " << p.what() << endl;
				//abort() 中止程序执行,直接从调用的地方跳出
				abort();
			}
		}

		cout << "milli - second : " << (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 是迭代器 deque<string>::itertor
		auto pItem = ::find(c.begin(), c.end(), target);
		cout << "::find(),milli - second : " << (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;
	}
}

4.2. 测试结果

4.3. Deque 解读

  • 在内存中表现为一段map,map的每个元素指向 1 个buffer
  • 每个buffer存放 8 个数据
  • 所以它可以前后扩充,如果往前添加元素,就在 97 前面,如果扩充到了最左侧还要扩充,就要继续开辟新的 buffer,向右同理
  • deque 是分段连续
  • 在遍历过程中,当遍历到 buffer 的尽头的时候,需要切换到另一个buffer进行继续遍历,这就需要重载++运算符
  • array 不能扩充,vector 两倍扩充,list一个个扩充,deque 每次扩充为一个 buffer
  • deque 没有sort() 接口,当容器有自己的 sort 接口的时候,就要调用自身的 sort(),如 list,forward_list

以下两种可以称作是容器适配器 因为他们是由另一个容器 deque 实现的

5. 测试 stack

5.1. 测试代码

#include<stack>
#include<stdexcept>
#include<string>
#include<cstdlib>//abort()
#include<cstdio>
#include<iostream>
#include<ctime>
#include<algorithm>
namespace tstack
{
	void test_stack()
	{
		srand((unsigned int)time(NULL));

		long value = 0;
		cout << "how many element:";
		cin >> value;

		cout << "\ntest_stack()............. \n";

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

		clock_t timeStart = clock();

		for (long i = 0; i < value; i++)
		{
			try
			{
				//将后面的内容 即 " " 内的内容写入缓冲区,写入个数10个
				snprintf(buf, 10, "%d", rand());
				c.push(string(buf));
			}
			catch (exception& p)
			{
				//当内存溢出时的i
				cout << "i =" << i << " " << p.what() << endl;
				//abort() 中止程序执行,直接从调用的地方跳出
				abort();
			}
		}

		cout << "milli - second : " << (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;
	}
}

5.2. 测试结果

5.3. stack 解读

  • stack(堆栈) 先进后出 FILO
  • 由于这个特性,不提供 iterator ,因为这样就可能可以改变其中的元素,破坏了容器独特的性质
  • deque 涵盖了 stack
  • stack 源码含有一个 deque

6. 测试 queue

6.1. 测试代码

#include<queue>
#include<stdexcept>
#include<string>
#include<cstdlib>//abort()
#include<cstdio>
#include<iostream>
#include<ctime>
#include<algorithm>
namespace tqueue
{
	void test_queue()
	{
		srand((unsigned int)time(NULL));

		long value = 0;
		cout << "how many element:";
		cin >> value;

		cout << "\ntest_queue()............. \n";

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

		clock_t timeStart = clock();

		for (long i = 0; i < value; i++)
		{
			try
			{
				//将后面的内容 即 " " 内的内容写入缓冲区,写入个数10个
				snprintf(buf, 10, "%d", rand());
				c.push(string(buf));
			}
			catch (exception& p)
			{
				//当内存溢出时的i
				cout << "i =" << i << " " << p.what() << endl;
				//abort() 中止程序执行,直接从调用的地方跳出
				abort();
			}
		}

		cout << "milli - second : " << (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;
	}
}

6.2. 测试结果

6.3. queue 解读

  • 先进先出 FIFO
  • 由于这个特性,不提供 iterator ,因为这样就可能可以改变其中的元素,破坏了容器独特的性质
  • pop() 与 stack 不同,queue pop() 在队首
  • 源码中也含有 deque
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值