Map和vector的数据查询及插入测试
#include <iostream>
#include <vector>
#include <iomanip>
#include <memory>
#include <ctime>
#include <map>
using namespace std;
int main()
{
map<int, int>st1;
vector<int> st2;
vector<int>st3;
clock_t time1 = clock();
clock_t time2 = 0;
//map测试
for (int i = 0; i < 90000; i++)
{
st1.insert({ i, i*i });
}
time2 = clock();
cout << "map测试:" << right << setw(8) << time2 - time1 << "毫秒。" << endl;
time1 = clock();
//vector测试
for (int i = 0; i < 90000; i++)
{
st2.push_back(i);
st3.push_back(i*i);
}
time2 = clock();
cout << "vector测试:" << right << setw(8) << time2 - time1 << "毫秒。" << endl;
//map查询测试
int findpos = st1.size() / 2;
cout << findpos << " " <<st1.find(findpos)->second << endl;;
time2 = clock();
cout << "map查找测试:" << right << setw(8) << time2 - time1 << "毫秒。" << endl;
time1 = clock();
//vector查询测试
int sz = st2.size() / 2;
cout<< sz<<" "<<*(find(st2.cbegin(), st2.cend(), sz));
time2 = clock();
cout << "vector查找测试:" << right << setw(8) << time2 - time1 << "毫秒。" << endl;
测试结果: