fork(4) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. template<typename T>
  8. typename vector<T>::const_iterator FindFirstDuplicate( const vector<T>& v )
  9. {
  10. vector<T> nv;
  11. nv.reserve( v.size() );
  12. for( auto it1 = v.cbegin(); it1 != v.cend(); ++it1 ) // can be const (?)
  13. {
  14. auto it2 = find( nv.cbegin(), nv.cend(), *it1 ); // search elem in new vector
  15. if( it2 == nv.cend() ) // if not present,
  16. nv.push_back( *it1 ); // add in new vector
  17. else // else, means we found a dupe
  18. return v.begin() + ( it2 - nv.cbegin() );
  19. }
  20. return v.begin();
  21. }
  22.  
  23. int main() {
  24.  
  25. vector<int> v = { 1,2,3,4,5,6,7,8,9,10,3,11 };
  26. auto it = FindFirstDuplicate(v);
  27. cout << "first dupe is " << *it << " at " << it - v.cbegin() << endl;
  28.  
  29. std::rotate( v.begin(), it, v.end() ) ;
  30. return 0;
  31. }
Compilation error #stdin compilation error #stdout 0s 3432KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:29:38: error: no matching function for call to ‘rotate(std::vector<int>::iterator, __gnu_cxx::__normal_iterator<const int*, std::vector<int> >&, std::vector<int>::iterator)’
  std::rotate( v.begin(), it, v.end() ) ;
                                      ^
prog.cpp:29:38: note: candidate is:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from prog.cpp:3:
/usr/include/c++/4.8/bits/stl_algo.h:1699:5: note: template<class _FIter> void std::rotate(_FIter, _FIter, _FIter)
     rotate(_ForwardIterator __first, _ForwardIterator __middle,
     ^
/usr/include/c++/4.8/bits/stl_algo.h:1699:5: note:   template argument deduction/substitution failed:
prog.cpp:29:38: note:   deduced conflicting types for parameter ‘_FIter’ (‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ and ‘__gnu_cxx::__normal_iterator<const int*, std::vector<int> >’)
  std::rotate( v.begin(), it, v.end() ) ;
                                      ^
stdout
Standard output is empty