Last active
October 27, 2016 15:41
-
-
Save mgarod/b346570df66191c67f1603daa4c2b496 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <climits> | |
using namespace std; | |
class Max2{ | |
public: | |
Max2() : largest(INT_MIN), nextlargest(INT_MIN), lindex(-1), nlindex(-1) { }; | |
friend ostream& operator<<(ostream& os, Max2 m); | |
void insert(int n, int nindex); | |
void print(ostream& os); | |
private: | |
int largest, nextlargest, lindex, nlindex; | |
}; | |
ostream& operator<<(ostream& os, Max2 m) { | |
m.print(os); | |
return os; | |
} | |
void Max2::print(ostream& os) { | |
os << lindex << ": " << largest << ", " << nlindex << ": " << nextlargest; | |
} | |
void Max2::insert(int n, int nindex) { | |
if (n > largest) { | |
std::swap(largest, n); | |
std::swap(lindex, nindex); | |
} | |
if (n > nextlargest) { | |
std::swap(nextlargest, n); | |
std::swap(nlindex, nindex); | |
} | |
} | |
Max2 findMax2 (vector<int> v) { | |
Max2 answer; | |
for (int i = 0; i < v.size(); ++i) { | |
answer.insert(v[i], i); | |
} | |
return answer; | |
} | |
int main() { | |
vector<int> vec = {6, 2, 10, 2, 19, 5, -3, -7, 11, 41, 25}; | |
cout << findMax2(vec) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment