<legend id='PJfCU'><style id='PJfCU'><dir id='PJfCU'><q id='PJfCU'></q></dir></style></legend>

    <small id='PJfCU'></small><noframes id='PJfCU'>

  1. <tfoot id='PJfCU'></tfoot>

    <i id='PJfCU'><tr id='PJfCU'><dt id='PJfCU'><q id='PJfCU'><span id='PJfCU'><b id='PJfCU'><form id='PJfCU'><ins id='PJfCU'></ins><ul id='PJfCU'></ul><sub id='PJfCU'></sub></form><legend id='PJfCU'></legend><bdo id='PJfCU'><pre id='PJfCU'><center id='PJfCU'></center></pre></bdo></b><th id='PJfCU'></th></span></q></dt></tr></i><div id='PJfCU'><tfoot id='PJfCU'></tfoot><dl id='PJfCU'><fieldset id='PJfCU'></fieldset></dl></div>

        <bdo id='PJfCU'></bdo><ul id='PJfCU'></ul>

    1. C++ std::set 更新很乏味:我无法就地更改元素

      时间:2024-05-12
      1. <small id='ZNSgc'></small><noframes id='ZNSgc'>

          <tbody id='ZNSgc'></tbody>
        • <legend id='ZNSgc'><style id='ZNSgc'><dir id='ZNSgc'><q id='ZNSgc'></q></dir></style></legend>
          <i id='ZNSgc'><tr id='ZNSgc'><dt id='ZNSgc'><q id='ZNSgc'><span id='ZNSgc'><b id='ZNSgc'><form id='ZNSgc'><ins id='ZNSgc'></ins><ul id='ZNSgc'></ul><sub id='ZNSgc'></sub></form><legend id='ZNSgc'></legend><bdo id='ZNSgc'><pre id='ZNSgc'><center id='ZNSgc'></center></pre></bdo></b><th id='ZNSgc'></th></span></q></dt></tr></i><div id='ZNSgc'><tfoot id='ZNSgc'></tfoot><dl id='ZNSgc'><fieldset id='ZNSgc'></fieldset></dl></div>
          <tfoot id='ZNSgc'></tfoot>
            <bdo id='ZNSgc'></bdo><ul id='ZNSgc'></ul>

                本文介绍了C++ std::set 更新很乏味:我无法就地更改元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我发现 std::set 上的更新操作很乏味,因为 cppreference.所以我目前做的是这样的:

                I find the update operation on std::set tedious since there's no such an API on cppreference. So what I currently do is something like this:

                //find element in set by iterator
                Element copy = *iterator;
                ... // update member value on copy, varies
                Set.erase(iterator);
                Set.insert(copy);
                

                基本上Set返回的迭代器是一个const_iterator,你不能直接改变它的值.

                Basically the iterator return by Set is a const_iterator and you can't change its value directly.

                有没有更好的方法来做到这一点?或者也许我应该通过创建我自己的来覆盖 std::set(我不知道它究竟是如何工作的......)

                Is there a better way to do this? Or maybe I should override std::set by creating my own (which I don't know exactly how it works..)

                推荐答案

                set 返回 const_iterators (标准说set::iteratorcode> 是 const,并且 set::const_iteratorset::iterator 实际上可能是同一类型- 请参阅 n3000.pdf 中的 23.2.4/6),因为它是一个有序容器.如果它返回一个常规的 iterator,您将被允许从容器下更改 items 值,这可能会改变排序.

                set returns const_iterators (the standard says set<T>::iterator is const, and that set<T>::const_iterator and set<T>::iterator may in fact be the same type - see 23.2.4/6 in n3000.pdf) because it is an ordered container. If it returned a regular iterator, you'd be allowed to change the items value out from under the container, potentially altering the ordering.

                您的解决方案是更改set 中的项目的惯用方法.

                Your solution is the idiomatic way to alter items in a set.

                这篇关于C++ std::set 更新很乏味:我无法就地更改元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何指定指向重载函数的指针? 下一篇:为什么使用 std::auto_ptr&lt;&gt; 是错误的?使用标准容器?

                相关文章

                  <bdo id='fPRsq'></bdo><ul id='fPRsq'></ul>
              • <tfoot id='fPRsq'></tfoot>
                <legend id='fPRsq'><style id='fPRsq'><dir id='fPRsq'><q id='fPRsq'></q></dir></style></legend>

                  <small id='fPRsq'></small><noframes id='fPRsq'>

                  <i id='fPRsq'><tr id='fPRsq'><dt id='fPRsq'><q id='fPRsq'><span id='fPRsq'><b id='fPRsq'><form id='fPRsq'><ins id='fPRsq'></ins><ul id='fPRsq'></ul><sub id='fPRsq'></sub></form><legend id='fPRsq'></legend><bdo id='fPRsq'><pre id='fPRsq'><center id='fPRsq'></center></pre></bdo></b><th id='fPRsq'></th></span></q></dt></tr></i><div id='fPRsq'><tfoot id='fPRsq'></tfoot><dl id='fPRsq'><fieldset id='fPRsq'></fieldset></dl></div>