tags:

views:

15

answers:

2

Hi there.

For example I have an array with about 10 elements in it.

std::deque<int> d;
front_inserter(d) = 100;
front_inserter(d) = 200;
front_inserter(d) = 300;
...
front_inserter(d) = 900;
front_inserter(d) = 1000;

Question: how to find 900 element, without using [] access? If the size of the massive will be changes, for example to 123, how to find 122 element?

PS: I don't want to use [] because this method does not perform d[-1] check...

Thanks.

+1  A: 

use deque::at.

d.at(121)

skwllsp
@skwllsp You are saying to check like this? - `d.at(d.size() - 1)`?
mosg
Yes. And in case d.size() == 0 there must be an exception
skwllsp
I resolved this solution with the other way, not using STL, but with help of program logic. Any way thanks.
mosg
A: 

If you mean a runtime check throwing in case of out of bound access you can use ::at(pos). If you mean d[-1] being the last element, d[-2] the second-last and so on (a-la Python) then you've to code your own (possibly template) function for that.

6502
No, I need a method to get `second-last` element from deque-array which could be with the different size...
mosg