Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按键然后按值在“hash_multimap”上迭代_C++_Hashtable - Fatal编程技术网

C++ 按键然后按值在“hash_multimap”上迭代

C++ 按键然后按值在“hash_multimap”上迭代,c++,hashtable,C++,Hashtable,我需要对一个键的所有值执行一些代码,并且需要对每个键重复该操作。我寻找的东西如下: for(auto key_iterator = hash_multimap.begin_keys(); key_iterator != hash_multimap.end_keys(); key_iterator++) { auto key = key_iterator->key; // set up state for(auto value_iterator = key_i

我需要对一个键的所有值执行一些代码,并且需要对每个键重复该操作。我寻找的东西如下:

for(auto key_iterator = hash_multimap.begin_keys();
    key_iterator != hash_multimap.end_keys(); key_iterator++)
{
    auto key = key_iterator->key;
    // set up state
    for(auto value_iterator = key_iterator->begin_values();
        value_iterator != key_iterator->end_values(); value_iterator++)
    {
        // mutate state
    }
    // use state
    // tear down state
}
这当然不起作用,但有没有办法达到类似的效果?问题是我需要检查每个键,然后对所有键使用共享状态。它的用途示例如下:

typedef std::hash_multimap<int> hash_t;
typedef hash_t::value_type hash_val;

hash_t hash;
hash.insert(hash_val(0, 1));
hash.insert(hash_val(1, 2));
hash.insert(hash_val(1, 3));
hash.insert(hash_val(2, 4));
hash.insert(hash_val(2, 5));
hash.insert(hash_val(2, 6));
hash.insert(hash_val(3, 7));
hash.insert(hash_val(3, 8));
hash.insert(hash_val(3, 9));

// print out the sum of values for each key here.
// expected output:
//
// 0: 1
// 1: 5
// 2: 15
// 3: 24
typedef std::hash_multimap hash_t;
typedef hash_t::value_type hash_val;
散列;
插入(hash_val(0,1));
插入(hash_val(1,2));
插入(hash_val(1,3));
插入(hash_val(2,4));
插入(hash_val(2,5));
插入(hash_val(2,6));
插入(hash_val(3,7));
插入(hash_val(3,8));
插入(hash_val(3,9));
//在此处打印每个键的值之和。
//预期产出:
//
// 0: 1
// 1: 5
// 2: 15
// 3: 24
仅使用
hash\u multimap.begin()
的问题在于,我不能确定它返回该键的连续块中的每个键,即使返回了,我也不知道这些块从何处开始,在何处结束

编辑:我也不能使用
hash\u multimap.equal\u range(key)
,因为我不能迭代键。一种只包含每个键一次的迭代键的方法也可以解决这个问题


我如何才能做到这一点?

您可以使用下限和上限来实现这一点。例如尝试-

 auto value_iterator = hash->begin();
 while( value_iterator != hash->end() ){
 {
     auto lIter = hash->lower_bound( value_iterator->first );
     auto uIter = hash->upper_bound( value_iterator->first );
     while( lIter != uIter ){
         // sum the values associated with keys
         // Increment lIter
     }
     value_iterator = uIter;
 }
编辑:如果您使用的库没有上限、下限成员函数,则可以按照@Thanatos的建议使用
equal\u range
。实现实际上有这些。逻辑是相同的,内部循环是-

pair<hash_multimap<int,int>::iterator,hash_multimap<int,int>::iterator> pairIter;
pairIter.equal_range(value_Iterator->first);
while( pairIter.first != pairIter.second ){
    // sum the values associated with keys
    // Increment pairIter->first
}
value_iterator = pairIter.second;
pairIter对;
pairIter.equal_range(值迭代器->第一个);
while(pairIter.first!=pairIter.second){
//对与键关联的值求和
//增量pairIter->first
}
值\迭代器=pairIter.second;

既然您使用了C++11语法,我也会;-]

#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <iostream>

int main()
{
    typedef std::unordered_multimap<int, int> map_t;
    typedef std::unordered_map<int, int> sums_t;

    map_t hash;
    hash.insert(map_t::value_type(0, 1));
    hash.insert(map_t::value_type(1, 2));
    hash.insert(map_t::value_type(1, 3));
    hash.insert(map_t::value_type(2, 4));
    hash.insert(map_t::value_type(2, 5));
    hash.insert(map_t::value_type(2, 6));
    hash.insert(map_t::value_type(3, 7));
    hash.insert(map_t::value_type(3, 8));
    hash.insert(map_t::value_type(3, 9));

    sums_t const& sums = std::accumulate(hash.cbegin(), hash.cend(), sums_t(),
        [](sums_t& acc, map_t::const_reference p)
        {
            return acc[p.first] += p.second, acc;
        }
    );

    std::for_each(sums.cbegin(), sums.cend(),
        [](sums_t::const_reference p)
        {
            std::cout << p.first << ": " << p.second << '\n';
        }
    );
}
#包括
#包括
#包括
#包括
int main()
{
typedef std::无序的多映射映射;
typedef std::无序映射和;
映射哈希;
insert(map_t::value_type(0,1));
insert(map_t::value_type(1,2));
insert(map_t::value_type(1,3));
insert(map_t::value_type(2,4));
insert(map_t::value_type(2,5));
insert(map_t::value_type(2,6));
insert(map_t::value_type(3,7));
insert(map_t::value_type(3,8));
insert(map_t::value_type(3,9));
sums\u t const&sums=std::accumulate(hash.cbegin()、hash.cend()、sums\u t(),
[](总和和附件,映射::常数参考p)
{
返回acc[p.first]+=p.second,acc;
}
);
std::for_each(sums.cbegin(),sums.cend(),
[](总和:常数参考p)
{

std::难道
散列多映射
不包含
下界
上界
就不会
相等范围
是等价的、更容易的吗?@Thanatos:它不会被分配回
值迭代器
then@Mahesh:我正在使用gnu扩展(SGI)实现,@Dani为什么不呢?
value\u iterator=equal\u range\u returned\u iter->second;
创建另一个映射来存储sum有点昂贵,因为
hash\u multimap
已经在bucket内部对它们进行了排序。附带说明:您应该使用
std::endl
而不是
'\n'
@Dani:1)如果您需要保留原始映射的值(这可能在大多数情况下都是如此),那么成本就不会很高。此外,另一种解决方案具有
O(4N•log2(N))
复杂性,而此解决方案具有
O(N)
复杂性。2)不,您不应该;
std::endl
只是
的简写形式