まあは、ちょうど現在、削除位置を特定し、この反復子の後に、このイテレータのすべてをバックアップし、すべてのベクトルを作成するためにイテレータを使用します。
#include <algorithm>
#include <vector>
// personally, I would pass "in" as a couple of iterators
template<typename T>
std::vector<std::vector<T>> list_one_removed(const std::vector<T>& in)
{
std::vector<std::vector<T>> result;
for (auto itor = in.begin(); itor != in.end(); ++itor) {
std::vector<T> buffer;
std::copy_if(in.begin(),
in.end(),
std::back_inserter(buffer),
[&itor](const T& t){ return &t != &(*itor); });
result.emplace_back(std::move(buffer));
}
return result;
}
あなたがループ "ごとに、" 新しいを持っている場合は、あなたが行うことができます:
#include <algorithm>
#include <vector>
template<typename T>
std::vector<std::vector<T>> list_one_removed(const std::vector<T>& in)
{
std::vector<std::vector<T>> result;
for (const auto& foo : in) {
std::vector<T> buffer;
std::copy_if(in.begin(),
in.end(),
std::back_inserter(buffer),
[&foo](const T& t){ return &t != &foo; });
result.emplace_back(std::move(buffer));
}
return result;
}
あなたがC++ 03を使用している場合:あなたは、より詳細(入力/出力と、おそらくあなたの現在のコード)を追加しない限り
#include <algorithm>
#include <vector>
template<typename T> struct identity {
const T& id_;
identity(const T& id) : id_(id) { }
bool operator()(const T& other) const
{
return &id_ == &other;
}
};
template<typename T>
std::vector<std::vector<T> > list_one_removed(const std::vector<T>& in)
{
std::vector<std::vector<T> > result;
for (typename std::vector<T>::const_iterator itor = in.begin();
itor != in.end();
++itor) {
std::vector<T> buffer;
std::remove_copy_if(in.begin(),
in.end(),
std::back_inserter(buffer),
identity<T>(*itor));
result.push_back(buffer);
}
return result;
}
あまりにも漠然とした、閉鎖されます。 – Nim
あなたは私たちにいくつかのコードを表示すると、答えはかなりシンプルです! – guitarflow
*最も簡単なのは、ベクトルの長さにわたって反復することです:コピーを作成し、そこからk番目の要素を削除します。それで十分ですか? – Beta