对列表中的对象进行排序和组合值[重复]

时间:2022-10-23 20:30:14

This question already has an answer here:

这个问题在这里已有答案:

I have an std::list<object> and object has 6 integer and a char array elements in it. I want to sort these objects with a specific integer (lets say integer a) and afterwards, I want to combine char arrays with the sorted order.

我有一个std :: list ,对象有6个整数和一个char数组元素。我想用一个特定的整数(比如整数a)对这些对象进行排序,之后,我想将char数组与排序顺序组合起来。

Does stl has some kind of method for this specific sorting ? If no, how am i supposed to do for this kind of sorting ?

stl是否有某种特定排序方法?如果不是,我该如何做这种排序呢?

2 个解决方案

#1


std::list::sort has an overload that takes a custom comparator, as does std::sort for more suitable containers. So you can use that for sorting:

std :: list :: sort有一个带有自定义比较器的重载,std :: sort用于更合适的容器。所以你可以用它来排序:

my_list.sort([](object const & o1, object const & o2) {return o1.a < o2.a;});

I'm not sure exactly what you mean by "combine char arrays", but you can iterate over the sorted list with std::for_each, std::transform, or a simple for loop in order to do something with each object in turn.

我不确定你是什么意思“组合字符串数组”,但你可以用std :: for_each,std :: transform或简单的for循环遍历排序列表,以便依次对每个对象做一些事情。

#2


You can provide your own comparator to the sort function or implement operator< for your object class. You can find more information here. Be aware to use std::list::sort rather then std::sort.

您可以为sort函数提供自己的比较器,或者为对象类实现operator <。您可以在这里找到更多信息。请注意使用std :: list :: sort而不是std :: sort。

// list::sort
#include <iostream>
#include <list>
#include <string>
#include <cctype>

// comparison, not case sensitive.
bool compare_nocase (const std::string& first, const std::string& second)
{
    unsigned int i=0;
    while ( (i<first.length()) && (i<second.length()) )
    {
        if (tolower(first[i])<tolower(second[i])) return true;
            else if (tolower(first[i])>tolower(second[i])) return false;
        ++i;
    }
    return ( first.length() < second.length() );
}

int main ()
{
    std::list<std::string> mylist;
    std::list<std::string>::iterator it;
    mylist.push_back ("one");
    mylist.push_back ("two");
    mylist.push_back ("Three");

    mylist.sort();

    std::cout << "mylist contains:";
    for (it=mylist.begin(); it!=mylist.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    mylist.sort(compare_nocase);

    std::cout << "mylist contains:";
    for (it=mylist.begin(); it!=mylist.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

  return 0;
}

#1


std::list::sort has an overload that takes a custom comparator, as does std::sort for more suitable containers. So you can use that for sorting:

std :: list :: sort有一个带有自定义比较器的重载,std :: sort用于更合适的容器。所以你可以用它来排序:

my_list.sort([](object const & o1, object const & o2) {return o1.a < o2.a;});

I'm not sure exactly what you mean by "combine char arrays", but you can iterate over the sorted list with std::for_each, std::transform, or a simple for loop in order to do something with each object in turn.

我不确定你是什么意思“组合字符串数组”,但你可以用std :: for_each,std :: transform或简单的for循环遍历排序列表,以便依次对每个对象做一些事情。

#2


You can provide your own comparator to the sort function or implement operator< for your object class. You can find more information here. Be aware to use std::list::sort rather then std::sort.

您可以为sort函数提供自己的比较器,或者为对象类实现operator <。您可以在这里找到更多信息。请注意使用std :: list :: sort而不是std :: sort。

// list::sort
#include <iostream>
#include <list>
#include <string>
#include <cctype>

// comparison, not case sensitive.
bool compare_nocase (const std::string& first, const std::string& second)
{
    unsigned int i=0;
    while ( (i<first.length()) && (i<second.length()) )
    {
        if (tolower(first[i])<tolower(second[i])) return true;
            else if (tolower(first[i])>tolower(second[i])) return false;
        ++i;
    }
    return ( first.length() < second.length() );
}

int main ()
{
    std::list<std::string> mylist;
    std::list<std::string>::iterator it;
    mylist.push_back ("one");
    mylist.push_back ("two");
    mylist.push_back ("Three");

    mylist.sort();

    std::cout << "mylist contains:";
    for (it=mylist.begin(); it!=mylist.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    mylist.sort(compare_nocase);

    std::cout << "mylist contains:";
    for (it=mylist.begin(); it!=mylist.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

  return 0;
}

相关文章