Skip to content

Instantly share code, notes, and snippets.

@itsjohncs
Created November 19, 2012 07:15
Show Gist options
  • Save itsjohncs/4109393 to your computer and use it in GitHub Desktop.
Save itsjohncs/4109393 to your computer and use it in GitHub Desktop.
Vector Exercises
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print_vector(vector<string> values) {
for (int i = 0; i < values.size(); ++i) {
cout << values.at(i) << endl;
}
}
vector<string> reversed(vector<string> values) {
vector<string> new_vector;
for (int i = values.size() - 1; i >= 0; --i) {
new_vector.push_back(values.at(i));
}
return new_vector;
}
void reverse_inplace(vector<string> & values) {
for (int i = 0; i < values.size() / 2; ++i) {
swap(values.at(i), values.at(values.size() - i - 1));
}
}
int main() {
vector<string> values;
values.push_back("first");
values.push_back("second");
values.push_back("third");
values.push_back("fourth");
values.push_back("fifth");
cout << "--NORMAL--" << endl;
print_vector(values);
cout << "--REVERSED--" << endl;
print_vector(reversed(values));
cout << "--REVERSED INPLACE--" << endl;
reverse_inplace(values);
print_vector(values);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment