What is the best way to access deque's element in C++ STL

Lucky Man picture Lucky Man · Jan 23, 2012 · Viewed 12.2k times · Source

I have a deque:

deque<char> My_Deque;
My_Path.push_front('a');
My_Path.push_front('b');
My_Path.push_front('c');
My_Path.push_front('d');
My_Path.push_front('e');

There are such ways to output it.

The first:

deque<char>::iterator It;
for ( It = My_Deque.begin(); It != My_Deque.end(); It++ )
    cout << *It <<  " ";

The second:

for (i=0;i<My_Deque.size();i++) {
    cout << My_Deque[i] <<  " ";
}

What is the best way to access deque's element - through iterator or like this: My_Deque[i]? Has a deque<...> element an array of pointers to each element for fast access to it's data or it provides access to it's random element in consequtive way (like on a picture below)? enter image description here

Answer

Kerrek SB picture Kerrek SB · Jan 23, 2012

Since you asked for "the best way":

for (char c : My_Deque) { std::cout << c <<  " "; }