Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <vector>
  5.  
  6. namespace my {
  7.     template <typename Iterator, typename U>
  8.     int count(Iterator begin, Iterator end, const U& u) {
  9.         int counter = 0;
  10.  
  11.         for(; begin != end; begin++) {
  12.             if (u == *begin) {
  13.                 ++counter;
  14.             }
  15.         }
  16.  
  17.         return counter;
  18.     }
  19. }
  20.  
  21. int main() {
  22.     std::vector<int> ints = {1, 2, 2, 3};
  23.     std::vector<std::string> strings = {"ala", "ma", "kota", "ala"};
  24.  
  25.     std::cout << my::count(ints.begin(), ints.end(), 2) << '\n'
  26.               << my::count(strings.begin(), strings.end(), "ala") << '\n';
  27.  
  28.     /*auto iterator = ints.begin();
  29.     iterator += 2;
  30.     --iterator;
  31.     std::cout << *iterator;*/
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement