问题标签 [erase-remove-idiom]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
225 浏览

c++ - 从向量中移除项目,并对移除的项目进行变异

我有一个std::vector<std::shared_ptr<Foo>>我想从中删除匹配某个谓词的项目。被移除的对象应该有一个被调用的方法,该方法设置一些状态以便在其他地方使用。

当返回 true 时,我不应该在谓词函数中这样做吗?感觉有点像混合关注点,但我能想到的唯一选择似乎更丑陋。

0 投票
3 回答
134022 浏览

c++ - 删除第一个和最后一个字符 C++

如何从 std::string 中删除第一个和最后一个字符,我已经在执行以下代码。

但是这段代码只删除了最后一个字符

如何也删除第一个字符?

0 投票
2 回答
2207 浏览

c++ - 从 QHash 中删除元素范围

我使用 QHash 作为容器,我的任务是删除所有满足谓词的项目。起初我想使用Erase-remove 习惯用法,结果发现 QHash 没有删除范围的选项,而只有一个通过迭代器删除单个元素的函数

std::unordered_map(概念上接近Qt的QHash)具有去除范围的功能

这意味着一个问题:为什么 QHash 没有类似的功能以及如何从满足谓词的 QHash 中删除项目的最佳方式?

0 投票
2 回答
3404 浏览

java - 使用方法删除重复项

我构建了删除重复项,但是我正在考虑如何使用一种方法,该方法使用以下标头从整数数组列表中删除重复元素:

编写一个测试程序,提示用户在列表中输入 10 个整数,并显示由一个空格分隔的不同整数。

这是我的代码,请修改,如何使用方法和测试。

0 投票
1 回答
650 浏览

c++ - In c++, is it safe to use std::numeric_limits::max() as a special "flag"?

Given

where ind is 1sorted in ascending order.

I want to do the equivalent of the following:

I came up with this:

This is very fast, but it doesn't feel right to use the std::numeric_limits::max() as a flag in this context.

Of course feelings shouldn't play too much into the equation ... clearly comparing the doubles within the std::remove is safe, and the limit will never occur in practice in this vector in a working application, so it should be ok, no?

Any thoughts on this?


1) Ref comment by the OP. – Alf

0 投票
1 回答
2597 浏览

c++ - std::erase 和 std::remove 组合删除特定元素不适用于特定示例

对于这个特定的例子,我的 GNU gdb Ubuntu 7.7.1 声明在返回 1 行: a = {2,3,7,1,5,4} 不是预期的(只删除一个 1),并且 b = {7 ,4,3,3,1} 这不是预期的。

我的期望是 b 应该是 a=2,3,7,5,4 和 b=7,4,3,3,1,7。

这里发生了什么事?

0 投票
0 回答
79 浏览

stl - 并行化 STL 列表擦除功能时出现分段错误

我正在尝试使用 Open MP 并行化我的 C++ 代码,但我遇到了分段错误,串行版本运行良好。我使用了 gdb 调试器,并且我知道在擦除 STL 列表的元素之一时会发生错误。

在下面的代码中,我有一个向量,其中包含具有 ID(boxID1 和 boxID2)的粒子对,并且每个粒子都有一个与之关联的实际联系人列表。然后我添加和删除粒子是否在给定列表中接触。

擦除时有没有办法避免这个错误?

0 投票
1 回答
205 浏览

c++ - Using erase-remove idiom for function

Stacked people.

Iam trying to implement an observer(esque?) pattern for my program. I have a component which stores what functions should be called if an event occours. My prolem is that i dont know how should i erase my function from the container, if the need arises. Tried storing the functions by reference, but iam not sure how to do that(or if thats possible.)

0 投票
2 回答
143 浏览

c++ - 向量包含类对象,类对象每个对象包含 3 个字符串。我如何找到特定的字符串,然后删除整个元素?

我有一个包含 3 个元素的类,例如 {first_name, Last_name, Phone}

我有一个包含这组信息的向量。我可以通过什么方式查找集合中的单个元素,例如 find(last_name),然后删除包含该特定姓氏的所有元素?

我尝试了许多示例,并在全球范围内的谷歌中进行了广泛搜索。请帮忙。附上一些代码:

现在这只是设置的相同基本代码。这是我尝试过的几件事。但是我越看代码所说的内容,它似乎就越不允许字符串参数......但是我不知道如何就特定字符串进行类争论......好吧我不知道我做错了什么。我有一种感觉,我可以用指针做到这一点,但整个指针的事情还没有点击。但这是我尝试过的一些事情。

当我编译项目时,头文件 stl_algo.h 打开并指向第 1133 行。任何帮助将不胜感激!谢谢你!

0 投票
1 回答
739 浏览

c++ - Why is a convenience helper for the erase-remove-idiom not provided by the standard?

Removing items from a collection in the STL requires a technique used so often that is has become an idiom: the erase-remove-idiom

One of the most common usages of this idiom is to remove an item of type T from a vector<T>

This is obviously very verbose, and violates the DRY principle - the vector in question is required 4 times there.

So my question is why does the standard not provide a convenience helper?

Something like

or

This could obviously be extended to

etc...