简体   繁体   English

是否可以使用std :: sort按字典顺序排序?

[英]Is it possible to use std::sort to sort by lexicographic order?

My problem looks like this: 我的问题看起来像这样:

  1. At the beginning u have to insert an amount of numbers. 在开始时,您必须插入一些数字。
  2. Next program counts the sum of digits of the number that u inserted in step one. 下一个程序计算您在第一步中插入的数字的位数之和。
  3. All scores are inserted in vector called vec 所有分数都插入称为vec的向量中

The problem is this: At the end of the program all numbers that You inserted in steps 1, must be sorted depends of theirs sums of digits(Sorting in increasing order). 问题是这样的:在程序末尾,您在步骤1中插入的​​所有数字都必须根据其数字总和进行排序(升序排列)。

And ATTENTION please! 请注意! For example if two of numbers(eg 123 and 12300) have the same sum of digits you have to sort them by the lexicographic order. 例如,如果两个数字(例如123和12300)具有相同的数字总和,则必须按词典顺序对其进行排序。

I don't want to create function build by myself but I would like to use “sort” function from library but I have problem with that..Is it possible to use sort function also to sorting by the lexicographic order? 我不想自己创建函数,但是我想使用库中的“ sort”函数,但是我对此有疑问。是否可以对字典顺序也使用sort函数进行排序? Can someone can help me? 有人可以帮我吗? Example input: 输入示例:

6 
13
36
27
12
4
123

Expected output: 预期产量:

12
13
4
123
27
36

My code: 我的代码:

#include<iostream>
#include<cmath>
#include<string>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;

int main()
{
    vector<vector<int> > vec;
    int num;
    int n;
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> num;
        vector<int> row;

        row.push_back(num);

        //conversion int to string:
        ostringstream ss;
        ss << num;
        string str = ss.str();

        int sum = 0;
        for (int g = 0; g < str.length(); g++){
            int pom = str[g] - '0';
            sum += pom;
        }

        row.push_back(sum);
        vec.push_back(row);
        row.clear();
    }

    //sort(vec[0][0], vec[vec.size()][0]);

    for (int i = 0; i < vec.size(); i++){
        for (int j = 0; j < 2; j++){
            //cout << vec[i][j] << " ";
        }
        cout << vec[i][0] << endl;
    }



    system("pause");
    return 0;
}

You could store each number as a string, but also pre-compute its digit-sum and keep both in a pair<int,string> , then put them into a vector<pair<int,string> and sort it. 您可以将每个数字存储为字符串,但也可以预先计算其数字和,并将两者保持在pair<int,string> ,然后将它们放入vector<pair<int,string>并对其进行sort No need for a custom comparator , the one for std::pair does exactly what you want. 不需要自定义比较器std::pair的一个就可以完全满足您的要求。

// note: std::pair<std::string,int> would not work
typedef std::pair<int,std::string> number;
std::vector<number> numbers;

// fill numbers such that number::first holds the digit sum
// and number::second the number as string.
// this is similar to your code

std::sort(numbers.begin(), numbers.end());

// now numbers are ordered as you want them

只需将合适的比较函数(或函子,例如lambda是自然的)传递给std::sort

Since you want to compare on sum of digits first and then lexicographically to break ties, it will be convenient to convert your input numbers to strings. 由于您要先比较数字总和,然后再按字典顺序比较以打破平局,因此将输入数字转换为字符串会很方便。

From there you can define a custom comparator to achieve the desired behavior: 在这里,您可以定义一个自定义比较器以实现所需的行为:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int sumDigits(string s)
{
    int sum = 0;
    for (unsigned int i=0; i<s.length(); ++i)
    {
        sum += s[i] - '0';
    }
    return sum;
}

bool digitSumComparator(int i, int j)
{
    int iSum = sumDigits(to_string(i));
    int jSum = sumDigits(to_string(j));
    if (iSum == jSum)
    {
        return iStr < jStr;
    }
    else
    {
        return iSum < jSum;
    }
}

int main()
{
    vector<int> v {6,13,36,27,12,4,123};
    sort(v.begin(), v.end(), digitSumComparator);
    for (vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
    {
        cout <<  *it << ' ';
    }
    cout << '\n';
    return 0;
}

Output: 输出:

$ g++ -std=c++11 digitsumsort.cpp
$ ./a.out
12 13 4 123 6 27 36

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM