Linkedmap的C++實現
在網上找了半天,沒有找到能夠按照插入順序迭代輸出的map容器,所以自己按照java語言中Linkedmap的基本原理用C++實現了一個簡單的Linkedmap,結合了list容器的迭代特性和map容器的查詢效率等優點,有不足之處還望指正。
代碼:
//
// Linked_map.h
// --------------------
// Map 容器和鏈表的結合,實現按照插入順序排序輸出
//
// @author:zxinxiang
// @time: 2015.04.28
//
#ifndef __LINKED_MAP_H__
#define __LINKED_MAP_H__
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#pragma warning(disable: 4786)
#pragma warning(disable: 4503)
#include <map>
#include <list>
#include <assert.h>
class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private:
noncopyable(const noncopyable&);
const noncopyable& operator=(const noncopyable&);
};
// Note: assumes K and V are POD types.
template <typename K, typename V>
class Linked_map : private noncopyable
{
public:
// The type of a value in the map.
typedef std::pair<K, V> value_type;
// The type of a non-const iterator over the map.
typedef typename std::list<value_type>::iterator iterator;
// The type of a const iterator over the map.
typedef typename std::list<value_type>::const_iterator const_iterator;
typedef typename std::map<K, iterator>::iterator pIterator;
// Constructor.
Linked_map()
: sizes_(0)
{
}
// Destructor.
~Linked_map()
{
}
// Get an iterator for the beginning of the map.
iterator begin()
{
return values_.begin();
}
// Get an iterator for the beginning of the map.
const_iterator begin() const
{
return values_.begin();
}
// Get an iterator for the end of the map.
iterator end()
{
return values_.end();
}
// Get an iterator for the end of the map.
const_iterator end() const
{
return values_.end();
}
// Check whether the map is empty.
bool empty() const
{
return values_.empty();
}
// Find an entry in the map.
iterator find(const K& k)
{
if (sizes_ > 0)
{
pIterator it = mappeds_.find(k);
if (it == mappeds_.end())
return values_.end();
iterator find_it = it->second;
return find_it;
}
return values_.end();
}
// Find an entry in the map.
const_iterator find(const K& k) const
{
if (sizes_ > 0)
{
pIterator it = mappeds_.find(k);
if (it == mappeds_.end())
return values_.end();
const_iterator find_it = it->second;
return find_it;
}
return values_.end();
}
// Insert a new entry into the map.
std::pair<iterator, bool> insert(const value_type& v)
{
pIterator pIt = mappeds_.find(v.first);
if (pIt == mappeds_.end())
{
values_.push_back(v);
mappeds_.insert(pair<K, iterator>(v.first, --values_.end()));
++sizes_;
}
else
{
values_.erase(pIt->second);
values_.push_back(v);
pIt->second = --values_.end();
}
return std::pair<iterator, bool>(--values_.end(), true);
}
// Insert an element value to the list of the specified location.
iterator insert(iterator it, const value_type& v)
{
pIterator pIt = mappeds_.find(v.first);
if(pIt != mappeds_.end())
{
values_.erase(pIt->second);
--sizes_;
}
iterator _it = values_.insert(it, v);
mappeds_.insert(pair<K, iterator>(v.first, _it));
++sizes_;
return _it;
}
// Erase an entry from the map.
void erase(iterator it)
{
assert(it != values_.end());
assert(sizes_ != 0);
value_type v = *it;
values_.erase(it);
mappeds_.erase(v.first);
--sizes_;
}
// Erase a key from the map.
void erase(const K& k)
{
iterator it = find(k);
if (it != values_.end())
erase(it);
}
// Remove all entries from the map.
void clear()
{
// Clear the values.
values_.clear();
mappeds_.clear();
sizes_ = 0;
}
// Calculate the number of elements.
size_t size()
{
return sizes_;
}
private:
// The number of elements in the map.
size_t sizes_;
// The list of all values in the map.
std::list<value_type> values_;
// The map of list nodes.
std::map<K, iterator> mappeds_;
};
#endif // __LINKED_MAP_H__
Test測試代碼:
#include <iostream>
#include <string>
#include <ctime>
#include "Linked_map.h"
using namespace std;
int main(int argc, char** argv)
{
//測試效率
clock_t start = clock();
Linked_map<int,string> myMap;
for(int i = 0; i < 1000000; i++)
{
//插入節點
myMap.insert(pair<int,string>(i, "hello linked map!"));
}
clock_t end = clock();
cout << "Test my LinkedMap: \n";
//調用了time.h文件的CLOCKS_PER_SEC,表示每過千分之一秒,clock()函數返回值加1
cout << "插入1百萬 條數據耗時: "
<< double(end - start) / CLOCKS_PER_SEC << " 秒" << endl;
//測試map效率
start = clock();
map<int,string> stlmap;
for(int j = 0; j < 1000000; j++)
{
//插入節點
stlmap.insert(pair<int,string>(j,"hello map!"));
}
end = clock();
cout << "Test STL Map: \n";
//調用了time.h文件的CLOCKS_PER_SEC,表示每過千分之一秒,clock()函數返回值加1
cout << "插入1百萬 條數據耗時: "
<< double(end - start) / CLOCKS_PER_SEC << " 秒" << endl;
return 0;
}
測試結果: