c++ 使用结构体作为键值时,遍历不完全的问题

凉亭_pavilion 2018-11-13 12:54:22
我尝试使用一个map,其键值为结构体Point:
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;

结果输出: count: 23 : 127

无论时使用for(auto &v : pointMap)还是使用迭代器,都是这样的结果。如果把结构体换成int就有可以了。我看了许多博客,他们的似乎也是这样的,但是我的就是少了许多,不知道是哪里出现了故障。
恳请指教。
...全文
107 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
凉亭_pavilion 2018-11-13
  • 打赏
  • 举报
回复
引用 6 楼 baiywcwttfln 的回复:
bool operator < (const Point & value) const{ return x<value.x||y<value.y; } 比较函数有问题 bool operator < (const Point & value) const{ return x<value.x || (x==value.x && y<value.y); }
确实如此。。。这么说来的话,似乎是这个比较的问题导致其插入的时候就出现故障,然后遍历也就不行了。。。
凉亭_pavilion 2018-11-13
  • 打赏
  • 举报
回复
引用 7 楼 ltuse_csdn 的回复:
[quote=引用 5 楼 zxc024000 的回复:] 是不是,你的程序写错了。。
不好意思,那个testmap是我测试用int做键值的时候写上去的。不过换回pointMap也是不行的。(我本来就是pointMap.size()[/quote] 你是怎么编译的? 我是用g++的版本是 gcc version 5.1.0 (tdm64-1)
凉亭_pavilion 2018-11-13
  • 打赏
  • 举报
回复
引用 5 楼 zxc024000 的回复:
是不是,你的程序写错了。。
不好意思,那个testmap是我测试用int做键值的时候写上去的。不过换回pointMap也是不行的。(我本来就是pointMap.size()
ForgetTomorrow 2018-11-13
  • 打赏
  • 举报
回复
count有初始化为0吗?看起来没啥问题,大小应该一样
LandyTan 2018-11-13
  • 打赏
  • 举报
回复
参考一下

#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
A-De 2018-11-13
  • 打赏
  • 举报
回复
bool operator < (const Point & value) const{
return x<value.x||y<value.y;
}

比较函数有问题

bool operator < (const Point & value) const{
return x<value.x || (x==value.x && y<value.y);
}
林多 2018-11-13
  • 打赏
  • 举报
回复
是不是,你的程序写错了。。
pointMap 和 testMap,两个map变量。应该输出pointMap的size吧。

代码,在本地试了一下。没问题的。
#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;
}
凉亭_pavilion 2018-11-13
  • 打赏
  • 举报
回复
引用 1 楼 qq_16961853 的回复:
参考一下

#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
额,链表什么的我手写问题不大,但是链表的话,跟着指针迭代就能遍历完了啊,不关这个结构体的事吧。 我尝试过把结构替换成int作为键值,这样倒是可以遍历完。结构体却是不行。
凉亭_pavilion 2018-11-13
  • 打赏
  • 举报
回复
引用 2 楼 ForgetTomorrow 的回复:
count有初始化为0吗?看起来没啥问题,大小应该一样
初始化了的。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧