fork(1) download
  1. // retorn position [ 0, (N-1) ] if found
  2. // return invalid position N if not found
  3. template < typename SEQUENCE_CONTAINER >
  4. typename SEQUENCE_CONTAINER::size_type lsearch( const SEQUENCE_CONTAINER& seq,
  5. const typename SEQUENCE_CONTAINER::value_type& data )
  6. {
  7. typename SEQUENCE_CONTAINER::size_type pos = 0 ;
  8.  
  9. for( const auto& value : seq )
  10. {
  11. if( data == value ) return pos ;
  12. else ++pos ;
  13. }
  14.  
  15. return seq.size() ;
  16. }
  17.  
  18. #include <vector>
  19. #include <list>
  20. #include <string>
  21. #include <iostream>
  22.  
  23. int main()
  24. {
  25. std::vector<int> vec { 53, 65, 64, 74, 74, 74, 32, 17, 23, 29, 86 } ;
  26. auto f = lsearch( vec, 17 ) ;
  27. if( f != vec.size() ) std::cout << "found at position " << f << '\n' ;
  28.  
  29. std::list<std::string> lst { "abc", "def", "ghi", "jkl", "mnop", "qrst", "uvwxyz" } ;
  30. std::cout << lsearch( lst, "mnop" ) << '\n' ;
  31.  
  32. std::string phrase = "STL container template for this function" ;
  33. std::cout << lsearch( phrase, 'L' ) << '\n' ;
  34. }
  35.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
found at position 7
4
2