'no match for ‘operator==’
I am having trouble figuring out what error there is in my code. It is supposed to take the input map and find the certain string in the key. If it finds that string in the key of a pair, it is supposed to add values associated with the key to a vector and find the final sum. I keep getting this error, though.
no match for ‘operator==’ (operand types are ‘const char’ and ‘const std::__cxx11::basic_string<char>’)
And I am unsure where in my code this error would arise. Here is my code below:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <map>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::vector;
using std::map;
int GetPointTotalForStudent(map<string, string> &input, string type_of){
vector<int> temp;
std::transform(input.begin(), input.end(), input.begin(), [type_of, temp](auto a) mutable{
if(std::find(a.first.begin(), a.first.end(), type_of) != a.first.end()){
temp.push_back(std::stoi(a.second));
}
});
int total = std::accumulate(temp.begin(), temp.end(), 0);
return total;
}
int main(){
map<string, string> TP_Map;
string type_of = "Exam";
TP_Map.insert({"Exam 1", "80"});
TP_Map.insert({"Project", "75"});
TP_Map.insert({"Exam 2", "90"});
GetPointTotalForStudent(TP_Map, type_of);
}
Solution 1:[1]
If you have access to c++ 17 you can do something like the following, which preserves your original vision.
return std::transform_reduce(
input.begin(), input.end(), 0, std::plus(), [type_of](auto a) {
if (a.first.find(type_of) != std::string::npos) {
return std::stoi(a.second);
}
return 0;
});
However, I think you might have overcomplicated the design by using std::transform, I personally would probably favor something simpler like:
int total = 0;
for(const auto& kvp : input){
if(kvp.first.find(type_of) != std::string::npos){
total += std::stoi(kvp.second);
}
}
return total;
using std::for_each
int total = 0;
std::for_each(input.begin(), input.end(), [type_of, &total](auto kvp) {
if (kvp.first.find(type_of) != std::string::npos) {
total += std::stoi(kvp.second);
}
});
return total;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
