
Is available in BOOST some container (set or map) that can keep the insertion order of elements? -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

On Tue, Nov 22, 2011 at 5:46 PM, Claude
Is available in BOOST some container (set or map) that can keep the insertion order of elements?
multi-index http://www.boost.org/doc/libs/1_48_0/libs/multi_index/doc/index.html John

I used this definition:
typedef multi_index_container<
int,
indexed_by
mySet;
But this not preserve the insertion order. I think that my error is in "index_by<>"; it is true? How do I fix? -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

2011/11/23 Claude
I used this definition:
typedef multi_index_container< int, indexed_by
> mySet;
But this not preserve the insertion order. I think that my error is in "index_by<>"; it is true? How do I fix?
First of all: do you need key-value container (map/hash map)
or just sequential one (list/vector)?
If you need it to be both at the same time, multi-index is the way to
go then and you
need need 2 indices, ordered and sequenced, for example:
typedef multi_index_container<
std::string,
indexed_by<
sequenced<>,
ordered_unique
text_container;
Cheers, Simon -- Szymon Gatner The Lordz Games Studio www.thelordzgamesstudio.com

I need a set and also a map. But not at the same time. -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

On 23 November 2011 15:17, Claude
I need a set and also a map.
std::unordered_map Best regards, -- Mateusz Loskot, http://mateusz.loskot.net

C++11 std::unordered_map store elements with no insertion order but based on a hash function... -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

On 23 November 2011 15:47, Claude
C++11 std::unordered_map store elements with no insertion order but based on a hash function...
Yes, but you can easily combine it with sequential container that tracks order, but maps gives mapping, without sorting 'overhead'. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net

I had tried, but did not succeed. I created a class with two members, a std:: map (or std::unordered_map) and a std::list to keep insertion order. But I could not create an iterator that give back the elements of the list. :( For this reason I ask for an alternative method... -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

It is very difficult to realize? -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

On Wed, Nov 23, 2011 at 10:11 AM, Claude
It is very difficult to realize?
No. The examples are pretty straightforward. Even I understood them, and that's saying quite a lot. I used the tutorial as well as the examples to get myself going. http://www.boost.org/doc/libs/1_47_0/libs/multi_index/doc/examples.html -- Chris Cleeland

It is very difficult to realize?
#include
container;
int main() { container c; c.insert(c.end(), item(2)); c.insert(c.end(), item(1)); c.insert(c.end(), item(3)); c.insert(c.end(), item(4)); for (container::const_iterator i = c.begin(); i != c.end(); ++i) std::cout << i->i_ << std::endl; }

Igor: but if I would simulate a std::map I should add another field in structure, to store the "value" of the map .. is correct? -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

but if I would simulate a std::map I should add another field in structure, to store the "value" of the map .. is correct?
Yes, you can add a "key" field to the struct, and then define ordered unique index on it. Note that when accessing multi_index_container you say explicitly which index you're working with. So, when you're working with random_access<> index, you'll get the items in the order of insertion, but when you switch to another (eg. ordered) index, you get the items in another order.

Ok, it is good! Last question:it is possible to insert an element with equal syntax of std::map (eg. myMap[myKey] = myValue;)? -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

Ok, it is good! Last question:it is possible to insert an element with equal syntax of std::map (eg. myMap[myKey] = myValue;)?
No, it's impossible. Please read the following: http://www.boost.org/doc/libs/1_48_0/libs/multi_index/doc/tutorial/technique...

Ok, thank you very much! -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

I was trying to encapsulate a boost multiindex in a class usable as std::
map.
This is code:
template

template
class Ordered_Map { private: struct item { item(K k,V v) : first(k),second(v) {} K first; V second; }; typedef multi_index_container< item, indexed_by< random_access<>, ordered_unique
> > > container; container c;
public:
V operator[](const K& key) {
c.insert(c.end(), item(key,2));
return (??); } void begin() {
//for (container::const_iterator i = c.begin(); i != c.end(); ++i) //std::cout << i->first << " - " << i->second << std::endl; } };
I have some problems; The operator[] is not yet complete.
Have you red the link I posted: http://www.boost.org/doc/libs/1_48_0/libs/multi_index/doc/tutorial/technique...
But the real problem is the begin() method, it should return a container::const_iterator, but if I use the signature: *container::const_iterator begin()* { return c.begin(); }
I get an error :-(
Maybe you try to return the iterator of the second index? Note that container::const_iterator is the iterator of the 1st index.

How can I return second index iterator? Why if I use container::const_iterator inside the begin() method it work as variable type? -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

Ok, this is my code:
#include

<...>
public: V& operator[]( const K& key ) { r = new mutable_pair(key,V()); c.insert(c.end(),*r); return (*r).second; } <..> I now correctly get two iterators, but in this class don't work the assignment: my output is: Joe - 0 Lillo - 0 Mary - 0
Of course, because insert() copies its parameter, so you actually return the ref to the original object while the container stores its copy (thus you also have a memory leak). The code should be as follows: V& operator[]( const K& key ) { c.insert(c.end(), mutable_pair(key,V())); return c.get<1>().find(key)->second; }

Now it work well!! Thanks! :) -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

Ok, now I want to make find() and erase() methods. In std::map, they return an iterator. Should I wrap the methods find() and erase(), to pass a const K& key, and return a container1:: const_iterator. is it right? -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

Ok, now I want to make find() and erase() methods. In std::map, they return an iterator. Should I wrap the methods find() and erase(), to pass a const K& key, and return a container1:: const_iterator. is it right?
std::map::find and std::map::erase have several overloads:
http://www.cplusplus.com/reference/stl/map/find/
http://www.cplusplus.com/reference/stl/map/erase/
And none of erase() overloads returns iterator.
By the way, why don't you just inherit from multi_index_container and
add [] operator?
MIC itself already has STL-complient interace, so trying to rewrite it
is hardly worth the effort.
The following is your code, but with inheritance:
#include
{
public:
V& operator[]( const K& key )
{
insert(end(), mutable_pair

Igor R
Ok, now I want to make find() and erase() methods. In std::map, they return an iterator. Should I wrap the methods find() and erase(), to pass a const K& key, and return a container1:: const_iterator. is it right?
std::map::find and std::map::erase have several overloads: http://www.cplusplus.com/reference/stl/map/find/ http://www.cplusplus.com/reference/stl/map/erase/ And none of erase() overloads returns iterator.
erase *does* return an iterator. The docs you're using are non-compliant, take a look for instance at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf HTH, Joaquín M López Muñoz Telefónica Digital

erase *does* return an iterator. The docs you're using are non-compliant, take a look for instance at
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
In c++11, but not in c++03 http://www.tech-archive.net/Archive/VC/microsoft.public.vc.stl/2005-10/msg00...

________________________________________ De: boost-users-bounces@lists.boost.org [boost-users-bounces@lists.boost.org] En nombre de Igor R [boost.lists@gmail.com] Enviado el: sábado, 03 de diciembre de 2011 20:27 Para: boost-users@lists.boost.org Asunto: Re: [Boost-users] Container with insertion order
erase *does* return an iterator. The docs you're using are non-compliant, take a look for instance at
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
In c++11, but not in c++03 http://www.tech-archive.net/Archive/VC/microsoft.public.vc.stl/2005-10/msg00...
Good point, I didn't recall this was a post-C++03 fix. Either way, Boost.MultiIndex ordered indices return the iterator the OP is after. Joaquín M López Muñoz Telefónica Digital Este mensaje se dirige exclusivamente a su destinatario. Puede consultar nuestra política de envío y recepción de correo electrónico en el enlace situado más abajo. This message is intended exclusively for its addressee. We only send and receive email on the basis of the terms set out at. http://www.tid.es/ES/PAGINAS/disclaimer.aspx

Igor R. wrote
By the way, why don't you just inherit from multi_index_container and add [] operator? MIC itself already has STL-complient interace, so trying to rewrite it is hardly worth the effort. The following is your code, but with inheritance:
I have already written a lot of code that uses std::map. I would like to replace std::map with my class Ordered_Map without changing my code. This is possible his example? -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

By the way, why don't you just inherit from multi_index_container and add [] operator? MIC itself already has STL-complient interace, so trying to rewrite it is hardly worth the effort. The following is your code, but with inheritance:
I have already written a lot of code that uses std::map. I would like to replace std::map with my class Ordered_Map without changing my code. This is possible his example?
Yes, it's possible. What I mean is that in your case it's much simpler to extend MIC's interface, than to write an adapter from scratch.

Ok, I added at your code a wrap for find() and erase() methods and it work well. But the class don't compile on GCC 4.6.1 (Linux Ubuntu 11.10) instead work on MSVC 10... -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

Another (last ?) question: how is possible simulate operator== for boost multi_index? -- View this message in context: http://boost.2283326.n4.nabble.com/Container-with-insertion-order-tp4097717p... Sent from the Boost - Users mailing list archive at Nabble.com.

Another (last ?) question: how is possible simulate operator== for boost multi_index?
No need to simulate, comparison operators are already defined for MIC.
You should only provide the relevant ops for your elements. In case of
mutable_pair it would look like this:
template
participants (8)
-
Chris Cleeland
-
Claude
-
Igor R
-
Joaquin M Lopez Munoz
-
JOAQUIN M. LOPEZ MUÑOZ
-
John Drescher
-
Mateusz Łoskot
-
Szymon Gatner