64,681
社区成员
发帖
与我相关
我的任务
分享
struct Point
{
int x;
int y;
Point(int _x=0,int _y=0):x(_x),y(_y){}
bool operator == (const Point & value) const{
return x==value.x&&y==value.y;
}
bool operator < (const Point & value) const{
return x<value.x||y<value.y;
}
};
map<Point, bool> pointMap;
// 已经插入一些数据
// for(auto &v : pointMap)
for(auto itr=pointMap.begin();itr!=pointMap.end();itr++){
count++;
// Point point(v.first.x,v.first.y);
}
cout<<"count: "<<count<<" : "<<testMap.size()<<endl;
#ifndef PAIR_H
#define PAIR_H
#include <tuple>
#include <list>
////////////////////////////////////////////////////////////////////////////////////
// 类声明
template<typename _KeyType, typename _ValueType>
class CPair
{
public:
CPair();
~CPair();
public:
void Push(_KeyType _Key, _ValueType _Value);
bool Remove(_KeyType _Key);
_ValueType GetValue(_KeyType _Key)const;
void UpdateValue(_KeyType _Key, _ValueType _Value);
void Clear();
size_t Size()const;
bool Empty()const;
private:
std::list<std::tuple<_KeyType, _ValueType>*> *m_pList;
};
////////////////////////////////////////////////////////////////////////////////////
// 构造函数
template<typename _KeyType, typename _ValueType>
CPair<_KeyType, _ValueType>::CPair()
:m_pList(new std::list<std::tuple<_KeyType, _ValueType>*>)
{
}
////////////////////////////////////////////////////////////////////////////////////
// 析构函数
template<typename _KeyType, typename _ValueType>
CPair<_KeyType, _ValueType>::~CPair()
{
if (m_pList)
{
while (!m_pList->empty())
{
std::tuple<_KeyType, _ValueType>* tup = m_pList->front();
m_pList->pop_front();
delete tup;
}
delete m_pList;
m_pList = NULL;
}
}
////////////////////////////////////////////////////////////////////////////////////
// 添加元素
template<typename _KeyType, typename _ValueType>
inline void CPair<_KeyType, _ValueType>::Push(_KeyType _Key, _ValueType _Value)
{
for (std::tuple<_KeyType, _ValueType>* tup : *m_pList)
{
if (std::get<0>(*tup) == _Key) { throw "Key已存在"; return; }
}
std::tuple<_KeyType, _ValueType>* tup = new std::tuple<_KeyType, _ValueType>(_Key, _Value);
m_pList->push_back(tup);
}
////////////////////////////////////////////////////////////////////////////////////
// 获取与_Key对应的Value
template<typename _KeyType, typename _ValueType>
inline _ValueType CPair<_KeyType, _ValueType>::GetValue(_KeyType _Key) const
{
for (std::tuple<_KeyType, _ValueType>* tup : *m_pList)
{
if (std::get<0>(*tup) == _Key)
return std::get<1>(*tup);
}
return _ValueType();
}
////////////////////////////////////////////////////////////////////////////////////
// 修改与_Key对应的Value
template<typename _KeyType, typename _ValueType>
inline void CPair<_KeyType, _ValueType>::UpdateValue(_KeyType _Key, _ValueType _Value)
{
for (std::tuple<_KeyType, _ValueType>* tup : *m_pList)
{
if (std::get<0>(*tup) == _Key)
{
std::get<1>(*tup) = _Value;
}
}
}
////////////////////////////////////////////////////////////////////////////////////
// 删除与_Key对应的Value
template<typename _KeyType, typename _ValueType>
inline bool CPair<_KeyType, _ValueType>::Remove(_KeyType _Key)
{
for (std::tuple<_KeyType, _ValueType>* tup : *m_pList)
{
if (std::get<0>(*tup) == _Key)
{
m_pList->remove(tup);
delete tup;
return true;
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////
// 清空整个容器
template<typename _KeyType, typename _ValueType>
inline void CPair<_KeyType, _ValueType>::Clear()
{
while (!m_pList->empty())
{
std::tuple<_KeyType, _ValueType>* tup = m_pList->front();
m_pList->pop_front();
delete tup;
}
}
////////////////////////////////////////////////////////////////////////////////////
// 返回容器中有多少条数据
template<typename _KeyType, typename _ValueType>
inline size_t CPair<_KeyType, _ValueType>::Size() const
{
return m_pList->size();
}
////////////////////////////////////////////////////////////////////////////////////
// 如果容器为空,返回true;否则返回false
template<typename _KeyType, typename _ValueType>
inline bool CPair<_KeyType, _ValueType>::Empty() const
{
return m_pList->empty();
}
#endif // PAIR_H
#include <map>
#include <iostream>
using namespace std;
struct Point
{
int x;
int y;
Point(int _x=0,int _y=0):x(_x),y(_y){}
bool operator == (const Point & value) const{
return x==value.x&&y==value.y;
}
bool operator < (const Point & value) const{
return x<value.x||y<value.y;
}
};
int main()
{
int count = 0;
map<Point, bool> pointMap;
Point p;
p.x = 1;
p.y = 1;
Point p1;
p1.x =2;
p1.y =2;
pointMap.insert( std::pair<Point,bool>(p,true));
pointMap.insert( std::pair<Point,bool>(p1,false));
for(auto itr=pointMap.begin();itr!=pointMap.end();itr++){
count++;
}
cout<<"count: "<<count<<" : "<<pointMap.size()<<endl;
}