症状
AtCoder Beginner Contest 397のC問題を実施していた時に以下のエラーメッセージが表示されました。 エラーメッセージを翻訳すると「エラー: ‘max(int&, std::map::size_type)’ の呼び出しに一致する関数がありません」でしたerror
Main.cpp: In function ‘int main()’:
Main.cpp:33:14: error: no matching function for call to ‘max(int&, std::map<int, int>::size_type)’
33 | ans = max(ans,before.size() + after.size());
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/12/bits/specfun.h:45,
from /usr/include/c++/12/cmath:1935,
from /usr/include/x86_64-linux-gnu/c++/12/bits/stdc++.h:41,
from Main.cpp:1:
/usr/include/c++/12/bits/stl_algobase.h:254:5: note: candidate: ‘template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)’
254 | max(const _Tp& __a, const _Tp& __b)
| ^~~
/usr/include/c++/12/bits/stl_algobase.h:254:5: note: template argument deduction/substitution failed:
Main.cpp:33:14: note: deduced conflicting types for parameter ‘const _Tp’ (‘int’ and ‘std::map<int, int>::size_type’ {aka ‘long unsigned int’})
33 | ans = max(ans,before.size() + after.size());
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/12/bits/stl_algobase.h:300:5: note: candidate: ‘template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)’
300 | max(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
/usr/include/c++/12/bits/stl_algobase.h:300:5: note: template argument deduction/substitution failed:
Main.cpp:33:14: note: deduced conflicting types for parameter ‘const _Tp’ (‘int’ and ‘std::map<int, int>::size_type’ {aka ‘long unsigned int’})
33 | ans = max(ans,before.size() + after.size());
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/12/algorithm:61,
from /usr/include/x86_64-linux-gnu/c++/12/bits/stdc++.h:65:
/usr/include/c++/12/bits/stl_algo.h:5746:5: note: candidate: ‘template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)’
5746 | max(initializer_list<_Tp> __l)
| ^~~
/usr/include/c++/12/bits/stl_algo.h:5746:5: note: template argument deduction/substitution failed:
Main.cpp:33:14: note: mismatched types ‘std::initializer_list<_Tp>’ and ‘int’
33 | ans = max(ans,before.size() + after.size());
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/12/bits/stl_algo.h:5756:5: note: candidate: ‘template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)’
5756 | max(initializer_list<_Tp> __l, _Compare __comp)
| ^~~
/usr/include/c++/12/bits/stl_algo.h:5756:5: note: template argument deduction/substitution failed:
Main.cpp:33:14: note: mismatched types ‘std::initializer_list<_Tp>’ and ‘int’
33 | ans = max(ans,before.size() + after.size());
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
code
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i < (n);i++)
int main() {
int ans = 0;
int n = 0;
cin >> n;
vector<int> a(n);
for(auto& x :a)cin >> x;
// 分割ロジック
for(int i = 1;i < n-1;i++){
map<int,int> before;
map<int,int> after;
// beforeCountロジック
for(int j = 0;j < i;j++){
if(!(before.count(a.at(j)))){
before[a.at(j)] = 1;
}
}
// afterCountロジック
for(int j = i;j < n-1;j++){
if(!(after.count(a.at(j)))){
after[a.at(j)] = 1;
}
}
//最大値を設定する
ans = max(ans,before.size() + after.size());
}
cout << ans << endl;
return 0;
}
解決策
maxでの引数がintで揃っていなかったため、エラーが表示されていたようなので、intの変数を作ってそこにmap.size()を入れることで、解決しました(なお、ロジックはおかしい模様)code
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i < (n);i++)
int main() {
int ans = 0;
int n = 0;
cin >> n;
vector<int> a(n);
for(auto& x :a)cin >> x;
// 分割ロジック
for(int i = 1;i < n-1;i++){
map<int,int> before;
map<int,int> after;
// beforeCountロジック
for(int j = 0;j < i;j++){
if(!(before.count(a.at(j)))){
before[a.at(j)] = 1;
}
}
// afterCountロジック
for(int j = i;j < n-1;j++){
if(!(after.count(a.at(j)))){
after[a.at(j)] = 1;
}
}
//ここに2行追加s
int beforeSize = before.size();
int afterSize = after.size();
//最大値を設定する
ans = max(ans,beforeSize + afterSize);
}
cout << ans << endl;
return 0;
}
参考