我实现谓词的方法有什么问题

What is wrong with my method of implementing a predicate?

本文关键字:什么 问题 方法 实现 谓词      更新时间:2023-10-16
vector<pair<string, long> > probLabel;
max_element(probLabel.begin(), probLabel.end(), vectPairMax)->first

Problabel包含一个标签和一个数字。我正在尝试获取Problabel中最大的数字并返回标签。因此,我正在使用max_element并写下谓词功能。

bool vectPairMax(const pair<string, int> &lhs, const pair<string, int> &rhs) {
 if (lhs.second < rhs.second) {
    return true;
 } else {
    return false;
 }
}

这是我正在写的函数还是函数指针?无论哪种情况,我都看不到这是什么错误。也许如果是功能指针,我需要解释它吗?通常,任何建议都将不胜感激

这是错误消息:

架构的未定义符号x86_64: " probcalc(dataAllfive&amp;,__ 1 :: map,std :: __ 1 :: __ 1 :: sallocator>,std :: __ 1 :: basic__string,std,std :: __ 1 :: allocator>,std :: std :: __ __ 1 :: __ 1 :: Sily,std :: Sill,std :: SERD :: SERT :: SERD :: STD :: SERT :: STD :: STD :: STD :: STD :: STD::__ 1 :: salcator>>,std :: __ 1 :: Aralocator,std :: __ 1 :: Aralocator> const,std :: __ 1 :: basic__string,std :: __ 1 :: allocator>:sallocator>>>>>>>>>>>&amp; std:,, std:,,:__ 1 :: Set,std :: __ 1 :: Arlocator>,std :: __ 1 :: Less,std :: __ 1 :: Arlocator>>,std :: __ 1 :: Aralocator,std :: __ 1 :: Arlocator>:>)",从: predictionTest(csvstream&amp;,dataAllfive&amp;)LD:符号(s)架构x86_64找不到符号clang:错误:链接器命令因出口代码1失败(使用-V查看调用)

完成凝结代码

string ProbCalc(Data &trainData, map<string, string> &tempMap2, set<string> &uniqueWords) {
   vector<pair<string, int> > probLabel; // Vector of pairs of labels and their probability.    
   // Iterate through all known posts in training data
   for (auto foo : trainData.postNum) {
      probLabel.push_back(make_pair(foo, 0));
   }
   // Label
   string mostProbPost = max_element(probLabel.begin(), probLabel.end(), vectPairMax)->first;
   return mostProbPost;
}

// Predicate for max_element function
bool vectPairMax(const pair<string, int> &lhs, const pair<string, int> &rhs) {
   if (lhs.second < rhs.second) {
    return true;
   } else {
    return false;
   }
}

谓词没有错。这可以很好地编译:

#include <vector>
#include <utility>
#include <string>
#include <algorithm>
bool vectPairMax(const std::pair<std::string, int> &lhs, const std::pair<std::string, int> &rhs) 
{
if (lhs.second < rhs.second) {
    return true;
} else {
    return false;
}
}
int main()
{
    std::vector<std::pair<std::string, int> > probLabel;
    std::string mostProbPost = max_element(probLabel.begin(), probLabel.end(), vectPairMax)->first;
}