Skip to content

Instantly share code, notes, and snippets.

@DanielDe
Created January 12, 2013 20:06
Show Gist options
  • Save DanielDe/4520250 to your computer and use it in GitHub Desktop.
Save DanielDe/4520250 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
void print_vec(const vector<int> &);
int main() {
vector<int> test;
test.push_back(3);
test.push_back(1);
test.push_back(4);
test.push_back(1);
test.push_back(5);
test.push_back(9);
print_vec(test);
return 0;
}
void print_vec(const vector<int> & v) {
for (unsigned i = 0; i < v.size(); ++i)
cout << v.at(i) << endl;
}
#include <iostream>
#include <vector>
using namespace std;
void print_vec_rev(const vector<int> &);
int main() {
vector<int> test;
test.push_back(3);
test.push_back(1);
test.push_back(4);
test.push_back(1);
test.push_back(5);
test.push_back(9);
print_vec_rev(test);
return 0;
}
void print_vec_rev(const vector<int> & v) {
for (int i = v.size() - 1; i >= 0; --i)
cout << v.at(i) << endl;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> frequencies(100, 0);
int next = 0;
while (cin >> next) {
frequencies.at(next - 1)++;
}
int max_freq = 0;
int max_num = 0;
for (unsigned i = 0; i < frequencies.size(); ++i) {
if (frequencies.at(i) > max_freq) {
max_freq = frequencies.at(i);
max_num = i + 1;
}
}
cout << "Most frequently occurring number: " << max_num << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
void print_vec(const vector<string> &);
vector<string> merge(const vector<string> &, const vector<string> &);
int main() {
vector<string> v1;
v1.push_back("apple");
v1.push_back("carrot");
v1.push_back("disc");
vector<string> v2;
v2.push_back("bear");
v2.push_back("echo");
v2.push_back("ferret");
v2.push_back("hamster");
vector<string> merged = merge(v1, v2);
print_vec(merged);
}
void print_vec(const vector<string> & v) {
for (unsigned i = 0; i < v.size(); ++i)
cout << v.at(i) << endl;
}
vector<string> merge(const vector<string> & v1, const vector<string> & v2) {
vector<string> result;
unsigned i = 0;
unsigned j = 0;
while (i < v1.size() && j < v2.size()) {
if (v1.at(i) < v2.at(j)) {
result.push_back(v1.at(i));
++i;
} else {
result.push_back(v2.at(j));
++j;
}
}
for (; i < v1.size(); ++i)
result.push_back(v1.at(i));
for (; j < v2.size(); ++j)
result.push_back(v2.at(j));
return result;
}
#include <iostream>
using namespace std;
struct Country {
string name;
unsigned population;
double area;
};
int main() {
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
struct Country {
string name;
unsigned population;
double area;
};
string largest_population(const vector<Country> &);
string largest_area(const vector<Country> &);
int main() {
Country usa;
usa.name = "United States of America";
usa.population = 311000000;
usa.area = 3794000;
Country canada;
canada.name = "Canada";
canada.population = 34000000;
canada.area = 3850000;
Country mexico;
mexico.name = "Mexico";
mexico.population = 112000000;
mexico.area = 1958000;
vector<Country> countries;
countries.push_back(usa);
countries.push_back(canada);
countries.push_back(mexico);
cout << "Largest population: " << largest_population(countries) << endl;
cout << "Largest area: " << largest_area(countries) << endl;
return 0;
}
string largest_population(const vector<Country> & c) {
string name = "";
unsigned largest = 0;
for (unsigned i = 0; i < c.size(); ++i) {
if (c.at(i).population > largest) {
largest = c.at(i).population;
name = c.at(i).name;
}
}
return name;
}
string largest_area(const vector<Country> & c) {
string name = "";
double largest = 0;
for (unsigned i = 0; i < c.size(); ++i) {
if (c.at(i).area > largest) {
largest = c.at(i).area;
name = c.at(i).name;
}
}
return name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment