34485

seeking bidirectional map with non-unique keys or values

Question:

I need a bidirectional container with non-unique keys or values. multimap doesn't work, because it's unidirectional and boost>::bimap only allows unique keys and values. Any such container in the standard or boost libs? Otherwise: any pointers/articles to implement such a structure would be appreciated. Only up to 100 or so element-pairs are expected. Any help appreciated, thanks, Jeanette

Answer1:

Actually boost::bimap does allow non-unique keys:

<a href="http://www.boost.org/doc/libs/release/libs/bimap/doc/html/boost_bimap/the_tutorial/differences_with_standard_maps.html" rel="nofollow">http://www.boost.org/doc/libs/release/libs/bimap/doc/html/boost_bimap/the_tutorial/differences_with_standard_maps.html</a>

<strong>Example:</strong>

#include <boost/bimap.hpp> #include <boost/bimap/multiset_of.hpp> #include <string> #include <iostream> namespace bm = boost::bimaps; int main() { using mybimap = bm::bimap< bm::multiset_of<std::string>, int >; mybimap map; map.left.insert( mybimap::left_value_type( "orange", 1 ) ); map.left.insert( mybimap::left_value_type( "apple", 42 ) ); map.left.insert( mybimap::left_value_type( "orange", 7 ) ); auto rng = map.left.equal_range( "orange" ); for( auto it = rng.first; it != rng.second; ++it ) std::cout << it->first << ": " << it->second << "\n"; }

<strong>Live Demo:</strong>

<a href="http://coliru.stacked-crooked.com/a/2efdc80cde5f2933" rel="nofollow">http://coliru.stacked-crooked.com/a/2efdc80cde5f2933</a>

Answer2:

You could use a vector of pairs. If you only have 100 elements, linear search is perfectly adequate for lookup on both pair elements. If you have larger collections, or if you frequently need equal-ranges on either pair element, you can maintain separate index structures.

(This is basically how boost.bimap is implemented anyway.)

Recommend

  • back button function for phonegap windows phone 7
  • Feature detection of foreignObject in SVG
  • XSD with multi occurrences unordered
  • Varnish/Apache Random 503 Errors
  • node.js POST request fails
  • matlab crashes without dump file when using fopen for file
  • C# Excel interop - how to test if interop object is still working and performing a task?
  • c++ regex_replace not doing intended substitution
  • HttpURLConnection.getOutputStream() takes 20 seconds. Why?
  • Why does java tzupdater add leap seconds?
  • How to convert SOAP response with xsi values to json in WSO2esb
  • jwtBearer bearer token with rc-1 update to ASP.Net 5
  • Apache RewriteRule redirection with url encoded
  • Jetty 9 HashLoginService
  • How to attach a node.js readable stream to a Sendgrid email?
  • Functions in global context
  • Python urlparse: small issue
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Read a local file using javascript
  • Syntax for setting draggablecursor property in google maps api
  • Seeking advice on Jetty HttpClient Hang
  • does jqgrid support a multiple checkbox list for editing
  • Cannot connect to cassandra from Spark
  • Optimizing database types to compact database (SQLite)
  • Is possible to count alias result on mysql
  • Cross-Platform Protobuf Serialization
  • Check if a string to interpolate provides expected placeholders
  • JSON with duplicate key names losing information when parsed
  • Trying to switch camera back to front but getting exception
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • How to Embed XSL into XML