4
"54 232 65 12"와 같은 문자열에서 다음 정수를 추출하는 가장 간단한 방법은 무엇입니까?sstream C++없이 문자열에서 정수 추출
그리고 마지막 숫자가 long long int이면? 가능 sstream
"54 232 65 12"와 같은 문자열에서 다음 정수를 추출하는 가장 간단한 방법은 무엇입니까?sstream C++없이 문자열에서 정수 추출
그리고 마지막 숫자가 long long int이면? 가능 sstream
없이이 작업을 수행 할 수 있나요이 시도 : C++ 11은 또한 unsigned long long int
반환 std::strtoull
을 사용할 수 있습니다에서
#include <cstdlib>
#include <cstdio>
#include <cerrno>
#include <cstring>
int main()
{
char str[] = " 2 365 2344 1234444444444444444444567 43";
for (char * e = str; *e != '\0';)
{
errno = 0;
char const * s = e;
unsigned long int n = strtoul(s, &e, 0);
if (errno) // conversion error (e.g. overflow)
{
std::printf("Error (%s) encountered converting:%.*s.\n",
std::strerror(errno), e - s, s);
continue;
}
if (e == s) { ++e; continue; } // skip inconvertible chars
s = e;
printf("We read: %lu\n", n);
}
}
합니다.
(Live example.)
반복 strtoul''적용하고 당신을 제공하는 최종 포인터를 사용합니다. –
'sstream' 대신'sprintf'를 사용하십시오. – sgarizvi
올바른'C++'방법은 sstream입니다. – Dariusz