Computing an inner product of tuples represented as maps : map « map multimap « C++ Tutorial






#include <map>
#include <iostream>
using namespace std;

int main()
{
  const long N = 600000; // Length of tuples x and y
  const long S = 10;     // Sparseness factor

  map<long, double> x, y;
  for (long k = 0; 3 * k * S < N; ++k)
    x[3 * k * S] = 1.0;
  for (long k = 0; 5 * k * S < N; ++k)
    y[5 * k * S] = 1.0;

  double sum;
  map<long, double>::iterator ix, iy;

  for (sum = 0.0, ix = x.begin(); ix != x.end(); ++ix) {
    long i = ix->first;
    iy = y.find(i);
    if (iy != y.end())
      sum += ix->second * iy->second;
  }
  cout << sum << endl;
  return 0;
}
4000








23.1.map
23.1.1.Demonstrating an STL map
23.1.2.Use string as the key and value in a map
23.1.3.Define string-string map and loop through the value key-pair
23.1.4.Declare a char int map
23.1.5.Create string float map
23.1.6.Multiple map
23.1.7.Store objects in a map
23.1.8.A phone list in which a person's name is the key and the phone number is the value.
23.1.9.Computing an inner product of tuples represented as maps