zoukankan      html  css  js  c++  java
  • vector数据查找方法

    用STL编敲代码时常常使用vector容器来存储数据。当容器中的数据有序时我们能够採取两种方式:

    (1) 利用<algorithm>中的find函数进行查找;

    (2) 折半查找。

    另外也能够将数据存入hash_map中进行查找,以下来測试比較这两种方法的时间效率。

    1. 測试数据集

    生成比99999小的全部素数作为查询数据集,查找2到99999之间的全部数。

    令数组A存储2~99999之间的全部数。则生成素数的方式

    (1) 找到当前最小的数字min;

    (2) 然后删除min的全部倍数。

    反复这两个过程直到A中全部的数字处理完成,即找到了2~99999之间的全部素数。

    2. 效率比較

    利用find函数查找须要2745ms,利用折半与hash_map均仅仅须要0ms。

    当数字添加到999999时,折半耗时63ms,hash_map耗时31ms。

    当数字添加到9999999时,折半耗时577ms,hash_map耗时499ms。

    注:hash_map中无法初始化桶的个数会减少hash的速度。

    (欢迎大家告知怎样初始化)

    3. 分析

    实际遇到的问题:在处理大规模图数据的过程中遇到了vector能存储全然部的图数据,而hash_map却不能。即vector存储的数据规模比hash_map大。

    折半查找仅仅能用于有序的数据的查找,而find无要求。

    4. 參考代码

    #include <string>
    #include <sstream>
    #include <time.h>
    #include <algorithm>
    #include <vector>
    #include <iostream>
    #include <hash_map>
    using namespace std;
    
    
    class compare
    {
    	vector<int> dataVector;
    	vector<int> findData;
    	hash_map<int, int> dataHash;
    public:
    	compare();
    	~compare(void);
    	void generalPrime();
    	void findTest();
    	void binSearch();
    	void hashTest();
    };
    
    compare::compare()
    {
    	generalPrime();
    }
    
    
    compare::~compare(void)
    {
    	findData.clear();
    	dataVector.clear();
    }
    
    void compare::findTest()
    {
    	clock_t startTime = clock();
    	vector<int>::iterator result;
    	int exist = 0;
    	for (vector<int>::iterator it = findData.begin(); it < findData.end(); it++)
    	{
    		result = find(dataVector.begin(), dataVector.end(), *it);
    		if (result != dataVector.end())
    		{
    			//查找成功
    			exist++;
    		}
    	}
    	clock_t endTime = clock();
    	cout << "exist num: " << exist << " find time " << (double)(endTime - startTime)/CLOCKS_PER_SEC*1000 << "ms" <<endl;
    }
    
    void compare::binSearch()
    {
    	int start;
    	int end;
    	int middle;
    	int exist = 0;
    	clock_t startTime = clock();
    	for (vector<int>::iterator it = findData.begin(); it < findData.end(); it++)
    	{
    		start = 0;
    		end = dataVector.size() - 1;
    		middle = (start + end) / 2;
    		while (start <= end)
    		{
    			if (*it < dataVector[middle])
    			{
    				end = middle - 1;
    			}
    			else if (*it > dataVector[middle])
    			{
    				start = middle + 1;
    			}
    			else
    			{
    				break;
    			}
    			middle = (start + end) / 2;
    		}
    		if (start <= end)
    		{
    			exist++;
    		}
    	}
    	clock_t endTime = clock();
    	cout << "exist num: " << exist << " binsearch time: " << (double)(endTime - startTime)/CLOCKS_PER_SEC * 1000 << "ms" << endl;
    }
    
    void compare::generalPrime()
    {
    	int maxPrime = 99999;
    	int flag;
    	vector<bool> visited(maxPrime, true);
    	for (int i = 2; i < maxPrime; ++i)
    	{
    		findData.push_back(i);
    		if (visited[i])
    		{
    			dataVector.push_back(i);
    			dataHash[i] = 1;
    			flag = i;
    			for (int ii = 2, flag = i * ii; flag < maxPrime; ++ii, flag *= ii)
    			{
    				visited[flag] = false;
    			}
    		}
    	}
    }
    
    void compare::hashTest()
    {
    	clock_t startTime = clock();
    	int exist = 0;
    	vector<int>::iterator result;
    	for (vector<int>::iterator it = findData.begin(); it < findData.end(); it++)
    	{
    		if (dataHash.find(*it) != dataHash.end())
    		{
    			exist++;
    		}
    	}
    	clock_t endTime = clock();
    	cout << "exist num: " << exist << " hash time " << (double)(endTime - startTime)/CLOCKS_PER_SEC*1000 << "ms" << endl;
    }
    int main()
    {
    	compare com;
    	com.findTest();
    	com.binSearch();
    	com.hashTest();
    	return 1;
    }


  • 相关阅读:
    如何修改Myeclipse的JSP模板
    解决----------“win10,不能打字了,已禁用IME”
    Scala学习之For、Function、Lazy(4)
    Scala学习之Tuple、Map、Array
    PHP Cookies
    PHP Cookies
    PHP 文件处理
    PHP include 和 require
    sqlserver2012 评估期已过问题处理
    PHP preg_match正则表达
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5183069.html
Copyright © 2011-2022 走看看