Skip to content

Instantly share code, notes, and snippets.

@Rhomboid
Created December 11, 2012 20:06
Show Gist options
  • Select an option

  • Save Rhomboid/4261681 to your computer and use it in GitHub Desktop.

Select an option

Save Rhomboid/4261681 to your computer and use it in GitHub Desktop.
vector example
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
using namespace std;
vector<double> getlist()
{
cout << "Enter values separated by whitespace, EOF to end." << endl;
return vector<double>((istream_iterator<double>(cin)), istream_iterator<double>());
}
void printlist(const vector<double> &v, const string &message = "Values: ")
{
cout << message << endl;
for(size_t i = 0; i < v.size(); ++i)
cout << i << " " << v[i] << endl;
}
double average(const vector<double> &v)
{
return accumulate(v.begin(), v.end(), 0.) / v.size();
}
int min_index(const vector<double> &v)
{
return distance(v.begin(), min_element(v.begin(), v.end()));
}
void left_rotate(vector<double> &v)
{
double front = v.front();
v.erase(v.begin());
v.push_back(front);
}
int main()
{
vector<double> v = getlist();
if(v.empty()) {
cout << "You didn't enter anything." << endl;
return 1;
}
printlist(v);
cout << "Average value: " << average(v) << endl;
cout << "Index of smallest value: " << min_index(v) << endl;
left_rotate(v);
printlist(v, "Values after left rotate:");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment