0

First question on stackoverflow :) I'm relatively new to C++, and have never used templates, so forgive me if I'm doing something silly. I have a template function that combs through a list and checks for a specified element of a general type. That way I can specify whether it's looking for a string, or an int, or whatever.

template <class T>   
bool inList(T match, std::string list)  
{
    int listlen = sizeof(list);
    for (int i = 0; i <= listlen; i++) {
        if (list[i] == match) return true;
        else continue;
    }
    return false;
};

This is my call to inList(). testvec is a string vector with a few elements, including "test":

if (inList<string>("test", testvec))
    cout << "success!";
else cout << "fail :(";

To my dismay and confusion, upon compiling, I am slapped with the following error:

error: no matching function for call to 'inList(const char [5], std::vector<std::basic_string<char> >&)'

What am I doing incorrectly? :(

[EDIT] I neglected to mention that the template definition is in the global namespace. (it's a simple test program to see if my template will work, and it does not, apparently :( )

ZeroKnight
  • 518
  • 3
  • 17

1 Answers1

4

That's because there is no way to convert from a std::vector to a std::string. Instead, what you need to do is abstract over the concept of a collection.

You do this so that people can pass in any collection type they want. They can use an array, a vector, a string, a list, a deque... as long as it fits the 'concept' of a collection (here's to hoping c++1x comes with concepts!). They can even use their own in-house specially optimized collection types. That's the beauty of templates.

Using C++11 (works with any standard collection, primitive arrays, and user-defined types):

template<class elem_t, class list_t>
bool in_list(const elem_t& elem, const list_t& list) {
   for (const auto& i : list) {
      if (elem == i) {
         return true;
      }
   }
   return false;
}

EDIT: It's a non-standard extension to deduce std::initializer_list as a template argument, so provide an explicit override:

template<class elem_t>
bool in_list(const elem_t& elem, std::initializer_list<elem_t> list) {
   for (const auto& i : list) {
      if (elem == i) {
         return true;
      }
   }
   return false;
}

With this version, you can call it like:

int main() {
   std::vector<int> a = {1, 2, 3, 4, 5};
   std::cout << in_list(3, a) << std::endl;
   std::string b = "asdfg";
   std::cout << in_list('d', b) << std::endl;
   std::cout << in_list('d', "asdfg") << std::endl;
   std::cout << in_list(3, {1, 2, 3, 4, 5}) << std::endl;
   return 0;
}

And for those of us still in C++98, this will work for both strings and vectors, and some user-defined types. It will not work with raw arrays though.

template<class elem_t, class list_t>
bool in_list_98(const elem_t& elem, const list_t& list) {
   list_t::const_iterator end = list.end(); //prevent recomputation of end each iteration
   for (list_t::const_iterator i = list.begin(); i < end; ++i) {
      if (elem == *i) {
         return true;
      }
   }
   return false;
}

Or, you can go STL style:

template<class elem_t, class iterator_t>
bool in_list_stl(const elem_t& elem, iterator_t begin, iterator_t end) {
   for (iterator_t i = begin; i < end; ++i) {
      if (elem == *i) {
         return true;
      }
   }
   return false;
}
//call like std::string s = "asdf"; in_list_stl('s', s.begin(), s.end());

If I made a mistake, sorry, I don't have my compiler running right now...

Robert Mason
  • 3,949
  • 3
  • 31
  • 44
  • The variety is good. Short answer is to pass a string instead of a vector of strings expecting it to convert to a string. There is technically `includes()` or `find()` for this purpose though; no need to write a solid one yourself ;) – chris Apr 29 '12 at 03:45
  • I think chris removed his comment on my question before I refreshed my page, as it's not there anymore, since what he mentioned solved my problem. I feel very silly. I had `std::string` as an argument to `inList()` because I didn't know I could use `std::vector` literally. I figured I should have used `std::string` because that's what the vector took, but now I feel silly looking back on it. **Thank you both for your responses, your response is also helpful for future reference, Robert :)** – ZeroKnight Apr 29 '12 at 03:54
  • If you use std::includes or std::find, just remember to test against end(). in_list(a, b) == [](const auto& a, const auto& b) { return std::find(b.begin(), b.end(), a) != b.end(); } - note I mean that lambda as an abstraction, not as actual code - I know we don't have polymorphic lambdas. That's something we have to leave with haskell for now. – Robert Mason Apr 29 '12 at 04:08