- 论坛徽章:
- 0
|
特化了一下remove函数,貌似是最好的解决办法了
- //main.cpp
- #include <iostream>
- #include <fstream>
- #include <list>
- #include <iterator>
- #include <functional>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- template <typename T>
- class List : public list<T>
- {
- public:
- void remove(const char * const & value)
- {
- iterator first = begin();
- iterator last = end();
- iterator m_prev = first;
- while (first != last)
- {
- iterator next = first;
- ++next;
- if (strcmp(*first ,value) == 0)
- erase(first);
- else
- m_prev = first;
- first = next;
- }
- }
- };
- int main(void)
- {
- char ss[][10] = {"aaaa", "bbbb", "cccc"};
- List<char*> S;
- for(int i=0; i<=2; i++) {
- S.push_back(ss[i]);
- }
- S.remove("aaaa");
- copy(S.begin(), S.end(), ostream_iterator<char*>(cout, "\n"));
- system("pause");
- return 0;
- }
复制代码 |
|