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

      <small id='44FTf'></small><noframes id='44FTf'>

        <legend id='44FTf'><style id='44FTf'><dir id='44FTf'><q id='44FTf'></q></dir></style></legend>
          <bdo id='44FTf'></bdo><ul id='44FTf'></ul>

        `std::set` 有什么问题?

        What is wrong with `std::set`?(`std::set` 有什么问题?)
        <legend id='LnKms'><style id='LnKms'><dir id='LnKms'><q id='LnKms'></q></dir></style></legend>

            <tfoot id='LnKms'></tfoot>
              <tbody id='LnKms'></tbody>
              <bdo id='LnKms'></bdo><ul id='LnKms'></ul>

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

              1. <i id='LnKms'><tr id='LnKms'><dt id='LnKms'><q id='LnKms'><span id='LnKms'><b id='LnKms'><form id='LnKms'><ins id='LnKms'></ins><ul id='LnKms'></ul><sub id='LnKms'></sub></form><legend id='LnKms'></legend><bdo id='LnKms'><pre id='LnKms'><center id='LnKms'></center></pre></bdo></b><th id='LnKms'></th></span></q></dt></tr></i><div id='LnKms'><tfoot id='LnKms'></tfoot><dl id='LnKms'><fieldset id='LnKms'></fieldset></dl></div>
                  本文介绍了`std::set` 有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在另一个主题中,我试图解决这个问题.问题是从 std::string 中删除重复的字符.

                  In the other topic I was trying to solve this problem. The problem was to remove duplicate characters from a std::string.

                  std::string s= "saaangeetha";
                  

                  由于顺序不重要,所以我先对s进行排序,然后使用std::unique,最后调整大小得到想要的结果:

                  Since the order was not important, so I sorted s first, and then used std::unique and finally resized it to get the desired result:

                  aeghnst
                  

                  没错!

                  现在我想做同样的事情,但同时我希望字符的顺序完好无损.意思是,我想要这个输出:

                  Now I want to do the same, but at the same time I want the order of characters intact. Means, I want this output:

                  sangeth
                  

                  所以我写了这个:

                  template<typename T>
                  struct is_repeated
                  {
                      std::set<T>  unique;
                      bool operator()(T c) { return !unique.insert(c).second; }
                  }; 
                  int main() {
                      std::string s= "saaangeetha";
                      s.erase(std::remove_if(s.begin(), s.end(), is_repeated<char>()), s.end()); 
                      std::cout << s ;
                  }
                  

                  给出这个输出:

                  saangeth
                  

                  也就是说,a 被重复了,虽然其他的重复没有了.代码有什么问题?

                  That is, a is repeated, though other repetitions gone. What is wrong with the code?

                  无论如何我更改我的代码:(见评论)

                  Anyway I change my code a bit: (see the comment)

                  template<typename T>
                  struct is_repeated
                  {
                      std::set<T> & unique;  //made reference!
                      is_repeated(std::set<T> &s) : unique(s) {} //added line!
                      bool operator()(T c) { return !unique.insert(c).second; }
                  }; 
                  int main() {
                      std::string s= "saaangeetha";
                      std::set<char> set; //added line!
                      s.erase(std::remove_if(s.begin(),s.end(),is_repeated<char>(set)),s.end()); 
                      std::cout << s ;
                  }
                  

                  输出:

                  sangeth
                  

                  问题解决了!

                  那么第一个解决方案有什么问题?

                  So what is wrong with the first solution?

                  另外,如果我不使成员变量 unique 引用类型,那么 问题不去.

                  Also, if I don't make the member variable unique reference type, then the problem doesn't go.

                  std::setis_repeated 函子有什么问题?问题究竟出在哪里?

                  What is wrong with std::set or is_repeated functor? Where exactly is the problem?

                  我还注意到,如果 is_repeated 函子被复制到某处,那么它的每个成员也会被复制.我在这里看不到问题!

                  I also note that if the is_repeated functor is copied somewhere, then every member of it is also copied. I don't see the problem here!

                  推荐答案

                  在 GCC (libstdc++) 中,remove_if 基本上实现为

                      template<typename It, typename Pred>
                      It remove_if(It first, It last, Pred predicate) {
                        first = std::find_if(first, last, predicate);
                      //                                  ^^^^^^^^^
                        if (first == last)
                           return first;
                        else {
                           It result = first;
                           ++ result;
                           for (; first != last; ++ first) {
                             if (!predicate(*first)) {
                      //          ^^^^^^^^^
                                *result = std::move(*first);
                                ++ result;
                             }
                           }
                        }
                      }
                  

                  请注意,您的谓词被按值传递给find_if,因此在find_if中修改的结构和集合不会传播回调用者.

                  Note that your predicate is passed by-value to find_if, so the struct, and therefore the set, modified inside find_if will not be propagated back to caller.

                  由于第一个副本出现在:

                  Since the first duplicate appears at:

                    saaangeetha
                  //  ^
                  

                  初始 "sa" 将在 find_if 调用后保留.同时,predicate 的集合是空的(find_if 中的插入是本地的).因此之后的循环将保留第三个 a.

                  The initial "sa" will be kept after the find_if call. Meanwhile, the predicate's set is empty (the insertions within find_if are local). Therefore the loop afterwards will keep the 3rd a.

                     sa | angeth
                  // ^^   ^^^^^^
                  // ||   kept by the loop in remove_if
                  // ||
                  // kept by find_if
                  

                  这篇关于`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 与自定义类对象的向量一起使用?)
                  <i id='Hf5pX'><tr id='Hf5pX'><dt id='Hf5pX'><q id='Hf5pX'><span id='Hf5pX'><b id='Hf5pX'><form id='Hf5pX'><ins id='Hf5pX'></ins><ul id='Hf5pX'></ul><sub id='Hf5pX'></sub></form><legend id='Hf5pX'></legend><bdo id='Hf5pX'><pre id='Hf5pX'><center id='Hf5pX'></center></pre></bdo></b><th id='Hf5pX'></th></span></q></dt></tr></i><div id='Hf5pX'><tfoot id='Hf5pX'></tfoot><dl id='Hf5pX'><fieldset id='Hf5pX'></fieldset></dl></div>

                    • <bdo id='Hf5pX'></bdo><ul id='Hf5pX'></ul>

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

                            <tbody id='Hf5pX'></tbody>
                        1. <legend id='Hf5pX'><style id='Hf5pX'><dir id='Hf5pX'><q id='Hf5pX'></q></dir></style></legend>

                          <tfoot id='Hf5pX'></tfoot>