对集合进行排序< string>基于长度 [英] Sorting a set<string> on the basis of length

查看:136
本文介绍了对集合进行排序< string>基于长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与

我想对集执行 sort()

我的代码是

#include <set>
#include <string>
#include <iostream>
#include <algorithm>
int main() {
  using namespace std;
  string s = "abc";
  set<string> results;
  do {
    for (int n = 1; n <= s.size(); ++n) {
      results.insert(s.substr(0, n));
    }
  } while (next_permutation(s.begin(), s.end()));

  sort (results.begin(),results.end());[](string a, string b)->bool{

              size_t alength = a.length();
              size_t blength = b.length();
              return (alength < blength);
  });
  for (set<string>::const_iterator x = results.begin(); x != results.end(); ++x) {
    cout << *x << '\n';
  }
  return 0;
}

但是错误的数量和类型非常复杂,如何解决它们。有人告诉我这个代码有什么问题。

But the numbers and types of errors were so complex that I couldn't understand how to fix them. Can someone tell me whats wrong with this code.

推荐答案

编辑 a href =http://stackoverflow.com/questions/3844909/sorting-a-setstring-on-the-basis-of-length/3846129#3846129>史蒂夫·汤森的解决方案实际上是你'重新搜索,作为一个C ++ 0x Lambda,我写为C ++ 03代码如下。

Edit: Note that Steve Townsend's solution is actually the one you're searching for, as he inlines as a C++0x Lambda what I write as C++03 code below.

另一个解决方案是定制 std :: set 排序函数:

Another solution would be to customize the std::set ordering function:

std :: set 有自己的顺序,一旦它被构造,不应该改变它。所以,以下代码:

The std::set has its own ordering, and you are not supposed to change it once it is constructed. So, the following code:

int main(int argc, char* argv[])
{
    std::set<std::string> aSet ;

    aSet.insert("aaaaa") ;
    aSet.insert("bbbbb") ;
    aSet.insert("ccccccc") ;
    aSet.insert("ddddddd") ;
    aSet.insert("e") ;
    aSet.insert("f") ;

    outputSet(aSet) ;

    return 0 ;
}

将输出以下结果:

 - aaaaa
 - bbbbb
 - ccccccc
 - ddddddd
 - e
 - f



...但您可以自定义其排序函数



现在,如果需要,您可以使用自己的比较函数自定义您的集合:

... But you can customize its ordering function

Now, if you want, you can customize your set by using your own comparison function:

struct MyStringLengthCompare
{
    bool operator () (const std::string & p_lhs, const std::string & p_rhs)
    {
        const size_t lhsLength = p_lhs.length() ;
        const size_t rhsLength = p_rhs.length() ;

        if(lhsLength == rhsLength)
        {
            return (p_lhs < p_rhs) ; // when two strings have the same
                                     // length, defaults to the normal
                                     // string comparison
        }

        return (lhsLength < rhsLength) ; // compares with the length
    }
} ;

在这个比较函子中,我处理了长度相同但不同内容意味着不同字符串,因为我相信(可能是错误的)原始程序中的行为是一个错误。要在原始程序中编写行为,请从代码中删除如果块。

现在,你构造集合:

int main(int argc, char* argv[])
{
    std::set<std::string, MyStringLengthCompare> aSet ;

    aSet.insert("aaaaa") ;
    aSet.insert("bbbbb") ;
    aSet.insert("ccccccc") ;
    aSet.insert("ddddddd") ;
    aSet.insert("e") ;
    aSet.insert("f") ;

    outputSet(aSet) ;

    return 0 ;
}

现在,集合将使用函数 MyStringLengthCompare 以订购其项目,因此,此代码将输出:

The set will now use the functor MyStringLengthCompare to order its items, and thus, this code will output:

 - e
 - f
 - aaaaa
 - bbbbb
 - ccccccc
 - ddddddd



但请注意订购错误!



创建自己的排序函数时,必须遵循以下规则:

But beware of the ordering mistake!

When you create your own ordering function, it must follow the following rule:


如果(lhs

return true if (lhs < rhs) is true, return false otherwise



<如果由于某种原因,你的顺序功能不尊重它,你的手上会有一个破碎的集合。

If for some reason your ordering function does not respect it, you'll have a broken set on your hands.

这篇关于对集合进行排序&lt; string&gt;基于长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆