Sorting a vector of objects in C++

后端 未结 8 1342
慢半拍i
慢半拍i 2021-01-28 13:26
struct Keyword
{
    std::string keyword;
    int numUses;
};  

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


        
8条回答
  •  不要未来只要你来
    2021-01-28 13:42

    #include 
    #include 
    #include 
    
    struct Keyword
    {
        std::string keyword;
        int numUses;
    };  
    
    bool sortingVector(const Keyword& key1, const Keyword& key2)
    {
        return key1.numUses < key2.numUses;
    }
    
    int main()
    {
        std::vector topKeywords(100);
    
        // imagine topKeywords initialization here
    
        std::sort(topKeywords.begin(), topKeywords.end(), sortingVector);
    
        return 0;
    }
    

    Compiles fine on my machine (gcc version 4.4.3).

提交回复
热议问题