C++——容器之分类与各种测试
Posted Strive_LiJiaLe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++——容器之分类与各种测试相关的知识,希望对你有一定的参考价值。
文章目录
六大部件的关系
容器——结构与分类
set与multiset的区别:set中的key值(key就是value)不可以重复,而multiset可以重复。
实现底层是红黑树,保持高度平衡的二叉树。自动调整
sequence 容器
使用容器array——代码案例
#include <array>
#include <iostream>
#include <ctime>
#include <cstdlib> //qsort, bsearch, NULL
namespace jj01
void test_array()
cout << "\\ntest_array().......... \\n";
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; //array.data() :起始位地址
long target = get_a_target_long();//输入要寻找的目标
timeStart = clock();//该函数从运行开始到该指令的毫妙数
::qsort(c.data(), ASIZE, sizeof(long), compareLongs);//qsort():1.起始位地址;2.元素个数;3.类型大小;4.比较函数(策略)
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;
long get_a_target_long()
long target=0;
cout << "target (0~" << RAND_MAX << "): ";
cin >> target;
return target;
使用容器vector——代码案例
#include <vector>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <iostream>
#include <ctime>
#include <algorithm> //sort()
namespace jj02
void test_vector(long& value)//传过来元素个数
cout << "\\ntest_vector().......... \\n";
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;
//曾經最高 i=58389486 then std::bad_alloc,内存不够
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();
test_moveable(vector<MyString>(),vector<MyStrNoMove>(), value);
使用容器list——代码案例
#include <list>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <algorithm> //find()
#include <iostream>
#include <ctime>
namespace jj03
void test_list(long& value)
cout << "\\ntest_list().......... \\n";
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; //357913941
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();
cout << "c.sort(), milli-seconds : " << (clock()-timeStart) << endl;
c.clear();
test_moveable(list<MyString>(),list<MyStrNoMove>(), value);
使用容器forward list——代码案例
#include <forward_list>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <iostream>
#include <ctime>
namespace jj04
void test_forward_list(long& 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
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; //536870911
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();
使用容器deque——代码案例
分段连续——由内部连续的段组成。前后都可再次扩充段。
#include <deque>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <iostream>
#include <ctime>
namespace jj05
void test_deque(long& value)
cout << "\\ntest_deque().......... \\n";
deque<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 << "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; //1073741821
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();
sort(c.begin(), c.end());
cout << "sort(), milli-seconds : " << (clock()-timeStart) << endl;
c.clear();
test_moveable(deque<MyString>(),deque<MyStrNoMove>(), value);
关联式容器
使用容器multiset——代码案例
#include <set>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <iostream>
#include <ctime>
namespace jj06
void test_multiset(long容器之分类与各种测试——set
set和multiset的去别在于前者的key值不可以重复,所以用随机数作为其元素进行插入时,遇到重复元素就会被拒绝插入(但是程序不会崩溃)。
例程
#include<stdexcept>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<set>
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_set(long& value)
{
cout << "
test_set()..........
";
set<string> c;
char buf[10];
clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.insert(string(buf)); //重复元素会被拒绝插入
}
catch(exception& p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "set.size()= " << c.size() << endl;//元素个数
cout << "set.max_size()= " << c.max_size() << endl; //和计算机内存大小相关
string target = get_a_target_string();
{
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target); //比 c.find(...) 慢很多
cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
}
{
timeStart = clock();
auto pItem = c.find(target); //比 std::find(...) 快很多
cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
}
}
int main()
{
long int value;
cout<<"how many elements: ";
cin>>value;
test_set(value);
return 0;
}
查看运行结果是否和我们预测的一样
要求插入100万个元素,但是由于set本身不容许重复的特性,实际仅插入了998385个元素。
以上是关于C++——容器之分类与各种测试的主要内容,如果未能解决你的问题,请参考以下文章
容器之分类与各种测试——unordered-multimap