侯捷-STL与泛型编程笔记(第一讲、容器概述——4.容器的使用)

本文对比了C++标准库中不同容器(array、vector、list、forward_list、slist)在插入、查找和排序操作上的性能表现,揭示了每种容器在特定场景下的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

array

#include <array>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

#define RAND_MAX 0x7fff
const long ASIZE = 100000L;

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

int compareLongs(const void*a, const void* b)
{
	return (*(long*)a - *(long*)b);
}

void test_array() //主要运行函数![在这里插入图片描述](https://img-blog.csdnimg.cn/201811260011423.PNG)
{
	array<long, ASIZE> c;
	clock_t timeStart = clock();
	for (long i=0; i < ASIZE; ++i) {
		c[i] = rand();
	}
	cout << "milli-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;  //data表示第一个元素地址

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

在这里插入图片描述

结论:

  • 插入100000L个,用时10
  • qsort+bsearch查找用时153

vector

vector的内存是成倍增长(一个元素一个空间,两个元素两个空间,三个元素有四个空间,四个元素四个空间)存不下就找个新的内存,全部数据移动到新内存

#include <vector>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio>  //snprintf()
#include <iostream>
#include <ctime> 
#include <algorithm> 	//sort()
#include <stdio.h>

//vs2012使用snprintff
#if _MSC_VER
#define snprintf _snprintf
#endif

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 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)
{
	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(exception& p)
		{
			cout << "i=" << i << " " << p.what() << endl; //防止超过最大内存导致程序失败
			abort(); // 结束程序(exit表示结束,有清理动作,abort只是结束)
		}
	}
	cout << "milli-seconds : " << (clock()-timeStart) << endl;	
	cout << "vector.max_size()= " << c.max_size() << endl;	//1073747823
	cout << "vector.size()= " << c.size() << endl;		
	cout << "vector.front()= " << c.front() << endl;	
	cout << "vector.back()= " << c.back() << endl;	
	cout << "vector.data()= " << c.data() << endl;
	cout << "vector.capacity()= " << c.capacity() << endl << 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 << endl;
		}
		else
		{
			cout << "not found!" << endl << endl;
		}

		{
			timeStart = clock();
			sort(c.begin(), c.end());
			cout << "sort, milli-seconds :" << (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;
		}
		c.clear();
	}
}

在这里插入图片描述
结论:

  • 插入100000L个,用时1740
  • 占用138255个空间
  • find查找用时:52
  • sort用时:6670,bsearch查找用时0

list

在这里插入图片描述

void test_list(const long &value)
{
	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 (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);
	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;
	//list不使用bsearch

	c.clear();
}

在这里插入图片描述
结论:

  • 插入100000L个,用时1066
  • find查找用时81,
  • 自带的sort用时22922

forward_list(单向链表)

在这里插入图片描述

#include <forward_list>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio>  //snprintf()
#include <iostream>
#include <ctime> 
void test_forward_list(const long &value)
{
	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));
		}
		catch (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;

	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();
	c.sort();
	cout << "c.sort, milli-seconds :" << (clock()-timeStart) << endl;

	c.clear();
}

在这里插入图片描述
结论:

  • 插入100000L个,用时1130
  • find查找用时2
  • 自带的sort用时28547

slist

(单向链表,gnu独特的链表类似forward_list)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值