(C ++) Deque: Differences between types of access to positions

0

I'm implementing the Dynamic Scrolling Backpack Problem in C++ and chose the deck to build my list. Studying on it, I realized these two different ways of accessing a specific position in the list:

//Retorna o item i da lista mydeque
mydeque[i]
mydeque.at(i)

What is the real difference between the two, since at first sight both are identical?

    
asked by anonymous 28.09.2016 / 20:19

2 answers

1

std::deque and std::vector have two ways of accessing elements in an indexed way:

  • Hair operator brackets []
  • By member function at

The basic difference between the two is that the at function checks the index, throwing an exception ( std::out_of_range ) in case of access beyond the size of the container. The brackets operator assumes that it will always be invoked with a valid index.

    
28.09.2016 / 20:25
0

mideque [i]: accesses the element without checking the validity of the index
mideque.at (i): accesses the element by checking the validity of the index

link link

    
28.09.2016 / 20:23