Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stl

deque

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

like image 851
Lucky Man Avatar asked Jan 22 '12 23:01

Lucky Man


1 Answers

Since you asked for "the best way":

for (char c : My_Deque) { std::cout << c <<  " "; }
like image 111
Kerrek SB Avatar answered Sep 21 '22 07:09

Kerrek SB