第5行:字符54:错误:没有用于调用"min(int,std :: __ cxx11 :: basic_string< char> :: size_type)"的匹配函数 [英] Line 5: Char 54: error: no matching function for call to 'min(int, std::__cxx11::basic_string<char>::size_type)'

查看:53
本文介绍了第5行:字符54:错误:没有用于调用"min(int,std :: __ cxx11 :: basic_string< char> :: size_type)"的匹配函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Solution {
public:
    string reverseStr(string s, int k) {
        for (int start = 0; start < s.size(); start += 2 * k) {
            int end = min(start + k - 1, s.size() - 1);
            while (start < end) {
                swap(s[start], s[end]);
                start++;
                end--;
            }
        }
        return s;
    }
};

第5行:字符54:错误:没有用于调用'min(int,std :: __ cxx11 :: basic_string :: size_type)'的匹配函数

Line 5: Char 54: error: no matching function for call to 'min(int, std::__cxx11::basic_string::size_type)'

推荐答案

编译器试图告诉您,问题是 start + k -1 s的类型.size()-1 是不同的.因此,解决此问题的一种方法是将 start k 的类型更改为 std :: size_t :

As the compiler tries to tell you, the issue is that the types of start + k -1 and s.size() - 1 are different. So one way to fix this is to change the types of start and k to std::size_t:

std::string reverseStr(std::string s, std::size_t k) {
    for (std::size_t start = 0; start < s.size(); start += 2 * k) {
        std::size_t end = std::min(start + k - 1, s.size() - 1);
        while (start < end) {
            swap(s[start], s[end]);
            start++;
            end--;
        }
    }
    return s;
}

或者,您也可以将 s.size()-1 强制转换为 int :

Alternatively you can just cast s.size() - 1 to int:

int end = std::min(start + k - 1, static_cast<int>(s.size() - 1));

还有第三种方法来显式指定 std :: min 的模板参数,但是这可能会触发编译器的有符号/无符号/无符号/有符号转换警告:

There is also the third way to explicitly specify the template parameter of std::min, but that might trigger signed-to-unsigned / unsigned-to-signed conversion warnings of your compiler:

int end = std::min<int>(start + k - 1, s.size() - 1);

这篇关于第5行:字符54:错误:没有用于调用"min(int,std :: __ cxx11 :: basic_string&lt; char&gt; :: size_type)"的匹配函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆