// retorn position [ 0, (N-1) ] if found
// return invalid position N if not found
template < typename SEQUENCE_CONTAINER >
typename SEQUENCE_CONTAINER::size_type lsearch( const SEQUENCE_CONTAINER& seq,
const typename SEQUENCE_CONTAINER::value_type& data )
{
typename SEQUENCE_CONTAINER::size_type pos = 0 ;
for( const auto& value : seq )
{
if( data == value ) return pos ;
else ++pos ;
}
return seq.size() ;
}
#include <vector>
#include <list>
#include <string>
#include <iostream>
int main()
{
std::vector<int> vec { 53, 65, 64, 74, 74, 74, 32, 17, 23, 29, 86 } ;
auto f = lsearch( vec, 17 ) ;
if( f != vec.size() ) std::cout << "found at position " << f << '\n' ;
std::list<std::string> lst { "abc", "def", "ghi", "jkl", "mnop", "qrst", "uvwxyz" } ;
std::cout << lsearch( lst, "mnop" ) << '\n' ;
std::string phrase = "STL container template for this function" ;
std::cout << lsearch( phrase, 'L' ) << '\n' ;
}
Ly8gcmV0b3JuIHBvc2l0aW9uIFsgMCwgKE4tMSkgXSBpZiBmb3VuZAovLyByZXR1cm4gaW52YWxpZCBwb3NpdGlvbiBOIGlmIG5vdCBmb3VuZAp0ZW1wbGF0ZSA8IHR5cGVuYW1lIFNFUVVFTkNFX0NPTlRBSU5FUiA+CnR5cGVuYW1lIFNFUVVFTkNFX0NPTlRBSU5FUjo6c2l6ZV90eXBlIGxzZWFyY2goIGNvbnN0IFNFUVVFTkNFX0NPTlRBSU5FUiYgc2VxLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgY29uc3QgdHlwZW5hbWUgU0VRVUVOQ0VfQ09OVEFJTkVSOjp2YWx1ZV90eXBlJiBkYXRhICkKewogICB0eXBlbmFtZSBTRVFVRU5DRV9DT05UQUlORVI6OnNpemVfdHlwZSBwb3MgPSAwIDsKCglmb3IoIGNvbnN0IGF1dG8mIHZhbHVlIDogc2VxICkKCXsKCQlpZiggZGF0YSA9PSB2YWx1ZSApIHJldHVybiBwb3MgOwoJCWVsc2UgKytwb3MgOwoJfQoKCXJldHVybiBzZXEuc2l6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