C ++:如何按数字顺序对数字字符串向量进行排序?

德克斯特

在我的程序中,我有一个通过用户输入填充的空字符串向量。该程序旨在从用户输入中获取数字,然后按从小到大的顺序对这些数字进行排序(数据类型是字符串,以便更容易检查不需要的输入,例如空格、字母、标点符号等)。实际上,程序根据起始数字而不是大小对数字进行排序。如何更改程序以按我想要的方式排序?

#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    vector<string> vect;
    string input;
    int intInput;
    int entries = 0;
    int i = 0;
    int x = 0;

    while (x < 1)
    {
        i++;
        cout << "Please input an integer. When finished providing numbers to organize, 
                         input any character that isn't an integer:\n";
        vect.resize(i+1);
        getline(cin, input);
        cout << endl;

        stringstream ss(input);
        if (ss >> intInput)
        {
            if (ss.eof())
            {
                vect.push_back(input); 
                entries++;
            }
            else
            {
                cout << "Error: Invalid input.\n\n";
            }
        }   
        else if (entries < 1)
        {
            cout << "Error: Invalid input.\n\n";
            i = -1;
            continue;
        }   
        else if (entries >= 1)
        {
            break;
        }       
    }
    
    cout << "All done? Organizing numbers!\n";
    
    sort(vect.begin(), vect.end());
    
    for (int j = 0; j < vect.size(); j++)
    {   
        cout << vect[j] << endl;
    }
    
    return 0;
}

我尝试了各种方法将字符串数据转换为int数据,比如lexical cast & stoi(),但是都没有成功,所以我想知道是否有另一种方法,比如在不改变数据的情况下对数据进行排序类型。

迈克猫

您可以指定一个比较函数,该函数返回第一个参数是否“小于”该std::sort函数的第二个参数。

在测试时,我发现一些空字符串,使std::stoithrowstd::invalid_argument被推入向量中(它看起来像 by vect.resize(i+1);)。因此,我添加了一些代码来检测错误并将无效字符串评估为小于任何有效整数。

sort(vect.begin(), vect.end(), [](const string& a, const string& b) {
    bool aError = false, bError = false;
    int aInt = 0, bInt = 0;
    try {
        aInt = stoi(a);
    } catch (invalid_argument&) {
        aError = true;
    }
    try {
        bInt = stoi(b);
    } catch (invalid_argument&) {
        bError = true;
    }
    if (aError && !bError) return true;
    if (bError) return false;
    return aInt < bInt;
});
#include <stdexcept>

应添加使用std::invalid_argument

参考:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Objective-c 按正确顺序使用字母数字字符串对 NSArray 进行排序

如果字符串包含数字和字符,如何按数字对字符串数组进行排序 C++

如何在C ++中从对数字进行排序到按字母顺序进行排序

C#UWP如何键入数字字符串并按顺序实时获取逗号?

在C ++中按字典顺序对字符串进行排序

按字母顺序对C字符串数组进行排序

C ++:如何在不使用算法库中排序功能的情况下按字母顺序对字符串向量进行排序

在C ++中对数字数组进行排序时是否必须将字符串转换为双精度?

如何按字母顺序对字符串数组进行排序并防止c中的突变

如何在C ++中按字母顺序对标准输入字符串进行排序?

在C ++中使用向量对数字进行排序

使用`sort`对数字字符串进行排序

Java:对数字字符串数组进行排序

按升序对数字进行排序和IList c#

按数字字符串对数组进行排序(Python 3.4.2)

如何使用C ++将数字字符转换为字符串

C++:按数字错误顺序对链表进行排序

如何在javascript中对数字字符串进行排序?

如何拆分数字字符串,然后将其存储在c中的int数组中?

如何在C ++中创建随机的字母数字字符串?

如何在C中对数字数组进行排序

C ++-是否可以按单列对2D字符串向量进行排序?

按字母顺序对字符串数组进行排序 C++

C#按字母顺序对字符串进行排序,后跟出现频率

通过在c中按字母顺序对2d字符串数组进行排序

在C中按字母顺序对字符串和结构进行排序

按字母顺序对字符串数组进行排序 - C++ 11

在 C 中对数字进行排序

如何对字母数字字符串进行排序