容器分类
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/22d0ed679a1c7a6962aeb4c7ee856697.png)
- array
- vector
- qeque
- list
- forward-list
- set/multiset
- map/multimap
- unordered set/multiset
- unordered map/multimap
- hashtable
容器测试(测试创建时间和排序时间)
#include <iostream>
#include <array>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <list>
#include <forward_list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
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,"%d",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;
}
}
int main() {
srand(time(NULL));
const long ASIZE = 1000000;
array<long,ASIZE> c;
clock_t timeStart = clock();
for (long i = 0; i < ASIZE; ++i) {
c[i] = rand();
}
cout << "array创建数据所用CPU时间:" << (clock() - timeStart) << endl;
cout << "array 尺寸:" << c.size() << endl;
cout << "array 首元素:" << c.front() << endl;
cout << "array 末元素:" << c.back() << endl;
cout << "array data(内存起点地址):" << c.data() << endl;
timeStart = clock();
qsort(c.data(),ASIZE, sizeof(long),compareLongs);
cout << "qsort 所用的cpu时间:" << (clock() - timeStart) << endl;
vector<string> vc;
timeStart = clock();
char buf[10];
for (long j = 0; j < ASIZE; ++j) {
try {
snprintf(buf,10,"%d",rand());
vc.push_back(string(buf));
}catch (exception& p){
cout << "异常信息" <<endl;
abort();
}
}
cout << "vector创建数据所用CPU时间:" << (clock() - timeStart) << endl;
cout << "vector 尺寸:" << vc.size() << endl;
cout << "vector 容量:" << vc.capacity() << endl;
cout << "vector 首元素:" << vc.front() << endl;
cout << "vector 末元素:" << vc.back() << endl;
cout << "vector data(内存起点地址):" << vc.data() << endl;
timeStart = clock();
sort(vc.begin(),vc.end());
cout << " vector sort 所用的cpu时间:" << (clock() - timeStart) << endl;
list<string> vl;
timeStart = clock();
for (long j = 0; j < ASIZE; ++j) {
try {
snprintf(buf,10,"%d",rand());
vl.push_back(string(buf));
}catch (exception& p){
cout << "异常信息" <<endl;
abort();
}
}
cout << "list创建数据所用CPU时间:" << (clock() - timeStart) << endl;
cout << "list 尺寸:" << vl.size() << endl;
cout << "list 可创建元素数:" << vl.max_size() << endl;
cout << "list 首元素:" << vl.front() << endl;
cout << "list 末元素:" << vl.back() << endl;
timeStart = clock();
vl.sort();
cout << "list sort 所用的cpu时间:" << (clock() - timeStart) << endl;
forward_list<string> vfl;
timeStart = clock();
for (long j = 0; j < ASIZE; ++j) {
try {
snprintf(buf,10,"%d",rand());
vfl.push_front(string(buf));
}catch (exception& p){
cout << "异常信息" <<endl;
abort();
}
}
cout << "forward_list创建数据所用CPU时间:" << (clock() - timeStart) << endl;
cout << "forward_list 可创建元素数:" << vfl.max_size() << endl;
cout << "forward_list 首元素:" << vfl.front() << endl;
timeStart = clock();
vfl.sort();
cout << "forward_list sort 所用的cpu时间:" << (clock() - timeStart) << endl;
deque<string> vde;
timeStart = clock();
for (long j = 0; j < ASIZE; ++j) {
try {
snprintf(buf,10,"%d",rand());
vde.push_back(string(buf));
}catch (exception& p){
cout << "异常信息" <<endl;
abort();
}
}
cout << "deque创建数据所用CPU时间:" << (clock() - timeStart) << endl;
cout << "deque 可创建元素数:" << vde.max_size() << endl;
cout << "deque 首元素:" << vde.front() << endl;
cout << "deque 末元素:" << vde.back() << endl;
timeStart = clock();
sort(vde.begin(),vde.end());
cout << "deque sort 所用的cpu时间:" << (clock() - timeStart) << endl;
stack<string> sta;
timeStart = clock();
for (long j = 0; j < ASIZE; ++j) {
try {
snprintf(buf,10,"%d",rand());
sta.push(string(buf));
}catch (exception& p){
cout << "异常信息" <<endl;
abort();
}
}
cout << "stack创建数据所用CPU时间:" << (clock() - timeStart) << endl;
cout << "stack 元素数:" << sta.size() << endl;
cout << "stack top元素:" << sta.top() << endl;
timeStart = clock();
cout << "stack sort 所用的cpu时间:" << (clock() - timeStart) << endl;
queue<string> que;
timeStart = clock();
for (long j = 0; j < ASIZE; ++j) {
try {
snprintf(buf,10,"%d",rand());
que.push(string(buf));
}catch (exception& p){
cout << "异常信息" <<endl;
abort();
}
}
cout << "queue创建数据所用CPU时间:" << (clock() - timeStart) << endl;
cout << "queue 元素数:" << que.size() << endl;
cout << "queue 首元素:" << que.front() << endl;
cout << "queue 末元素:" << que.back() << endl;
timeStart = clock();
cout << "queue sort 所用的cpu时间:" << (clock() - timeStart) << endl;
multiset<string> ms;
timeStart = clock();
for (long j = 0; j < ASIZE; ++j) {
try {
snprintf(buf,10,"%d",rand());
ms.insert(string(buf));
}catch (exception& p){
cout << "异常信息" <<endl;
abort();
}
}
cout << "multiset创建数据所用CPU时间:" << (clock() - timeStart) << endl;
cout << "multiset 元素数:" << ms.size() << endl;
cout << "multiset 可创建元素数:" << ms.max_size() << endl;
timeStart = clock();
cout << "multiset sort 所用的cpu时间:" << (clock() - timeStart) << endl;
multimap<long,string> mm;
timeStart = clock();
for (long j = 0; j < ASIZE; ++j) {
try {
snprintf(buf,10,"%d",rand());
mm.insert(pair<long,string>(j,buf));
}catch (exception& p){
cout << "异常信息" <<endl;
abort();
}
}
cout << "multimap创建数据所用CPU时间:" << (clock() - timeStart) << endl;
cout << "multimap 元素数:" << mm.size() << endl;
cout << "multimap 可创建元素数:" << mm.max_size() << endl;
timeStart = clock();
cout << "multimap sort 所用的cpu时间:" << (clock() - timeStart) << endl;
return 0;
}