1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
Populating trie_map
===================
This section illustrates how to use :cpp:class:`~mdds::trie_map` to build a
database of city populations and perform prefix searches. In this example,
we will use the 2013 populations of cities in North Carolina, and use the city
names as keys.
Let's define the type first:
.. literalinclude:: ../../example/trie_map.cpp
:language: C++
:start-after: //!code-start: type
:end-before: //!code-end: type
:dedent: 4
The first template argument specifies the key type, while the second template
argument specifies the value type. In this example we are using ``std::string``
and ``int``, respectively.
Once the type is defined, the next step is instantiation:
.. literalinclude:: ../../example/trie_map.cpp
:language: C++
:start-after: //!code-start: inst
:end-before: //!code-end: inst
:dedent: 4
It's pretty simple as you don't need to pass any arguments to the constructor.
Now, let's populate this data structure with some population data:
.. literalinclude:: ../../example/trie_map.cpp
:language: C++
:start-after: //!code-start: populate
:end-before: //!code-end: populate
:dedent: 4
It's pretty straight-forward. Each :cpp:func:`~mdds::trie_map::insert` call
expects a pair of string key and an integer value. You can insert your data
in any order regardless of key's sort order.
Now that the data is in, let's perform prefix search to query all cities whose
name begins with "Cha":
.. literalinclude:: ../../example/trie_map.cpp
:language: C++
:start-after: //!code-start: search-cha
:end-before: //!code-end: search-cha
:dedent: 4
You can perform prefix search via :cpp:func:`~mdds::trie_map::prefix_search`
method, which returns a results object that can be iterated over using a range-based
for loop. Running this code will produce the following output:
.. code-block:: none
Cities that start with 'Cha' and their populations:
Chapel Hill: 59635
Charlotte: 792862
Let's perform another prefix search, this time with a prefix of "W":
.. literalinclude:: ../../example/trie_map.cpp
:language: C++
:start-after: //!code-start: search-w
:end-before: //!code-end: search-w
:dedent: 4
You'll see the following output when running this code:
.. code-block:: none
Cities that start with 'W' and their populations:
Wilmington: 112067
Wilson: 49628
Winston-Salem: 236441
Note that the results are sorted in key's ascending order.
.. note::
Results from the prefix search are sorted in key's ascending order.
|