Boost logo

Boost Users :

Subject: Re: [Boost-users] Container with insertion order
From: Claude (clros_at_[hidden])
Date: 2011-11-30 06:54:47


Ok, this is my code:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/random_access_index.hpp>

using namespace boost::multi_index;

template<typename K,typename V> //K = Key, V = Value
class Ordered_Map
{
private:
        struct mutable_pair
        {
                typedef K first_type;
                typedef V second_type;

                mutable_pair():first(K()),second(V()){}
                mutable_pair(const K& f,const V&s):first(f),second(s){}
                mutable_pair(const std::pair<K,V>& p):first(p.first),second(p.second){}

                K first;
                mutable V second;
        };

         typedef multi_index_container<
        mutable_pair,
        indexed_by< random_access<>,
                    ordered_unique<member&lt;mutable_pair, K,
&amp;mutable_pair::first> >
>
> container1;

        container1 c;
        mutable_pair *r;

public:
        V& operator[]( const K& key )
          {
            r = new mutable_pair(key,V());
            c.insert(c.end(),*r);
            return (*r).second;
          }

   typename container1::const_iterator cbegin() const
   {
    return c.cbegin();
   }

   typename container1::const_iterator cend() const
   {
    return c.cend();
   }
};

int main()
{

                Ordered_Map<std::string,int> myMap;
                myMap["Joe"] = 3;
                myMap["Lillo"] = 9;
                myMap["Mary"] = -3;
                for (auto i = myMap.cbegin(); i != myMap.cend(); ++i)
                std::cout << i->first << " - " << i->second << std::endl;

                std::cin.get();
                return 0;
}

I now correctly get two iterators, but in this class don't work the
assignment: my output is:

Joe - 0
Lillo - 0
Mary - 0

--
View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p4122696.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net