Newsgroup: it.comp.lang.c++
Titolo: Mappe in c++

Salve a tutti.. sono uno studente di ingegneria informatica che dovrebbe
produrre
per l'esame di fondamenti due un'avventura testuale in c++
So che dovrei utilizzare le mappe all'interno del mio programma ma il mio
problema e che non riesco a reperire materiale su questo tipo di struttura
dati.
Se sapete qualcosa a riguardo ve ne sarei grato.
Mi interesserebbe in alternativa poter consultare qualche sorgente che
faccia uso delle mappe per poter dare una sbirciatina al codice.
Grazie.

http://www.sgi.com/tech/stl/Map.html


 a écrit dans le message news:
iPix8.2090$W9.3...@twister2.libero.it...

Ti copio e incollo un pezzo preso dal libro "Thinking in C++" 2 volume che
puoi scaricare gratis dal web su 
www.bruceeckel.com
.
Nel secondo volume questo argomento e' trattato con altri esempi e puoi
anche scaricare del codice oltre al libro.
Eccoti il pezzo:
Associative containers
The set, map, multiset and multimap are called associative containers
because they associate keys with values. Well, at least maps and multimaps
associate keys to values, but you can look at a set as a map that has no
values, only keys (and they can in fact be implemented this way), and the
same for the relationship between multiset and multimap. So, because of the
structural similarity sets and multisets are lumped in with associative
containers.
The most important basic operations with associative containers are putting
things in, and in the case of a set, seeing if something is in the set. In
the case of a map, you want to first see if a key is in the map, and if it
exists you want the associated value for that key to be returned. Of course,
there are many variations on this theme but that's the fundamental concept.
The following example shows these basics:
//: C04:AssociativeBasics.cpp
// Basic operations with sets and maps
#include "Noisy.h"
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main() {
  Noisy na[] = { Noisy(), Noisy(), Noisy(),
    Noisy(), Noisy(), Noisy(), Noisy() };
  // Add elements via constructor:
  set<Noisy> ns(na, na+ sizeof na/sizeof(Noisy));
  // Ordinary insertion:
  Noisy n;
  ns.insert(n);
  cout << endl;
  // Check for set membership:
  cout << "ns.count(n)= " << ns.count(n) << endl;
  if(ns.find(n) != ns.end())
    cout << "n(" << n << ") found in ns" << endl;
  // Print elements:
  copy(ns.begin(), ns.end(),
    ostream_iterator<Noisy>(cout, " "));
  cout << endl;
  cout << "\n-----------\n";
  map<int, Noisy> nm;
  for(int i = 0; i < 10; i++)
    nm[i]; // Automatically makes pairs
  cout << "\n-----------\n";
  for(int j = 0; j < nm.size(); j++)
    cout << "nm[" << j <<"] = " << nm[j] << endl;
  cout << "\n-----------\n";
  nm[10] = n;
  cout << "\n-----------\n";
  nm.insert(make_pair(47, n));
  cout << "\n-----------\n";
  cout << "\n nm.count(10)= "
    << nm.count(10) << endl;
  cout << "nm.count(11)= "
    << nm.count(11) << endl;
  map<int, Noisy>::iterator it = nm.find(6);
  if(it != nm.end())
    cout << "value:" << (*it).second
      << " found in nm at location 6" << endl;
  for(it = nm.begin(); it != nm.end(); it++)
    cout << (*it).first << ":"
      << (*it).second << ", ";
  cout << "\n-----------\n";
} ///:~
The set<Noisy> object ns is created using two iterators into an array of
Noisy objects, but there is also a default constructor and a
copy-constructor, and you can pass in an object that provides an alternate
scheme for doing comparisons. Both sets and maps have an insert( ) member
function to put things in, and there are a couple of different ways to check
to see if an object is already in an associative container: count( ), when
given a key, will tell you how many times that key occurs (this can only be
zero or one in a set or map, but it can be more than one with a multiset or
multimap). The find( ) member function will produce an iterator indicating
the first occurrence (with set and map, the only occurrence) of the key that
you give it, or the past-the-end iterator if it can't find the key. The
count( ) and find( ) member functions exist for all the associative
containers, which makes sense. The associative containers also have member
functions lower_bound( ), upper_bound( ) and equal_range( ), which actually
only make sense for multiset and multimap, as you shall see (but don't try
to figure out how they would be useful for set and map, since they are
designed for dealing with a range of duplicate keys, which those containers
don't allow).
Maurus