fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cstring>
  5.  
  6.  
  7. template <typename T>
  8. std::size_t count(const std::vector<T> & _v, const T&& _value)
  9. {
  10. std::size_t cnt = 0;
  11. for (const auto& it : _v)
  12. {
  13. if (it == _value) ++cnt;
  14. }
  15. return cnt;
  16. }
  17.  
  18. template<>
  19. std::size_t count(const std::vector<const char *> & _v, const char * const && _value)
  20. {
  21. std::size_t cnt = 0;
  22. for (const auto& it : _v)
  23. {
  24. if (strcmp(it, _value) == 0) ++cnt;
  25. }
  26. return cnt;
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32. std::vector<int> vi = { 0, 1, 7, 13, 7, 9, 14, 7, 7 };
  33. std::vector<double> vd = { 0, 1, 0, 1, 1, 1, 0 };
  34. std::vector<std::string> vs = { "abc", "d", "e", "abc", "f", "abc", "abc" };
  35. std::vector<const char *> vpc = { "abc", "d", "e", "abc", "f", "abc", "abc" };
  36.  
  37. std::cout << count(vi, 7) << std::endl;
  38. std::cout << count(vs, std::string("abc")) << std::endl;
  39. std::cout << count(vd, 1.0) << std::endl;
  40.  
  41. const char * search = "abc";
  42. std::cout << count(vpc, search) << std::endl;
  43. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:42:32: error: cannot bind ‘const char*’ lvalue to ‘const char* const&&’
  std::cout << count(vpc, search) << std::endl;
                                ^
prog.cpp:19:13: note:   initializing argument 2 of ‘std::size_t count(const std::vector<T>&, const T&&) [with T = const char*; std::size_t = long unsigned int]’
 std::size_t count(const std::vector<const char *> & _v, const char * const &&  _value)
             ^~~~~
stdout
Standard output is empty