|
Since you did not post your code, I don't know what you wrote. Here is an example:
- #include <iostream>
- #include <vector>
- #include <list>
- #include <iterator>
- #include <string>
- using namespace std;
- // modern trend is to use typename as possible,
- // class for typename is a misnomer.
- template<typename InputIterator, typename T>
- inline size_t count_my( InputIterator First, InputIterator Last, const T& Value )
- {
- size_t n=0;
- while(First!=Last)
- {
- if(*First == Value) // this may have problem since not all types have "==" defined
- ++n;
- ++First;
- }
- return n;
- }
- int main(int argc, char** argv)
- {
- vector<int> vi;
- size_t n;
- vi.push_back(1);
- vi.push_back(2);
- vi.push_back(1);
- vi.push_back(3);
- n = count_my(vi.begin(), vi.end(), 1);
- cout<<n<<endl; // outputs 2
- list<string> ls;
- ls.push_back("to");
- ls.push_back("be");
- ls.push_back("or");
- ls.push_back("not");
- ls.push_back("to");
- ls.push_back("be");
- n = count_my(ls.begin(), ls.end(), string("to"));
- cout<<n<<endl; // outputs 2
- return 0;
- }
复制代码 |
|