atoi 함수를 사용하는 방법에 대한 간단한 예제를 제공해 줄 수 있습니까? 나는 그것이 어떻게 작동해야하는지 알고 있지만, 대부분의 예제는 객관적인 C에 있습니다 ... 나는 아직 그것을 배웠던 이후로 읽는 데 어려움이 있습니다. 감사합니다. 미리 알려 주시기 바랍니다. 이 마지막 경우의 동작은 (아마 그래서 일부 구현은 루프 부호있는 정수 오버 플로우에 대한 검사 시간을 소비하지 않고 10 배 이전 값으로 다음 숫자를 추가 할 수 있습니다) 정의되지 않기 때문에C++의 "atoi"함수의 간단한 예제
0
A
답변
3
#include <cstdlib> // wraps stdlib.h in std, fixes any non-Standard content
std::string t1("234");
int i1 = std::atoi(t1.c_str());
int i1b = std::stoi(t1); // alternative, but throws on failure
const char t2[] = "123";
int i2 = std::atoi(t2);
const char* t3 = "-93.2"; // parsing stops after +/- and digits
int i3 = std::atoi(t3); // i3 == -93
const char* t4 = "-9E2"; // "E" notation only supported in floats
int i4 = std::atoi(t4); // i4 == -9
const char* t5 = "-9 2"; // parsing stops after +/- and digits
int i5 = std::atoi(t5); // i5 == -9
const char* t6 = "ABC"; // can't convert any part of text
int i6 = std::atoi(t6); // i6 == 0 (whenever conversion fails completely)
const char* t7 = "9823745982374987239457823987"; // too big for int
int i7 = std::atoi(t7); // i7 is undefined
, std::stoi
또는 std::strtol
대신 사용하는 것이 좋습니다 .
다음을 참조하십시오 : 예제가 포함 된 문서는 cppreference atoi; stoi
Google ....? http://en.cppreference.com/w/cpp/string/byte/atoi – yizzlez
'atoi'는 값이 'int'로 표현 될 수없는 경우 정의되지 않은 동작을 일으키는 것에주의하십시오. 'strtol'과'strtoul' 함수는 잘 정의 된 동작과 오류 복구 기능을 가지고 있습니다; 물론이 작업을 위해 설계된 C++ 문자열 스트림을 사용할 수도 있습니다. –