Algorithmic Solutions > LEDA > LEDA Guide > Simple,Basic, and Advanced Data Types > Advanced Data Types > Dictionaries


Dictionaries

The data type dictionary can be used to store objects of an arbitrary type I together with an associated key of a linearly ordered type K (int, string, …). The elements of a dictionary are of type dic_item.

Example

The following program reads strings from standard input (cin) and stores for each string the number of occurrences in a dictionary D. Finally it outputs all strings in D and their multiplicity.
#include <LEDA/core/dictionary.h>
#include <LEDA/core/string.h>

using namespace leda;

int main()
{
  dictionary<string,int> D; 
           //objects of type int, keys of type string
  string s;
  
  dic_item it;
  do  {
    std::cout << "Insert String ('stop' terminates loop): ";
    std::cin >> s;
    it=D.lookup(s);
    if (it==nil) D.insert(s,1);
    else D.change_inf(it,D.inf(it)+1);
  } while (s!="stop");
  
  forall_items(it,D) {
    std::cout << D.key(it) << " " << D.inf(it) << std::endl;
  }
  
  return 0;
}

Strengths

Disadvantages

Tips

  • To store objects with an associated key of a linearly ordered type, carefully choose between: Dictionary, Dictionary Array, and Sorted Sequence . Dictionary Arrays are fastest, but have a very restricted interface. Sorted Sequences offer lots of functionality, but are slowest. Dictionaries are in between in both categories.
  • If your keys are of type pointer, item, or int use Maps.

See also:

Other data types with key of linearly ordered type:

Dictionary Arrays

Sorted Sequences

Data type with key of type pointer, item, or int: Maps

Data type with key of hashed type: Hashing Arrays

Two-Dimensional Dictionaries


Manual Entries:

Manual Page Dictionaries

User Defined Parameter Types

LEDA Item Concept

Linear Orders

Hashed Types




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH