相关疑难解决方法(0)

用C++对对象矢量进行排序

struct Keyword
{
    std::string keyword;
    int numUses;
};  

bool sortingVector(const Keyword& key1, const Keyword& key2)
{
    return key1.numUses < key2.numUses;
}

sort(topKeywords.begin(), topKeywords.end(), sortingVector);



: no matching function for call to 'sort(std::vector<Keyword>::iterator, std::vector<Keyword>::iterator, <unresolved overloaded function type>)'
        c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:5236:18: note: candidate is: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<Keyword*, std::vector<Keyword> >, _Compare = bool (NewsAggregatorImpl::*)(const Keyword&, const Keyword&)]
Run Code Online (Sandbox Code Playgroud)

为什么这不正确,我的编译器给了我那个错误.
我希望我的功能是全球性的.
谢谢.

c++

0
推荐指数
1
解决办法
8542
查看次数

什么是基于第一对的"第一"排序类型<std :: pair <int,std :: pair <int,int >>>的std :: vector的标准方法

由向量携带的是一对由id索引的对:

<std::pair<int, std::pair<int, int>>>

         496              1,   256

        (message id)   
Run Code Online (Sandbox Code Playgroud)

如您所见,"id"是重复的.为了进一步处理这些数据,对我来说,根据"消息ID"对三元组进行排序对逻辑非常有用.如何使用STL函数执行此操作?

这是一些数据样本:

 15:38:08.307 - (I) ET02 -  For message Id: 496 Tag - value pair: 1 - 256
 15:38:08.307 - (I) ET02 -  For message Id: 496 Tag - value pair: 2 - 27060
 15:38:08.307 - (I) ET02 -  For message Id: 496 Tag - value pair: 3 - 2014-06-16T17:07:00.519
 15:38:08.307 - (I) ET02 -  For message Id: 487 Tag - value pair: 1 - 1044
 15:38:08.307 - (I) ET02 - …
Run Code Online (Sandbox Code Playgroud)

c++ stl

0
推荐指数
1
解决办法
123
查看次数

如何基于第二个字符串对字符串向量的向量进行排序

我有一个字符串向量的向量.这些向量中的第二个字符串是我想要排序的.有没有办法使用std :: sort来做到这一点?

从我可以告诉我应该传入一个struct/class中的函数,但我不确定它应该是什么样子.

c++ sorting vector

0
推荐指数
1
解决办法
628
查看次数

基于vector <bool>的内容对vector vector <vector <bool >>进行排序

我有2000个 vector<vector<bool>>,每个vector<bool>包含200个元素,我将对这个向量矢量进行排序.假设元素vector<bool>是一个二进制数.

原始数据:

vector 1: 1,1,1
vector 2: 1,0,1
vector 3: 0,0,0
vector 4: 1,0,0
Run Code Online (Sandbox Code Playgroud)

排序后:

vector 3: 0,0,0
vector 4: 1,0,0
vector 2: 1,0,1
vector 1: 1,1,1
Run Code Online (Sandbox Code Playgroud)

可以使用sort一个特殊的谓词,但令人惊讶的是,当我在sort没有谓词的情况下调用时,它似乎无论如何都可以工作.

    vector<bool> A = {1, 1, 1};
    vector<bool> B = {1, 0, 1};
    vector<bool> C = {0, 0, 0};
    vector<bool> D = {1, 0, 0};

    vector < vector<bool> > v = {A,B,C,D};

    sort(v.begin(),v.end());
Run Code Online (Sandbox Code Playgroud)

并且顺序为上面的"排序后".

为什么没有特殊的谓词呢?

c++

0
推荐指数
1
解决办法
548
查看次数

标签 统计

c++ ×4

sorting ×1

stl ×1

vector ×1