C++,vector 自定义类型的排序

本文详细介绍了在C++中使用vector容器进行数据排序的方法,包括通过重载运算符实现自定义排序逻辑,以及如何利用标准库中的algorithm函数进行排序操作。

C++,vector的排序

// 通过重载<,>,方便,看起来好看

// 还可以自定义比较函数


// 四个头文件,特别要注意须包含 <functional>,下面才能用less<CValue>()

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>


using namespace std;


class CValue
{
public:
   int index;
   double value;
public:
  CValue(void){index = -1; value = 100;}
  // 重载=,<,>
  void operator = (const CValue& rhs){ this->index = rhs.index;this->value = rhs.value;}
  bool operator < (const CValue& rhs)const{ return this->value < rhs.value;}// 升序
  bool operator > (const CValue& rhs)const{ return this->value > rhs.value;}// 降序
};

void SortMatrixByRow(vector< vector<CValue> >& distMatrix)
{
  int i, j;
  int dist;
  CValue temp;
  int numCluster=7;// 本簇数据的样本个数

  distMatrix.clear();distMatrix.resize(numCluster, vector<CValue>(numCluster));


  for (i = 0; i < numCluster ; i++){
    for (j = i+1; j < numCluster ; j++){
      /*if (i < j)*/{
       dist = i+j;
       temp.index = j;temp.value = dist;
       distMatrix[i][j] = temp;
       distMatrix[j][i] = temp;
     }
    }
  }

  for (i = 0; i < numCluster ; i++){
    // 如果是升序排序,则将greater<CValue>()改为less<CValue>()
    sort(distMatrix[i].begin(), distMatrix[i].end(), greater<CValue>());
  }

}


int main(int argc, char **argv)
{
   vector< vector<CValue> > distMatrix;

   SortMatrixByRow(distMatrix);

   for (int i = 0; i < distMatrix.size() ; i++){
     for (int j = 0; j < distMatrix[i].size() ; j++){
       cout << distMatrix[i][j].value << " ";
     }
     cout << endl;
   }
   return 0;
}


你可以使用sort函数对vector进行自定义排序。以下是一个示例代码,假设你要对一个vector<string>进行排序,按照字符串的长度从小到大排序: ``` #include<iostream> #include<algorithm> #include<vector> #include<string> using namespace std; bool compare(const string& a, const string& b) { return a.length() < b.length(); } int main() { vector<string> names = {"John", "Mary", "Alice", "Tom"}; cout << "Before sorting: "; for(auto& name : names) { cout << name << " "; } cout << endl; sort(names.begin(), names.end(), compare); cout << "After sorting: "; for(auto& name : names) { cout << name << " "; } cout << endl; return 0; } ``` 在这个例子中,我们定义了一个compare函数,它接受两个字符串参数,比较它们的长度并返回比较结果。然后在主函数中,我们调用sort函数,并传入compare作为自定义的比较函数。这样,sort函数会根据compare函数的返回结果对vector进行排序。最后,我们输出排序后的结果。 所以,通过自定义比较函数,你可以根据自己的需求对vector进行排序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【C++vector数组排序](https://blog.youkuaiyun.com/weixin_46308081/article/details/117195502)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值