mercredi 19 mai 2021

sregex_iterator next and end.. How does this work?

Folks, I am new to C++. So please excuse me for my ignorance here. I am trying to understand the below code that I got online. What does while (next != end) line exactly do? When I printed the address of &next and &end they were always different (even post while loop). I take std::sregex_iterator next and std::sregex_iterator end are two different instances. So how come while (next != end) line thinks they will be equal at some point of time? I looked under the hood. It seems to be regex_iteratorstring::const_iterator. Tried to understand the constructor etc but do not want to list all those I found cause those might not be related. Just to let you know I am from Java world but have been taking courses to understand C++.

Any help is much appreciated. Thanks

string subject("This is a test");
    try {
      std::regex re("\\S+");
      std::sregex_iterator next(subject.begin(), subject.end(), re);
      std::sregex_iterator end;
       
      while (next != end) {     
        std::smatch match = *next;
        std::cout << match.str() << "\n";
        std::cout << match.position() << "\n";
        next++;    
      }
      
    } catch (std::regex_error& e) {
      // Syntax error in the regular expression
    }

Aucun commentaire:

Enregistrer un commentaire