std::map的小小失误
# 故事简介
完全属于我的脑子失误,短暂的弱智时刻,导致该失误的诞生,’罪过‘。
由于项目开发,需增加一逻辑:过滤同样的某接口调用的请求,保持内部响应逻辑处理系统不允许存在一个以上同样的请求。
请求内容包括ID和参数,两者可以区分出不同调用请求。
Request = { RequestID,RequestParam }
# 失误情况
使用 std::multimap 结构做记录。添加、删除、测试(验证)记录条目。
详细定义,如下:
namespace query_lib
{
typedef std::multimap<int, std::pair<std::string, size_t> > request_record_type;
static request_record_type _request_record;
/// 记录或标记函数功能请求 标记类型 pair<func_id, pair<func_param, record> >
bool add_request_record(const request_record_type::value_type& obj)
{
/// Lock lock(...);// todo lock
request_record_type::iterator lower_it = _request_record.upper_bound(obj.first);
for (; lower_it != _request_record.end(); ++lower_it )
{
if (lower_it->first != obj.first)
break;
else if ( lower_it->second.first == obj.second.first)
{
++lower_it->second.second;
return false;
}
}
_request_record.insert(lower_it, obj);
return true;
}
/// 擦除函数功能请求的记录或标记 标记类型 pair<func_id, pair<func_param, record> >
void del_request_record(const request_record_type::value_type& obj)
{
/// Lock lock(...);// todo lock
request_record_type::iterator upper_it = _request_record.upper_bound(obj.first);
request_record_type::iterator lower_it = _request_record.lower_bound(obj.first);
for (request_record_type::iterator iter = upper_it; iter != lower_it; ++iter)
{
if (iter->first == obj.first && iter->second.first == obj.second.first)
{
if (0 != iter->second.second)
{
--(iter->second.second);
}
else
assert(false);
}
}
}
bool test_request_record(const request_record_type::value_type& obj)
{
/// Lock lock(...);// todo lock
request_record_type::iterator upper_it = _request_record.upper_bound(obj.first);
request_record_type::iterator lower_it = upper_it;
for (; lower_it != _request_record.end();
++lower_it )
{
if (lower_it->first != obj.first)
break;
else if ( lower_it->second.first == obj.second.first &&
obj.second.second != 0)
return true;
}
return false;
}
# 问题解析
诶,简单一句话。颠倒了map::lower_bound & map::upper_bound两成员函数的含义。
罪过啊,浪费了设计,测试,调错,修改过程的数小时(3H+)时间啊,而且那时加班时间!
转,Look following ……
---
public member function
map::lower_bound
iterator lower_bound ( const key_type& x );
const_iterator lower_bound ( const key_type& x ) const;
Return iterator to lower bound
Returns an iterator pointing to the first element in the container whose key does not compare less than x (using the container's comparison object), i.e. it is either equal or greater.Unlike upper_bound, this member function returns an iterator to the element also if it compares equal to x and not only if it compares greater.
Notice that, internally, all the elements in a map container are always ordered by their keys following the criterion defined by its comparison object, therefore all the elements that follow the one returned by this function will have a key that compares greater than x.
---
举个明显的例子说明吧,如下:
map = { (1,a), (2, b), (2, c), (2, d), (3, e), (4, f) }
map.lower_bound(2) => (2, b)
map.lupper_bound(2) => (3, e)
# 忠告建议
当你在想好了设计要开始编写算法逻辑的代码时,再次提醒“清晰确认使用的基本元素”!
# 链接
修改后,正确的程序参见链接页面。
没有评论:
发表评论