Hello! What methods can be used for sequential reading of characters from a string starting from a certain position. Let the vector contain the following path strings:

data / 2016 / E24G_2016037

data / 2013 / E30G_20130715

data / 2012 / E36G_20120311

data / 2011 / 15G_20111203

How can one read two-digit numbers from them, namely 24, 30, 36 and 15 for further writing to the array? Because each line contains the same symbol "G", near the values ​​of interest, I was only able to search for its position. Any suggestions would be extremely helpful!

Example:

std::vector<string>mydata; void myClass::getNums() { int pos; char nums[32]; for(int i=0; i<(int)mydata.size(); ++i) { pos=mydata[i].rfind("G"); } } 

    1 answer 1

    Let's start with the fact that the digits are not double-digit :)

    If the criterion is that they go before G, well, something like while(isdigit(mydata[i][pos])) --pos; - and you become the first NOT digit followed by the number that interests you. Then you can simply atoi(mydata[i][pos+1]) , you can also read a lot of other options ...

    We need an exact criterion for what to read. Then you can even use regular expressions ...

    • the numbers are still double digits, but at least three digits. It all depends on the base of the number system and the alphabet used. - vp_arth
    • @vp_arth Give an example of a double digit - Harry
    • Suppose x0 in 4-digit numeral system with the alphabet 0,1,x0,x1 - vp_arth
    • @vp_arth In such a system, x0 and x1 are one character. From the fact that you use words instead of numbers - say, четыре - this number will not become six-digit. You are confusing concepts ... A figure is basically one sign - no matter how you write it down. - Harry
    • Ok, I agree. There are no double digits ) - vp_arth