8

I am looking for something similar to traversing a string in Python:

i.e.

for char in str:
    do something

How can I do this in C++?

Thank you

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
Quester
  • 1,969
  • 4
  • 15
  • 13

3 Answers3

25

if str is std::string, or some other standard container of char, then that's

for (char c : str) {
    // do something
}

If you want to modify the characters of the string, then you'll want a reference char & c rather than a value char c.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I'm pretty sure that this is what he's looking for, but... In Python, his `char` would have type string, and in his `do something`, he could do string like operations on it. (On the other hand, he couldn't modify the character in `str`, because `char` would be a one character long substring of `std`.) – James Kanze Oct 29 '14 at 11:24
  • OP should specify what he wants otherwise the answers aren't precise enough – Marco A. Oct 29 '14 at 11:28
  • @JamesKanze: Indeed, I'm not trying to exactly emulate Python semantics, since that would probably be a bad idea even if I knew Python. This is just "something similar to traversing a string in Python". – Mike Seymour Oct 29 '14 at 11:30
  • 1
    in c++17, this [will probably become](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3994.htm) `for (c : str) { ... }` – TemplateRex Nov 02 '14 at 21:45
13

With a std::string it's going to be quite easy:

std::string myStr = "Hello";
for (char c : myStr)
    std::cout << c; // Print out every character

or by reference in case you want to modify it..

std::string myStr = "Hello";
for (char& c : myStr)
    c = 'A';
// mystr = "AAAAA"
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • 1
    @NeilKirk I don't think you can edit the string much using a python loop either. Since your using C++11, you can use `auto` and `auto&`... – matsjoyce Oct 29 '14 at 11:04
  • 1
    @matsjoyce But why? That would just be obfuscation. – James Kanze Oct 29 '14 at 11:24
  • @JamesKanze But auto is the best thing ever! Which is easier to read: `std::map>::const_iterator& iter;` or `auto iter;`? Read [this](http://stackoverflow.com/questions/6434971/how-much-is-too-much-with-c0x-auto-keyword). – matsjoyce Oct 29 '14 at 11:26
  • 4
    @matsjoyce: But this is a `char`, not a crazy nested (or unnamable) type. All `auto` does here is make it slightly harder to figure the type out. – Mike Seymour Oct 29 '14 at 11:28
  • @matsjoyce: Not really. It'll update the type for you, but not the semantics of whatever you're doing with it. For example, the first loop here will break due to the use of `cout` rather than `wcout`. But this is getting rather off-topic. Lets just agree that, here, you *can* use `auto` but it doesn't make much difference. – Mike Seymour Oct 29 '14 at 11:35
  • 1
    @matsjoyce On the contrary. It will mean that when you go looking for all of the `char`, to find where you need to fix the logic, you won't find this one. – James Kanze Oct 29 '14 at 12:08
  • Marco, both yours and Mike's are great somewhat identical answers. Occam's razor made the selection for best answer. :) – Quester Oct 30 '14 at 01:09
  • Glad that you liked my answer. No worries, Mike's often more concise than me. I upvoted his answer too – Marco A. Oct 30 '14 at 01:11
0

use range based for loop like this

char a1[5] = {'a', 'a', 'a', 'c'};
for (auto a : a1)
    cout << a << endl;
emsr
  • 15,539
  • 6
  • 49
  • 62
Ali Kazmi
  • 1,460
  • 9
  • 22