Created
November 13, 2018 01:19
-
-
Save abhijangda/2d222a702066ef4b099f17452c3ed778 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <algorithm> | |
| #include <iostream> | |
| #include <vector> | |
| #include <iterator> | |
| using namespace std; | |
| class Contest { | |
| std::vector <int> scores; | |
| Contest& operator=(Contest); | |
| public: | |
| Contest (std::vector<int> v) : scores(v) | |
| { | |
| std::cout << "Before scores"; | |
| copy (v.begin (), v.end (), std::ostream_iterator<int> (std::cout, " ")); | |
| std::cout << std::endl; | |
| } | |
| void print_after () | |
| { | |
| std::cout << "After scores"; | |
| copy (scores.begin (), scores.end (), std::ostream_iterator<int> (std::cout, " ")); | |
| std::cout << std::endl; | |
| } | |
| void swap (Contest& other); | |
| }; | |
| void print_vector (std::vector<int> v) | |
| { | |
| copy (v.begin (), v.end (), std::ostream_iterator<int> (std::cout, " ")); | |
| } | |
| void Contest::swap (Contest& other) | |
| { | |
| std::vector <int>* firstPtr = (std::vector<int>*)this; | |
| std::vector <int>* secondPtr = (std::vector<int>*)&other; | |
| firstPtr->swap (*secondPtr); | |
| } | |
| int main () | |
| { | |
| std::vector<int> first (10, 1); | |
| std::vector<int> second (10, 2); | |
| Contest c1 (first); | |
| Contest c2 (second); | |
| c1.swap(c2); | |
| c1.print_after (); | |
| c2.print_after (); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment