std::stoi, std::stol, std::stoll
| 헤더 <string>
|
||
int stoi ( const std::string& str,
std::size_t* pos = nullptr, int base = 10 );
|
에 정의됨 (1) | (C++11부터) |
int stoi ( const std::wstring& str,
std::size_t* pos = nullptr, int base = 10 );
|
(2) | (C++11부터) |
long stol ( const std::string& str,
std::size_t* pos = nullptr, int base = 10 );
|
(3) | (C++11부터) |
long stol ( const std::wstring& str,
std::size_t* pos = nullptr, int base = 10 );
|
(4) | (C++11부터) |
long long stoll( const std::string& str,
std::size_t* pos = nullptr, int base = 10 );
|
(5) | (C++11부터) |
long long stoll( const std::wstring& str,
std::size_t* pos = nullptr, int base = 10 );
|
(6) | (C++11부터) |
문자열 str에서 부호 있는 정수 값을 해석합니다.
내부(변환 함수에 대한) 포인터 ptr를 유형 char* (1,3,5) 또는 wchar_t* (2,4,6)이라고 합시다.
std::strtol(str.c_str(), &ptr, base)을 호출합니다.std::wcstol(str.c_str(), &ptr, base)을 호출합니다.std::strtol(str.c_str(), &ptr, base)을 호출합니다.std::wcstol(str.c_str(), &ptr, base)을 호출합니다.std::strtoll(str.c_str(), &ptr, base)을 호출합니다.std::wcstoll(str.c_str(), &ptr, base)을 호출합니다.첫 번째 공백이 아닌 문자가 발견될 때까지 std::isspace 호출로 식별되는 모든 공백 문자를 버린 다음, 유효한 base-n(여기서 n=) 정수 숫자 표현을 형성하기 위해 가능한 한 많은 문자를 가져와 정수 값으로 변환합니다. 유효한 정수 값은 다음 부분으로 구성됩니다.
base
- (선택 사항) 더하기 또는 빼기 부호
- (선택 사항) 접두사 (
0) 8진수 밑을 나타냄 (밑이8또는0인 경우에만 적용) - (선택 사항) 접두사 (
0x또는0X) 16진수 밑을 나타냄 (밑이16또는0인 경우에만 적용) - 일련의 숫자
밑에 대한 유효한 값 집합은 {0, 2, 3, ..., 36}입니다. 밑-2 정수에 대한 유효한 숫자 집합은 {0, 1}이고, 밑-3 정수에 대해서는 {0, 1, 2}입니다. 10보다 큰 밑의 경우 유효한 숫자에는 알파벳 문자가 포함되며, 밑-Aa 정수의 경우 11에서 시작하여 밑-Zz 정수의 경우 36까지입니다. 문자의 대소문자는 무시됩니다.
현재 설치된 C 로케일에서 추가 숫자 형식이 허용될 수 있습니다.
의 값이 base0인 경우 숫자 밑이 자동으로 감지됩니다. 접두사가 0이면 밑은 8진수이고, 접두사가 0x 또는 0X이면 밑은 16진수이며, 그렇지 않으면 밑은 10진수입니다.
입력 시퀀스의 일부로 빼기 부호가 있었다면, 숫자 시퀀스에서 계산된 숫자 값은 결과 유형에서 단항 빼기를 통해 부호가 반전됩니다.
pos이 널 포인터가 아니면 ptr은 str.c_str()에서 변환되지 않은 첫 번째 문자의 주소를 받고, 해당 문자의 인덱스가 계산되어 *pos에 저장되며, 변환으로 처리된 문자 수를 제공합니다.
매개변수
| str | - | 변환할 문자열 |
| pos | - | 처리된 문자 수를 저장할 정수의 주소 |
| base | - | 숫자 진법 |
반환값
str 의 내용에 해당하는 정수 값.
예외
- std::invalid_argument 변환이 수행될 수 없는 경우.
- std::out_of_range 변환된 값이 결과 타입의 범위를 벗어나거나 기본 함수( std::strtol 또는 std::strtoll )가 errno 를 ERANGE 로 설정하는 경우.
예제
#include <iomanip> #include <iostream> #include <stdexcept> #include <string> #include <utility> int main() { const auto data = { "45", "+45", " -45", "3.14159", "31337 with words", "words and 2", "12345678901", }; for (const std::string s : data) { std::size_t pos{}; try { std::cout << "std::stoi(" << std::quoted(s) << "): "; const int i{std::stoi(s, &pos)}; std::cout << i << "; pos: " << pos << '\n'; } catch (std::invalid_argument const& ex) { std::cout << "std::invalid_argument::what(): " << ex.what() << '\n'; } catch (std::out_of_range const& ex) { std::cout << "std::out_of_range::what(): " << ex.what() << '\n'; const long long ll{std::stoll(s, &pos)}; std::cout << "std::stoll(" << std::quoted(s) << "): " << ll << "; pos: " << pos << '\n'; } } std::cout << "\nCalling with different radixes:\n"; for (const auto& [s, base] : {std::pair<const char*, int> {"11", 2}, {"22", 3}, {"33", 4}, {"77", 8}, {"99", 10}, {"FF", 16}, {"jJ", 20}, {"Zz", 36}}) { const int i{std::stoi(s, nullptr, base)}; std::cout << "std::stoi(" << std::quoted(s) << ", nullptr, " << base << "): " << i << '\n'; } }
가능한 출력:
std::stoi("45"): 45; pos: 2
std::stoi("+45"): 45; pos: 3
std::stoi(" -45"): -45; pos: 4
std::stoi("3.14159"): 3; pos: 1
std::stoi("31337 with words"): 31337; pos: 5
std::stoi("words and 2"): std::invalid_argument::what(): stoi
std::stoi("12345678901"): std::out_of_range::what(): stoi
std::stoll("12345678901"): 12345678901; pos: 11
Calling with different radixes:
std::stoi("11", nullptr, 2): 3
std::stoi("22", nullptr, 3): 8
std::stoi("33", nullptr, 4): 15
std::stoi("77", nullptr, 8): 63
std::stoi("99", nullptr, 10): 99
std::stoi("FF", nullptr, 16): 255
std::stoi("jJ", nullptr, 20): 399
std::stoi("Zz", nullptr, 36): 1295
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2009 | C++11 |
std::out_of_range
예외가 발생하지 않음 -
std::strtol 또는 std::strtoll 이 errno 를 ERANGE 로 설정하는 경우 |
예외 발생 |
참고 항목
|
(C++11)
(C++11)
|
문자열을 부호 없는 정수로 변환
(함수) |
|
(C++11)
(C++11)
(C++11)
|
문자열을 부동 소수점 값으로 변환
(함수) |
|
(C++11)
|
바이트 문자열을 정수 값으로 변환
(함수) |
|
(C++11)
|
바이트 문자열을 부호 없는 정수 값으로 변환
(함수) |
|
(C++11)
(C++11)
|
바이트 문자열을
std::intmax_t
또는
std::uintmax_t
로 변환
(함수) |
|
(C++17)
|
문자 시퀀스를 정수 또는 부동 소수점 값으로 변환
(함수) |
|
(C++11)
|
바이트 문자열을 정수 값으로 변환
(함수) |
|
(C++11)
|
정수 또는 부동 소수점 값을
string
으로 변환
(함수) |
|
(C++11)
|
정수 또는 부동 소수점 값을
wstring
으로 변환
(함수) |