- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 STL 关联容器(std::set
和 std::map
),其中包含一个 std::unique_ptr<>
键。实例。键定义等效于以下内容:
struct Key
{
std::unique_ptr<Object> object;
bool operator== (const Key& rhs) const { return object->equal (*rhs.object); }
bool operator< (const Key& rhs) const { return object->less (*rhs.object); }
}
众所周知,STL 关联容器(尤其是自 C++11 起)无法获取对要移动的键的非常量引用。我的 key 是不可复制的,所以 c++: Remove element from container and get it back不起作用。
有没有非 UB 的方法来克服这个问题?
我目前的解决方案如下:
template <typename T>
using map_pair_type = std::pair<typename T::key_type, typename T::mapped_type>;
template <typename T>
typename T::value_type take_set (T& container, typename T::iterator iterator)
{
typename T::value_type result = std::move (const_cast<typename T::value_type&> (*iterator));
container.erase (iterator);
return result;
}
template <typename T>
map_pair_type<T> take_map (T& container, typename T::iterator iterator)
{
map_pair_type<T> result {
std::move (const_cast<typename T::key_type&> (iterator->first)),
std::move (iterator->second)
};
container.erase (iterator);
return result;
}
最佳答案
这是其中之一:
Really sorry. We tried to make this work and couldn't get it through committee.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3586.pdf
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3645.pdf
据我所知,您的解决方案是最好的。您的 map 解决方案确实表现出未定义的行为。如果第二步抛出异常,它将变得非常糟糕。除此之外,我怀疑它会起作用。而且我怀疑我会因为这样说而被否决。
UB 的原因是键被定义为 const
(而不是仅仅被 const 引用引用)。在这种情况下丢弃 const
(并让移动构造函数修改对象)是 UB。
如果 N3586 被接受,您可以:
move_only_type mot = move(*s.remove(s.begin()));
或:
move_only_key mok = move(m.remove(m.begin())->first);
N3586/N3645在组委会中表现出色。它经过讨论并通过了工作组阶段,只是在全体委员会中被否决了。问题是 std::lib 必须提交 UB 才能实现它。尚未重新提交。
更新
现在可以在 C++17 中执行此操作,但成员函数称为 extract
而不是 remove
。
关于c++ - STL 关联容器 : erasing and getting back the (noncopyable) element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27671303/
假设您要按值从 vector 中删除单个元素。 remove 之间有什么区别? -删除: vector v; // add some values vector::iterator it = remo
class CSensor { public: CSensor(int nVal1,char* pVal2,unsigned int nVal3); CSensor(cons
在回答过程中another question我偶然发现 std::vector::erase() 和 std::deque::erase() 的措辞略有不同。 这就是 C++14 关于 std::de
我想处理 vector 中的元素一段时间。为了优化这一点,我不想在处理项目时删除它,而是在最后删除所有已处理的项目。 vector::iterator it; for(it = items.begin
是否 std::set erase ( x ) 在 STL 中以相同的方式实现 erase ( collection.find( x ) ); (其中 x 是 const key_type&) 吗?
This question already has answers here: How to catch .erase exception in vectors? (2个答案) 8天前关闭。 当我尝试
我有以下代码: #include using namespace std; int main() { set S; S.insert("item1"); S.insert("i
在'helper'函数中输出集合元素时,预计元素'2'已经被移除。但实际结果仍然是'1 2 3 4 5'。 但是,集合的大小是 4。 我想知道潜在的问题。 #include #include us
有点难以解释,但我想制作一个以图片为背景的网站,并在图片上覆盖一层灰色。然后我想在覆盖层上方添加一些文本,在文本所在的位置,灰色覆盖层被移除,因此文本是在没有灰色覆盖层的情况下写入的。 我在一年前用图
这是导致错误的代码: 工厂.h: #include #include namespace BaseSubsystems { template class CFactory
我正在研究结构 vector 。 当我试图用迭代器调用这个函数时,像这样: vec2.erase (vec2.begin()+iter2); 它向我发送了这个错误: "no match for 'op
进程文件: eraser or eraser.exe 进程名称: Eraser 进程类别:存在安全风险的进程 英文描述: eraser.exe is the main executable
我正在编写一个简单的程序,它使用了 std::map::erase . 该程序很好,但有一些我不明白的地方。 如果我传给 erase function 第一个迭代器超出第二个迭代器的间隔,该函数不会删
如果我的变量是 0 到 6294 之间的随机数,我如何让我的输出仅打印出第一个变量? 我的随机数代码是 int random = (int)(Math.random() * 6294); 例如,如果它
我正在尝试通过一个测试程序,并且我通过了所有测试,除了涉及到我的删除功能时,程序崩溃了。 我最好的猜测可能是前面或后面的哨兵节点正在被删除。或内存泄漏。 最佳答案 我怀疑有人愿意阅读所有这些代码并为您
背景:这是一个程序,用于将存储在 vector 中的数字逐位加 1。 前面有 0 的数字可以作为输入,但不能作为输出。例如:0123 和 123 都是有效输入 但 0124 是无效输出 而 124 是
我正在尝试创建一个垂直滚动的射击游戏,当您按下空格键时,会创建一颗子弹,然后当子弹离开屏幕时,子弹就会被销毁。我通过声明为 vector bullets; 的 vector 跟踪子弹当我试图销毁屏幕外
我在下面写这段代码,发现了这个奇怪的行为: #include #include #include using namespace std; int main() { map map1;
我为 SFML 编写了一个线程渲染器,它接受指向可绘制对象的指针并将它们存储在一个 vector 中以在每一帧中绘制。开始向 vector 添加对象和从 vector 中删除对象会经常导致段错误 (S
如果我想从 map 中删除单个元素(除了可能的错误检查之外我不关心返回值),有两种方法可以实现:erase按值键或删除: http://ideone.com/YWocN7 #include #inc
我是一名优秀的程序员,十分优秀!