C++ std::string Looping through each character


Example

C++11

std::string supports iterators, and so you can use a ranged based loop to iterate through each character:

std::string str = "Hello World!";
for (auto c : str)
    std::cout << c;

You can use a "traditional" for loop to loop through every character:

std::string str = "Hello World!";
for (std::size_t i = 0; i < str.length(); ++i)
    std::cout << str[i];