2

In the following code:

typedef bimap< set_of< std::string >, list_of< int > > bm_type;
bm_type bm;

bm.left["one"] = 1; // "one" -> 1
bm.left["one"] = 2; // replaced: "one" -> 2
bm.right[2] = "two"; // Compile error

How can I get rid of the compile error? Isn't it possible to access the right view with operator[]?

B Faley
  • 17,120
  • 43
  • 133
  • 223
  • What are you expecting this to do? – David Schwartz Dec 10 '12 at 10:50
  • @DavidSchwartz I am trying to use bimap like a normal std::map. All my keys and values are unique. I need to be able to get/set keys by values and vice versa. (and I was not sure about using `list_of`) – B Faley Dec 10 '12 at 10:52
  • But a map has only one place any given element can go. I honestly don't understand what you expect this to do. Can you please describe precisely what `bm.right[2]` should do? For example, if there's no `2` in the list, should it add one? If so, *where*? – David Schwartz Dec 10 '12 at 10:53
  • @DavidSchwartz I expect `bm.right[2] = "two"` to change "one <-> 2" relation to this: "two <-> 2" (or add this relation if it does not already exist) – B Faley Dec 10 '12 at 10:55
  • Right, but add it **where**? At the beginning of the list? The end? The middle? Some random place? This just doesn't make sense if the integers are organized as a list. – David Schwartz Dec 10 '12 at 10:55
  • @DavidSchwartz Does not matter. – B Faley Dec 10 '12 at 10:56
  • Then you shouldn't be using a list! A list's whole purpose is to preserve arbitrary ordering. – David Schwartz Dec 10 '12 at 10:57
  • @DavidSchwartz So, will `bimap< set_of< std::string >, set_of< int > >` help? – B Faley Dec 10 '12 at 10:58
  • Yes, since there is only one way to add an integer to a set of integers. – David Schwartz Dec 10 '12 at 10:58
  • @DavidSchwartz But when I change `list_of` to `set_of`, I get compiler error for `bm.left["one"] = 1`. – B Faley Dec 10 '12 at 11:00
  • 1
    Right, because entries in a set are immutable. You need to pick the collection that has precisely the semantics you really want. – David Schwartz Dec 10 '12 at 11:01
  • 1
    @DavidSchwartz Could you please advise on the collections that meet my requirement? – B Faley Dec 10 '12 at 11:02
  • @DavidSchwartz I have the same problem. What collection provides a hashmap like behavior? – danijar Sep 16 '14 at 15:29

2 Answers2

2

list_of has no operator[].

Look at http://www.boost.org/doc/libs/1_47_0/libs/bimap/doc/html/boost_bimap/reference/list_of_reference.html

ForEveR
  • 55,233
  • 2
  • 119
  • 133
1

This code just doesn't make any sense. Lists don't have operator[] because you have to choose where to insert an element if it's created. Since lists are not internally sorted (like maps are), a 2 can go at the beginning of the list, the end of the list, the middle of the list, or anywhere else.

(Boost's list_of mimics the semantics of the standard list.)

David Schwartz
  • 179,497
  • 17
  • 214
  • 278