#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//can't use reference arg
//bool Filter( const wstring& s, const wstring& pattern )
bool Filter( const wstring s, const wstring pattern )
{
return wstring::npos != s.find( pattern );
}
void main()
{
vector<wstring> vec;
vec.push_back( L"AAA" );
vec.push_back( L"BBB" );
vec.push_back( L"CCC" );
remove_if( vec.begin(), vec.end(), bind2nd( ptr_fun(Filter), L"C" ) );
}
C++愤恨者札记10——bind2nd示例
本文介绍了如何使用C++中std库提供的模板函数,结合bind2nd和ptr_fun来实现向量中特定元素的移除。通过定义一个过滤函数并应用remove_if算法,有效地从向量中移除了所有匹配特定模式的元素。

被折叠的 条评论
为什么被折叠?



