Read data from a file and save that to vector
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
using namespace std;
int main( )
{
ifstream stock_file( "data.txt" );
vector<float> price( (istream_iterator<float>( stock_file )),istream_iterator<float>() );
vector<float> percent_change( price.size() );
adjacent_difference( price.begin(), price.end(),percent_change.begin() );
copy( price.begin(), price.begin()+5,ostream_iterator<float>( cout, " " ) );
}
Related examples in the same category