简体   繁体   English

我可以在自己的结构中使用向量排序 function 吗? 总是编译器错误

[英]Can I use vector sort function in my own struct? always compiler error

[Error] 'class std::vector' has no member named 'sort' which sort can use in struct and how to use? [错误] 'class std::vector' 没有名为 'sort' 的成员,可以在结构中使用哪个排序以及如何使用? sorry for my bad english.对不起,我的英语不好。

typedef struct name
{
    int x,y;
}name;

int main()
{
    int n,m,i;
    vector<name> a(10000);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&m==0)
        {
            break;
        }   
        a.clear();
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i].x);
            a[i].y=a[i].x%m;
        }
        a.sort();
        for(i=0;i<n;i++)
        {
            printf("%d\n",a[i].x);
        }
    }
    return 0;
}

vector doesn't have a sort function. vector没有sort function。 Instead, use std::sort , and implement an operator< for your struct.相反,请使用std::sort ,并为您的结构实现operator<

struct name
{
    int x,y;
    bool operator<( const name &other ) const {
        return x < other.x; // modify to whatever sorting conditions you need
    }
};

Then for calling sort, do然后调用排序,做

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

Since std::vector has no sort() member function, you should use std::sort using the third argument as the sorting predicate.由于std::vector没有sort()成员 function,您应该使用std::sort使用第三个参数作为排序谓词。 By doing so, you are not changing the internals of the struct you are sorting by adding an operator < :通过这样做,您不会通过添加operator <来更改要排序的结构的内部struct

#include <algorithm>
#include <vector>
struct name
{
    int x,y;
};

int main()
{
   std::vector<name> a(10000);

   // assuming you want to sort by x-coordinate  
   std::sort(a.begin(), a.end(), [](name& n1, name& n2) { return n1.x < n2.x; });
}

You can also create a separate lambda function:也可以单独创建 lambda function:

#include <algorithm>
#include <vector>
struct name
{
    int x,y;
};

int main()
{
   std::vector<name> a(10000);
   auto comp = [](name& n1, name& n2) { return n1.x < n2.x; };

   // assuming you want to sort by x-coordinate  
   std::sort(a.begin(), a.end(), comp);
}

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

相关问题 用自己的结构对向量排序 - Sort Vector with own Struct 我应该总是使用带有自己索引的一维向量,还是多维向量? - Should I always use a 1D vector with my own indexing, or is a multi-dimensional vector ok? 如何使用一个 function 按结构中的不同变量排序 - How can I use one function to sort by different variables in a struct 我可以为C#创建自己的JIT \\ Interpreter \\ Compiler并在Visual Studio中使用它吗? - Can I make my own JIT\Interpreter\Compiler for C# and use it in Visual Studio? 我如何使用包含向量的结构向量<Mat> - How can i use a Vector of Struct containing vector<Mat> struct Compiler Error向量的C ++迭代器 - C++ iterator for vector of struct Compiler Error 如何在Nant中使用自己的编译器? - How do I use my own compiler with Nant? 如何在我自己的类Vector中使用Move构造函数而不是Move赋值运算符? - How can I use Move constructor instead of Move assignment operator in my own class Vector? 是否应该使用std :: vector +我自己的大小变量? - Should I use std::vector + my own size variable or not? 如何转换我的堆排序程序以使用 C++ 标准容器(例如 std::vector)? - How can I convert my heap sort program to use C++ standard containers (for example std::vector)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM