How can I build a std::vector and then sort them?

前端 未结 7 2145
一向
一向 2021-02-06 22:09

I have a bunch of strings that I need to sort. I think a std::vector would be the easiest way to do this. However, I\'ve never used vectors before and so would like some help.

7条回答
  •  自闭症患者
    2021-02-06 22:45

    Try using comaprator:

     #include 
     #include 
     #include 
     #include 
     #include 
     using namespace std;
    
    //comparing function only sorts if string size is equal and keeps the larger integgers at last.
    bool myfunction (string i,string j) 
    { 
    int n=i.length();
    int m=j.length();
    if(n==m)
        return (i>n;
    vector  arr(n);
    for(int i=0;i>arr[i];
    
    
    sort(arr.begin(),arr.end(),myfunction);
    
    for(int i=0;i

提交回复
热议问题