C++从 std::multimap 中查找多个键
我有一个 STL::multimap
,我搜索它以使用重复键的值填充 std::list
。
我可以在 std::list
中查找/插入 count > 的所有键的元素值吗? 1
不用一一数?
std::multimap<int, std::string> mm ;
mm[0] = "a" ;
mm[1] = "b" ;
mm[0] = "c" ;
mm[2] = "j" ;
mm[2] = "k" ;
std::list<std::string> lst ;
lst
可能包含 "a" ,"c","j","k"
;
我尝试这个
template <class K, class V>
class extract_value {
private:
K last_key_ ;
std::list<V> m_list_value ;
std::pair<K, V> first_elem ;
public:
extract_value(const K& k_): last_key_(k_) { }
void operator() (std::pair<const K, V> elem)
{
if (last_key_ == elem.first)
{
m_list_value.push_back(elem.second) ;
}
else
{
// First entry
last_key_ = elem.first;
first_elem= elem ;
}
}
std::list<V> get_value() { return m_list_value ; }
};
ex_ = for_each(mm.begin(),mm.end(), extract_value<int, std::string>(0)) ;
std::list<std::string> lst = ex_.get_value() ;
我不确定这段代码是否可以编译。
I have an STL::multimap
and which I search to populate a std::list
with values where keys are duplicated.
Can I find/insert to a std::list
the value of elements for all key where count > 1
without counting them one by one?
std::multimap<int, std::string> mm ;
mm[0] = "a" ;
mm[1] = "b" ;
mm[0] = "c" ;
mm[2] = "j" ;
mm[2] = "k" ;
std::list<std::string> lst ;
lst
might contain "a" ,"c","j","k"
;
I try this
template <class K, class V>
class extract_value {
private:
K last_key_ ;
std::list<V> m_list_value ;
std::pair<K, V> first_elem ;
public:
extract_value(const K& k_): last_key_(k_) { }
void operator() (std::pair<const K, V> elem)
{
if (last_key_ == elem.first)
{
m_list_value.push_back(elem.second) ;
}
else
{
// First entry
last_key_ = elem.first;
first_elem= elem ;
}
}
std::list<V> get_value() { return m_list_value ; }
};
ex_ = for_each(mm.begin(),mm.end(), extract_value<int, std::string>(0)) ;
std::list<std::string> lst = ex_.get_value() ;
I'm not sure if this code compiles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用 equal_range 方法,该方法返回一对限制请求值的迭代器,然后在返回的迭代器之间循环。 (为了简洁起见,请注意使用 typedef)。
lst 现在应该包含 { "j", "k" }
You use the equal_range method which returns a pair of iterators bounding the requested value then loop between the returned iterators. (Note the use of the typedef for brevity).
lst should now contain { "j", "k" }
http://www.cplusplus.com/reference/stl/multimap/
是。使用 count() 函数。请参阅发布的参考资料。但为什么要担心这个呢?如果存在重复项,则无论如何您都必须迭代它们才能填充列表。
编辑:另外,“them”的先行词尚不清楚。我认为它是指与特定键关联的值。这与多重映射中的值总数相反。
edit2:从您回答问题的声音来看,您希望多重映射告诉您哪些值具有重复的键。没有办法做到这一点;如果不循环键,multimap 就无法提供您想要的功能。
如果您想要此功能,您应该考虑在将值插入多重映射时插入填充重复项列表...当然,仅在发现存在重复项时才插入它们。
http://www.cplusplus.com/reference/stl/multimap/
Yes. Use the count() function. Please see the posted reference. But why worry about that? If there are duplicates, you'll have to iterate over them anyway to populate the list.
edit: also, the antecedant of "them" is unclear. I take it to mean the values associated with a specific key. This is opposed to the total number of values in the multimap.
edit2: from the sounds of how you are replying to questions, you want the multimap to tell you what values have duplicate keys. There is no way to do this; multimap doesn't provide the functionality you want without looping over your keys.
If you want this functionality, you should consider inserting populating your list of duplicates as you insert the values into the multimap... of course, only inserting them as you discover when there are duplicates.
来源:
输出:
Source:
Output: