makrusak

code

Feb 26th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <string>
  5. #include <vector>
  6. #include <list>
  7. #include <deque>
  8. #include <set>
  9. using namespace std;
  10.  
  11. template<typename Container>
  12. typename Container::iterator returnLastRepeat(Container &cont)
  13. {
  14.         typename Container::iterator ans = cont.end();
  15.         for (typename Container::iterator it1 = cont.begin(); it1 != cont.end(); it1++)
  16.         {              
  17.                 for (typename Container::iterator it2 = cont.begin(); it2 != it1; it2++)
  18.                 {
  19.                         if (*it1 == *it2) ans = it1;
  20.                 }
  21.         }
  22.         return ans;
  23. }
  24.  
  25. int main()
  26. {
  27.         int arr1[10] = {1, 2, 3, 4, 4, 5, 6, 6, 7, 8};
  28.         vector<int> cont1 (arr1, arr1 + 10);
  29.         cout << "Last repeating element in vector: " << (*returnLastRepeat(cont1)) << endl;
  30.        
  31.         double arr2[5] = {1.3, 5.7, 9.4, 5.7, 10.3};
  32.         list<double> cont2 (arr2, arr2 + 5);
  33.         cout << "Last repeating element in list: " << *returnLastRepeat(cont2) << endl;
  34.  
  35.         string arr3[8] = {"one", "two", "three", "four", "three", "two", "one", "ten"};
  36.         set<string> cont3 (arr3, arr3 + 8);
  37.         if (returnLastRepeat(cont3)==cont3.end()) cout << "GOOD\n";
  38.         else cout << "Last repeating element in set: " << *returnLastRepeat(cont3) << endl;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment