Plain arrays define no functions but you can use the library begin
and end
functions introduced with c++11:
#include <iostream>
int main()
{
char a[] = { 'H', 'e', 'l', 'l', 'o' }; // use chars instead of strings
for (auto it = std::begin(a); it != std::end(a); ++it) // use iterators
{
std::cout << *it;
}
}
Run on ideone.
Or even shorter using a range for as pointed out by BarryTheHatchet:
#include <iostream>
int main()
{
char a[] = { 'H', 'e', 'l', 'l', 'o' };
for (auto c : a)
{
std::cout << c;
}
}
Run on ideone.
Keep in mind that the list initialization as above doesn't include a NULL char. If you declare your array with a NULL char at the end you can use cout directly without for loop:
char a[] = { 'H', 'e', 'l', 'l', 'o', '\0' }; // explicit NULL with list initialization
std::cout << a;
char b[] = "Hello"; // implicit NULL when using a literal
std::cout << b;