0

Let's say we have a for loop:

for(char &ch : text){
    if(ch == 'a')
        std::cout << "Found 'a' at position: " << {a.index} << std::endl;
}

Is there an easy way to get {a.index}?

Of course I know I could do something like this:

for(int i = 0; text[i] != '\0'; i++){
    if(text[i] == 'a')
        std::cout << "Found 'a' at position: " << i << std::endl;
}

or

i = 0;
for(char &ch : text){
    if(ch == 'a')
        std::cout << "Found 'a' at position: " << i << std::endl;
    i++;
}

But I'm asking, because of educational purpose if there is some built-in way to do this.

przekaska
  • 35
  • 5

0 Answers0