Computing an inner product of tuples represented as vectors : vector « vector « C++ Tutorial






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

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

  vector<double> x(N), y(N);


  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 = 0.0;
  for (long k = 0; k < N; ++k)
    sum += x[k] * y[k];

  cout << sum << endl;
  return 0;
}
4000








16.1.vector
16.1.1.Demonstrating the simplest STL vector constructors: duplicate chars
16.1.2.Demonstrating the simplest STL vector constructors: empty vector
16.1.3.Use generic vector to create vector of chars
16.1.4.Use generic vector to create vector of integers
16.1.5.creates a vector of ints with zero elements
16.1.6.creates a vector of 10 ints with value 100
16.1.7.Use generic vector to create vector of strings
16.1.8.The Instantiation of vector
16.1.9.Using the vector
16.1.10.get an iterator that points to the maximum with max_element
16.1.11.Assign elements in int array to vector
16.1.12.Store a class object in a vector
16.1.13.Read keyboard input to a vector
16.1.14.Add class to a vector and then delete them one by one
16.1.15.Pass vector to a function
16.1.16.Computing an inner product of tuples represented as vectors
16.1.17.Assign value to the last element
16.1.18.Demonstrating STL vector copying constructors
16.1.19.Instantiate one vector and initialize it to the contents of another
16.1.20.Demonstrating STL vector constructors with a user-defined type and showing copying explicitly
16.1.21.Vector Init Array
16.1.22.Demonstrating STL vector constructors with a user-defined type