• <bdo id='Gnht9'></bdo><ul id='Gnht9'></ul>
    1. <legend id='Gnht9'><style id='Gnht9'><dir id='Gnht9'><q id='Gnht9'></q></dir></style></legend>

    2. <small id='Gnht9'></small><noframes id='Gnht9'>

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

        如何更新 std::set 的现有元素?

        How to update an existing element of std::set?(如何更新 std::set 的现有元素?)
          <bdo id='e4bkt'></bdo><ul id='e4bkt'></ul>

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

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

            1. <small id='e4bkt'></small><noframes id='e4bkt'>

              <tfoot id='e4bkt'></tfoot>
                    <tbody id='e4bkt'></tbody>

                  本文介绍了如何更新 std::set 的现有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 std::set,我想更新一些值其中存在的元素.请注意,我正在更新的值不会更改集合中的顺序:

                  I have a std::set<Foo>, and I'd like to update some value of an existing element therein. Note that the value I'm updating does not change the order in the set:

                  #include <iostream>
                  #include <set>
                  #include <utility>
                  
                  struct Foo {
                    Foo(int i, int j) : id(i), val(j) {}
                    int id;
                    int val;
                    bool operator<(const Foo& other) const {
                      return id < other.id;
                    }
                  };
                  
                  typedef std::set<Foo> Set;
                  
                  void update(Set& s, Foo f) {
                    std::pair<Set::iterator, bool> p = s.insert(f);
                    bool alreadyThere = p.second;
                    if (alreadyThere)
                      p.first->val += f.val; // error: assignment of data-member
                                             // ‘Foo::val’ in read-only structure
                  }
                  
                  int main(int argc, char** argv){
                    Set s;
                    update(s, Foo(1, 10));
                    update(s, Foo(1, 5));
                    // Now there should be one Foo object with val==15 in the set.                                                                
                    return 0;
                  }
                  

                  有什么简洁的方法可以做到这一点吗?或者我是否必须检查该元素是否已经存在,如果存在,将其删除,添加值并重新插入?

                  Is there any concise way to do this? Or do I have to check if the element is already there, and if so, remove it, add the value and re-insert?

                  推荐答案

                  由于 val 不参与比较,可以声明为 mutable

                  Since val is not involved in comparison, it could be declared mutable

                  struct Foo {
                    Foo(int i, int j) : id(i), val(j) {}
                    int id;
                    mutable int val;
                    bool operator<(const Foo& other) const {
                      return id < other.id;
                    }
                  };
                  

                  这意味着 val 的值可能会在逻辑常量 Foo 中发生变化,这意味着它不应该影响其他比较运算符等.

                  This implies that the value of val may change in a logically-const Foo, which means that it shouldn't affect other comparison operators etc.

                  或者您可以直接删除和插入,如果插入使用旧位置就在之后的位置作为提示,则需要O(1)额外的时间(与访问和修改相比).

                  Or you could just remove and insert, that takes O(1) additional time (compared to accessing and modifying) if insertion uses the position just before just after the old one as the hint.

                  类似于:

                  bool alreadyThere = !p.second; // you forgot the !
                  if (alreadyThere)
                  {
                      Set::iterator hint = p.first;
                      hint++;
                      s.erase(p.first);
                      s.insert(hint, f);
                  }
                  

                  这篇关于如何更新 std::set 的现有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                    <bdo id='13jbE'></bdo><ul id='13jbE'></ul>
                      <tbody id='13jbE'></tbody>
                  • <tfoot id='13jbE'></tfoot>

                    1. <small id='13jbE'></small><noframes id='13jbE'>

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

                          1. <legend id='13jbE'><style id='13jbE'><dir id='13jbE'><q id='13jbE'></q></dir></style></legend>