插入到包含另一个结构的structur中并从中删除元素
insert in and delete an element from structur containing another structure
我有一个包含另一个结构的结构,我想从第一个结构中删除一个元素。这两个结构是这样定义的:1**第一种结构:
struct Solution {
Problem *_Problem;
vector<Tour> _tour; .
vector<unsigned int> length;
unsigned int nbrCleint;
unsigned int TotalDemande;
}
2**第二种结构:
struct Tour{
vector<unsigned int> clientVisite;
unsigned int demande;
vector<unsigned int> CostScenario;
Tour();
void clear();
};
现在我想从结构中删除"解决方案"中的一个元素;例如delete元素,其中解决方案_tour.clientVisite=="某个矢量"
我该怎么做?如何插入具有相同原理的元素??
注意C++<C++11
Solution s;
for (vector<Tour>::iterator i = s._tour.begin(); i != s._tour.end(); ++i) {
if (i->clientVisite == somevector) {
s._tour.erase(i); break;
}
}
struct Solution {
/// Remove every tour where the tour's visit array exactly matches visit
/// @param visit is a vector of unsigned ints representsing locations
/// @returns the number of tours removed
std::size_t remove_tour_where_visit_matches(const std::vector<unsigned int> visit)
{
auto original = _tour.size();
_tour.erase(std::remove_if(std::begin(_tour), std::end(_tour),
[&visit](const auto& tour)
{
return tour.clientVisite == visit;
}),
std::end(_tour));
return original - _tour.size();
}
Problem *_Problem;
vector<Tour> _tour;
vector<unsigned int> length;
unsigned int nbrCleint;
unsigned int TotalDemande;
};
我还没有c++11
然后使用自定义函数对象:
struct equal_visit
{
equal_visit(const std::vector<unsigned int>& l)
: _l(l)
{}
bool operator()(const Tour& r) const
{
return _l == r.clientVisite;
}
const std::vector<unsigned int>& _l;
};
struct Solution {
/// Remove every tour where the tour's visit array exactly matches visit
/// @param visit is a vector of unsigned ints representsing locations
/// @returns the number of tours removed
std::size_t
remove_tour_where_visit_matches(const std::vector<unsigned int>& visit)
{
std::size_t original = _tour.size();
_tour.erase(std::remove_if(_tour.begin(), _tour.end(),
equal_visit(visit)),
_tour.end());
return original - _tour.size();
}
Problem *_Problem;
vector<Tour> _tour;
vector<unsigned int> length;
unsigned int nbrCleint;
unsigned int TotalDemande;
};
相关文章:
- 如何从存储在std::映射中的std::集中删除元素
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 从矢量中删除元素后出现隔离错误
- 如何使用remove_if从矢量中删除元素
- 如何从使用 for 循环中的矢量大小的矢量中删除元素
- C++映射不删除元素
- 如何从标准::元组中删除元素?
- 在动态数组中添加/删除C++元素
- 如何通过比较彼此的成员从QStringList中删除元素
- 如何修复从矢量中删除元素的错误?
- 从 std::set 中删除元素,同时在 C++17 中迭代该元素
- 使用remove_if从矢量中删除元素
- 用C++从三维矢量中删除元素
- 从对象数组中删除元素
- 如何在不使用 vector::erase() 的情况下编写自定义 Vector 方法来删除元素?
- C++ - 按自定义数据类型向量的值删除元素
- 从任意容器中廉价删除元素的惯用方法?
- 从数组中删除元素不起作用的函数
- 删除元素 BST
- 从向量中删除元素时未处理的异常