容器的分类与各种测试(三)——queue

queue是单端队列,但是在其实现上是使用的双端队列,所以在queue的实现上多用的是deque的方法。(只要用双端队列的一端只出数据,另一端只进数据即可从功能上实现单端队列)如下图

容器的分类与各种测试(三)——queue

例程

#include<stdexcept>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<queue>
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, "%ld", 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;
}
void test_queue(long& value)
{
	cout << "\ntest_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(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();                          //删除front元素
	cout << "queue.size()= " << c.size() << endl;
	cout << "queue.front()= " << c.front() << endl;	
	cout << "queue.back()= " << c.back() << endl;	
}
int main()
{
	long int value; 
	cout<<"how many elements: ";

	cin>>value;
	test_queue(value);
	return 0;
}

 运行结果

容器的分类与各种测试(三)——queue

 

相关推荐