Unordered Associative Containers

Last time I introduced what will likely be adopted into the next version of the C++ Standard, a new type of container called unordered associative containers. By parsing the name of this new container type, you can determine a lot of useful information. First of all, these containers, unlike sets and maps, are unordered. Traversing these containers results in an unordered list of elements. If you want an ordered list, you need to use set/map or vector+sort or some other solution.

Secondly, these containers are associative. That means elements can be compared in a meaningful way. However, unlike set/map, which require the programmer to supply a "less-than" form of element relationship, unordered associative containers require a "equivalent" form of element relationship (operator ==() ).

Finally, these are true STL containers. They support iteration and the other basic concepts of what it means to be an STL container. For example, they support functions like empty(), begin() and end().

Declaring an unordered associative container is easy. Here’s an example:

std::str1::unordered_set< int > us;

You add items to this container in the same way that you might add items into a standard set:

us.insert( 42 );

us.insert( 1005 );

us.insert( 3 );

And you can find an item just like you’d expect, too:

 if( us.find( 42 ) != us.end() )

   // element found

What’s really interesting about unordered associative containers is what’s going on under the covers. Tune in next time…


This entry was posted in C++. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s