#include #include #include #include #include #include #include #include "mySTD.h" using namespace std; int main () { // create a small vector pushing back numbers vector data_small; data_small.push_back(2.); data_small.push_back(3.); data_small.push_back(4.); data_small.push_back(5.); // compute average and variance through templace functions cout << mySTD::myAve(data_small.begin() , data_small.end() ) << endl; cout << mySTD::myVar(data_small.begin() , data_small.end() ) << endl; // loop over vector elements for( int k = 0 ; k < data_small.size() ; k++) { cout << "Value ==>> " << data_small[k] << endl; } // loop over vector elements using iterators for( vector::iterator myIt = data_small.begin(); myIt != data_small.end(); myIt++) { cout << "Value ==>> " << *myIt << endl; } // create a vector and read elements from file // 1 - the naive way int N = 1000000; string filename = "data.txt"; vector data_to_read ; mySTD::ReadVector( filename , N , data_to_read ); // 2 - the professional way ifstream inputFile(filename); std::vector data((std::istream_iterator(inputFile)),std::istream_iterator()); inputFile.close(); // print the vector elements to stdin using the function copy ostream_iterator< double > output( cout, "\n" ); copy( data.begin(), data.end(), output ); // print to file usign the function copy ofstream fileout("test.test"); ostream_iterator< double > output_file( fileout, "\n" ); copy( data.begin(), data.end(), output_file ); // print vector using a function mySTD::print( data.begin() , data.end() ); // now use an algorithm in the STL to sort elements clock_t t = clock(); sort(data.begin(), data.end()); t = clock() - t; float elapsed_time = ((float)t)/CLOCKS_PER_SEC; cout << "It took me seconds " << elapsed_time << " to sort " << N << " elements" << endl; // now compute the mean and variance using functions cout << mySTD::myAve(data.begin(), data.end() ) << endl; cout << mySTD::myVar(data.begin(), data.end() ) << endl; // or even better use accumulators from STL double sum = std::accumulate(data.begin(), data.end(), 0.0); double mean = sum / data.size(); double sq_sum = std::inner_product(data.begin(), data.end(), data.begin(), 0.0); double stdev = std::sqrt(sq_sum / data.size() - mean * mean); // or use BOOST functions /* accumulator_set > acc; for_each(a_vec.begin(), a_vec.end(), bind(ref(acc), _1)); cout << mean(acc) << endl; cout << sqrt(variance(acc)) << endl; */ }