If I have a pointer to a string in the middle of which contains a number. I can translate to a number like this:

wchar_t* begin = L"A12A"; begin++; wchar_t* end; unsigned long n = ::wcstoul(begin, &end, 10); 

And how can I do this if the number is contained in std::wstring and there is an iterator at the beginning of the number:

 std::wstring wstr(L"A12B") std::wstring::iterator it = wstr.begin(); it++; //Получаем число 

And at the output, the iterator should point to B;

    2 answers 2

    If you only have an iterator at hand and there is no parent (to the iterator) object (string), then you can do it like this

     wchar_t* itAddress = &(*it); unsigned long n = ::wcstoul(itAddress, L'\0', 10); 

    Those. through an iterator we get a pointer to the data that is stored in the row (the iterator for the string itself is a pointer itself, in fact, you just need to convert it correctly)

    If you have a parent string, you can do this:

     unsigned long n = ::wcstoul(wstr.c_str() + 1, L'\0', 10); 

    Those. immediately legally (by string) we get a pointer to the data stored in the string

    PS

    Slightly corrected the code to determine where the wcstoul function finished

     wchar_t* itAddress = &(*it); wchar_t* itAddressEnd = 0; unsigned long n = ::wcstoul(itAddress + 1, &itAddressEnd, 10); std::wstring::iterator itNew = it + ((long long)itAddressEnd - (long long)itAddress - 2); 

    -2 because 1) started with the symbol it + 1 - it gave -1 , 2) finished on the previous character - it gave -1 more

    • Yes, how to get a pointer through an iterator is understandable, but I would like to do so that the iterator would point to the place where the parsing of the string ended, as wcstoul does, because the parsing of the remaining characters should go on. - Rikitikitavi
    • Anton, my joint, I set foot with the second parameter, added to PS above how to do it in order to get an iterator to the next part of the text - Zhihar

    Only a solution like this comes to mind

     #include <iostream> #include <string> using namespace std; int main() { wstring wstr(L"A1298BAAC"); wstring::iterator it = wstr.begin(); it++; wchar_t* result_it; auto x = wcstoul(&*it, &result_it, 10); wstring::iterator it2 = wstr.begin(); while(it2 != wstr.end() && &*it2++ != result_it){ } int pos = std::distance(wstr.begin(), it2) - 1; std::wcout << x << endl << result_it << endl << pos << endl << wstr.substr(pos); } 

    And conclusion

     1298 BAAC 5 BAAC