これはremove_copy_if
の候補のようです。私はboost
を使って何かを書いたことがあるかもしれませんが、おそらくもっと嫌なことに見えますが、あなたのアルゴリズムの一般化を提供します。
#include <boost/iterator/transform_iterator.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <algorithm>
#include <map>
#include <set>
#include <string>
struct filter_cond : std::unary_function<std::string, bool> {
filter_cond(std::string const &needle):needle(needle) { }
bool operator()(std::string const& arg) {
return (arg.find(needle) == std::string::npos);
}
std::string needle;
};
int main() {
std::set<std::string> result;
typedef std::map<std::string, int> map_type;
map_type map;
std::remove_copy_if(
boost::make_transform_iterator(map.begin(),
boost::bind(&map_type::value_type::first, _1)),
boost::make_transform_iterator(map.end(),
boost::bind(&map_type::value_type::first, _1)),
std::inserter(result, result.end()),
filter_cond("foo")
);
}
おそらくマニュアルループが好きでしょう。 C++ 1xでは、ラムダ式の方がはるかに優れています。呼び出し元のコードでは
template<class T>
struct StringCollector
{
public:
StringCollector(const std::string& str, std::set<std::string>& selectedKeys) : m_key(str), m_selectedKeys(selectedKeys)
{
}
void operator()(const std::pair<std::string,T>& strPair_in)
{
size_t found = strPair_in.first.find(m_key);
if(found != std::string::npos)
{
m_selectedKeys.insert(strPair_in.first);
}
}
private:
std::set<std::string>& m_selectedKeys;
std::string m_key;
};
:
私はこれを使用していないブーストを実装しようとして失敗しました。私は単にremove_copy_ifとinserterを意味します。何か案は? –
ええと、あなた自身の入力イテレータラッパーをコード化する必要があります。私はそれを行う別の方法を考えることができません。 –
もう一つのアイデアは、変換を使用し、拒否の場合に空の文字列を返すことです。しかし針が空文字列の場合はあいまいです:( –