How to iterate over a string by obtaining the indices of each character in C++?

Asked

Viewed 46 times

1

Example done in Python:

editor = 'Luiz'

for contador, letra in enumerate(editor):
   print(f"A letra '{letra}' está no índice {contador} ")

I was wondering if it’s possible to do this kind of counter inside the foreach in C++, as in the above code in Python.

I noticed that in C++ there is a function called enum, which is very similar to the enumerate Python, but I don’t know if it does the same thing, I just compared it because I found the names of the two functions very similar. It does?

But anyway, is or is not able to put an accountant within the foreach in C++?

Example of foreach in C++:

#include <iostream>
using namespace std;

int main() {
   string editor = "Luiz";
   for (auto l : editor) cout << "Letra: " << l << endl;
   
   return 0;
}

2 answers

2

enum is not a function. See documentation his.

The simplest way to do this is to add an external counter to keep track, it’s not the most beautiful solution, but it works:

#include <iostream>
using namespace std;

int main() {
   string editor = "Luiz";
   int i = 0;
   for (auto l : editor) cout << "Letra: " << l << " Índice: " << i++ << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Or you can use the for normal that you already know from the previous questions, this is a case that it can be more useful, there are different ways precisely to meet each situation, do not need to always use the same, this case I would use so:

#include <iostream>
using namespace std;

int main() {
   string editor = "Luiz";
   for (int i = 0; i < editor.length(); i++) cout << "Letra: " << editor[i] << " Índice: " << i << endl;
}

It is possible to use other forms as well, even joining the element information with the index, as Python does, but it is more complex and less efficient, Python does so because it does not have the for standard (it even has a simple tie but the staff thinks it’s sacrilegious to use). Make the simple.

1

The enumerate is required in Python since the language does not have the for "traditional", where the programmer creates a counter variable (usually i) and increases it with each iteration. Because of this, if you need the indexes, it is common to use the function in question.

In C++, the option that seems to me more trivial to do with the code you already have is to maintain an external counter and increase it with each iteration. Something like that:

#include <iostream>
using namespace std;
 
int main() {
   string editor = "Luiz";
 
   int i = 0;
   for (auto l : editor) cout << "Letra: " << l << " Índice: " << i++ << endl;
 
   return 0;
}

See it working on Ideone.

The operator in the expression i++ is called postfix increment Operator. It basically returns the value of its operand and then increments it. Learn more.

You can see other alternatives in this question in Soen. However, all the options shown to me seem to end up complicating unnecessarily.


About the enum referred to in the question, this is not a function, but rather a language resource for the definition of enumerations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.