Iterate through string char by char

for(unsigned int i = 0; i<infix.length(); i++) {
    char c = infix[i]; //this is your character
}

That's how I've done it. Not sure if that's too "idiomatic".


If you're using std::string, there really isn't a reason to do this. You can use iterators:

for (auto i = inflix.begin(); i != inflix.end(); ++i) std::cout << *i << '\n';

As for your original code you should have been using char* instead of char and you didn't need the reference.


Your code needs a pointer, not a reference, but if using a C++11 compiler, all you need is:

void print(const std::string& infix)
{
    for(auto c : infix)
        std::cout << c << std::endl;
}

Tags:

C++