developer tip

std :: stou가없는 이유는 무엇입니까?

copycodes 2020. 9. 10. 08:00
반응형

std :: stou가없는 이유는 무엇입니까?


C ++ 11은 몇 가지 새로운 문자열 변환 함수를 추가했습니다.

http://en.cppreference.com/w/cpp/string/basic_string/stoul

여기에는 stoi (string to int), stol (string to long), stoll (string to long long), stoul (string to unsigned long), stoull (string to unsigned long long)이 포함됩니다. 그 부재에서 주목할만한 것은 stou (문자열에서 부호없는) 함수입니다. 필요하지 않지만 다른 모든 이유가 있습니까?

관련 : C ++ 11에 "sto {short, unsigned short}"함수가 없습니까?


가장 확실한 대답은 C 라이브러리에 해당하는 " strtou" 가없고 C ++ 11 문자열 함수는 모두 C 라이브러리 함수를 둘러싼 얇게 베일 된 래퍼입니다. std::sto*함수는 미러 strto*이고 함수는를 std::to_string사용 sprintf합니다.


편집 :로 KennyTM는 지적, 모두 stoistol사용 strtol이 존재하는 동안 왜 기본 변환 기능으로,하지만 여전히 신비 stoul그 용도를 strtoul, 해당하는 없습니다 stou.


stoi존재 하는지 모르겠지만와 가설 stou의 유일한 차이점 은 결과가 다음 범위에 있는지 확인하는 것입니다 .stoulstouunsigned

unsigned stou(std::string const & str, size_t * idx = 0, int base = 10) {
    unsigned long result = std::stoul(str, idx, base);
    if (result > std::numeric_limits<unsigned>::max()) {
        throw std::out_of_range("stou");
    }
    return result;
}

(마찬가지로 는 다른 범위 검사 stoi를 사용 하는와 유사 stol하지만 이미 존재하므로 정확히 구현하는 방법에 대해 걱정할 필요가 없습니다.)

참고 URL : https://stackoverflow.com/questions/8715213/why-is-there-no-stdstou

반응형