main.cpp:26:18: error: no matching function for call to ‘max(_gnucxx::_alloctraits,…
- User
main.cpp:26:18: error: no matching function for call to ‘max(__gnu_cxx::__alloc_traits, int>::value_type&, __gnu_cxx::__alloc_traits, int>::value_type&, __gnu_cxx::__alloc_traits, int>::value_type&, __gnu_cxx::__alloc_traits, int>::value_type&)’ 26 | mx = std::max(gens[0], gens[1], gens[2], gens[3]); | ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/11/bits/char_traits.h:39, from /usr/include/c++/11/ios:40, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from main.cpp:1: /usr/include/c++/11/bits/stl_algobase.h:254:5: note: candidate: ‘template constexpr const _Tp& std::max(const _Tp&, const _Tp&)’ 254 | max(const _Tp& __a, const _Tp& __b) | ^~~
- Assistant
The error message indicates that there is no matching function for the
std::maxcall in your code. The available candidate is the overloadedstd::maxfunction that takes two arguments.It seems like you are trying to pass four arguments (
gens[0], gens[1], gens[2], gens[3]) tostd::max, which is causing the error.To fix this issue, you need to modify your usage of
std::maxto only pass two arguments at a time. If you want to find the maximum value among the four elements, you can callstd::maxmultiple times, comparing two elements at a time.Here's an example of how you can modify your code:
mx = std::max(std::max(gens[0], gens[1]), std::max(gens[2], gens[3]));This will first compare
gens[0]andgens[1], then compare the maximum of those two withgens[2], and finally compare the maximum of the three withgens[3]. - User
#include #include #include
int main() { std::vector gens(5, 0); //0 -> A, 1 -> C, 2 -> G, //3 -> T, 4 -> ? int n, mx; std::string step; std::cin >> n; std::cin >> step; for(char c: step) { if (c == 'A') gens[0] += 1; else if (c == 'C') gens[1] += 1; else if (c == 'G') gens[2] += 1; else if (c == 'T') gens[3] += 1; else if (c == '?') gens[4] += 1; } mx = std::max(std::max(gens[0], gens[1]), std::max(gens[2], gens[3])); if (4*mx - gens[0] - gens[1] - gens[2] - gens[3] > gens[4]){ std::cout << "==="; return 0; } for (int i=0; i<n; i++){ if (step[i] == "?") { if (gens[0] > 0) { step[i] = "A"; gens[0] -= 1; } else if (gens[1] > 0) { step[i] = "C"; gens[1] -= 1; } else if (gens[2] > 0) { step[i] = "G"; gens[2] -= 1; } else if (gens[3] > 0) { step[i] = "T"; gens[3] -= 1; } else break; }
} std::cout << step; return 0;}
не правильное изменение символов с ртроке, как изменить
- Assistant
In your code, you are trying to modify characters in a string by assigning string literals ("A", "C", "G", "T") to individual characters in the string. However, in C++, you cannot assign a string literal to a char directly.
To fix this issue, you need to use single quotes ('A', 'C', 'G', 'T') instead of double quotes for character literals.
Here's the corrected code:
#include <iostream> #include <vector> #include <string> int main() { std::vector<int> gens(5, 0); int n; std::string step; std::cin >> n; std::cin >> step; for (char c : step) { if (c == 'A') gens[0] += 1; else if (c == 'C') gens[1] += 1; else if (c == 'G') gens[2] += 1; else if (c == 'T') gens[3] += 1; else if (c == '?') gens[4] += 1; } int mx = std::max(std::max(gens[0], gens[1]), std::max(gens[2], gens[3])); if (4 * mx - gens[0] - gens[1] - gens[2] - gens[3] > gens[4]) { std::cout << "==="; return 0; } for (int i = 0; i < n; i++) { if (step[i] == '?') { if (gens[0] > 0) { step[i] = 'A'; gens[0] -= 1; } else if (gens[1] > 0) { step[i] = 'C'; gens[1] -= 1; } else if (gens[2] > 0) { step[i] = 'G'; gens[2] -= 1; } else if (gens[3] > 0) { step[i] = 'T'; gens[3] -= 1; } else break; } } std::cout << step; return 0; }