Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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
Perl的TIE的C++等价::IXHASH(索引关联数组)?_C++_Hash_Containers_Indexed - Fatal编程技术网

Perl的TIE的C++等价::IXHASH(索引关联数组)?

Perl的TIE的C++等价::IXHASH(索引关联数组)?,c++,hash,containers,indexed,C++,Hash,Containers,Indexed,Perl的IxHash是一个关联数组,它记住元素添加到数组中的顺序,允许您通过键或索引直接访问它们。它还具有返回所有键或值的方法,始终按照它们最初插入的顺序 是否有一个等价的C++类容器? 考虑了DEQE或数组和无序的映射。 如果你需要删除,它会像ixhash一样打开。 迭代他们的密钥是一个肮脏的黑客。如果您想要一个真正的键和值迭代器,请查看boost iterator_facade #include <unordered_map> #include <deque> #i

Perl的IxHash是一个关联数组,它记住元素添加到数组中的顺序,允许您通过键或索引直接访问它们。它还具有返回所有键或值的方法,始终按照它们最初插入的顺序


是否有一个等价的C++类容器?

考虑了DEQE或数组和无序的映射。 如果你需要删除,它会像ixhash一样打开。 迭代他们的密钥是一个肮脏的黑客。如果您想要一个真正的键和值迭代器,请查看boost iterator_facade

#include <unordered_map>
#include <deque>
#include <iterator>
#include <iostream>

using namespace std;

template <typename KeyType, typename ValueType>
class MapWithInsertionOrder {
    public:
        bool exists(const KeyType &kk)
        {
            return cache_.find(kk) != cache_.end();
        }

        bool store(const KeyType &kk, const ValueType &vv)
        {
            if (exists(kk))
            {
                return false;
            }
            keys_.push_back(kk);
            cache_.insert(make_pair(kk, vv));

        }

        typedef unordered_map<KeyType,ValueType> cache_type;
        typedef typename cache_type::value_type value_type;

        typedef deque<KeyType> order_type;
        typedef typename order_type::iterator order_iterator;

        value_type &fetch(const KeyType &kk)
        {
            return *cache_.find(kk);
        }

        value_type &at(size_t idx)
        {
            auto kk = keys_.at(idx);
            return fetch(kk);
        }

        size_t size()
        {
            return keys_.size();
        }

        order_iterator keys_begin()
        {
            return keys_.begin();
        }

        order_iterator keys_end()
        {
            return keys_.end();
        }

    private:

        order_type keys_;
        cache_type cache_;

};

int main()
{
    MapWithInsertionOrder<string, string> mm;

    mm.store( "cat", "tom" );
    mm.store( "mouse", "jerry" );
    mm.store( "bird", "tweety" );
    mm.store( "dog", "spike" );

    cout << mm.at(0).second << endl;
    cout << mm.at(1).second << endl;
    cout << mm.at(2).second << endl;

    cout << mm.exists("cat") << endl;
    cout << mm.exists("mouse") << endl;
    cout << mm.exists("dog") << endl;

    for( auto itr = mm.keys_begin();itr != mm.keys_end();itr++ )
    {
        cout << *itr << endl;
    }
    return 0;
}

我不知道标准库中有这样的容器。然而,如果您愿意使用Boost,您可能会在Boost中找到您想要的东西。