0

I have these typedefs:

typedef pair<k2Base, list<v2Base>> shuffled_pair;
typedef list<shuffled_pair> shuffled_list;

and this function:

shuffled_pair getItem(unsigned int index){
    return this->_items[index];
} 

where this->_items is of type shuffled_list as declared in the typedef.

I get this error from the compiler:

 Type 'const shuffled_list' (aka 'const list<pair<k2Base, list<v2Base> > >') does not provide a subscript operator

But the type is basically a list type, so what is the problem?

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
Eyzuky
  • 1,843
  • 2
  • 22
  • 45
  • If you downvote please explain why and not just downvote. I can edit my question if it is needed, but not if I do not know the problem with it. – Eyzuky May 16 '17 at 17:16
  • Provide a [MCVE] that reproduces the problem as required please. – πάντα ῥεῖ May 16 '17 at 17:17
  • @πάνταῥεῖ The question is specific enough. The word "typedef" in the title threw me for a loop at first. I'll remove it. – Potatoswatter May 16 '17 at 17:19
  • What do you mean example? I am trying to get an element from a list, using an index. I show you the compile error that I get trying to do that. rather than that what else can be shown? thanks. – Eyzuky May 16 '17 at 17:20
  • 1
    _@Eyzuky_ Read the link first. Though @potatoswatter gor it right already. – πάντα ῥεῖ May 16 '17 at 17:21
  • @Potatoswatter thanks, I thought the typedef has something to do with the problem, sorry for the confusion – Eyzuky May 16 '17 at 17:21
  • 2
    It can never be repeated often enough: NEVER use std::list unless you actually know that a linked list is the right data structure. It is almost never the right data structure. Also, std::list doesn't implement `operator[]()` so you're calling a function that doesn't exist (http://en.cppreference.com/w/cpp/container/list) – Rob K May 16 '17 at 17:24
  • Great, thanks for this clarification regarding the list data structure. – Eyzuky May 16 '17 at 17:25
  • @BoundaryImposition If I understood the problem from the error message I wouldn't ask the question. – Eyzuky May 16 '17 at 18:02
  • I guess I was expressing surprise that you read a very clear error message yet still couldn't work it out, and didn't simply Google "C++ list does not provide a subscript operator". I've marked this question as a duplicate of one of the top answers that appears when you do Google using those terms. – Lightness Races in Orbit May 16 '17 at 18:09
  • The error message refers to the type defined in the typedef. That is why I thought it is related to that. Sorry for being really dumb. – Eyzuky May 16 '17 at 18:27
  • @BoundaryImposition – Eyzuky May 16 '17 at 18:27
  • Okay, that makes some sense – Lightness Races in Orbit May 16 '17 at 18:35

3 Answers3

5

The standard library only provides the subscript operator when it has O(1) runtime.

For std::list, the library requires you to get an iterator and use std::advance to acknowledge the performance hit.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
1

In C++, a list is a linked list. For the type of list that you're looking for, you should look into vector or deque, which have a constant-time subscript operator. Deque is generally smarter than vector but doesn't interoperate very well with functions that need pointers to sequences of elements.

zneak
  • 134,922
  • 42
  • 253
  • 328
1

Because std::list doesn't implement operator[] for direct access to members. See std::list docs.

Because of the way std::list is implemented, this would be an O(n) operation. Try using std::vector or std::deque if you need direct member access.

crazypeter
  • 1,279
  • 1
  • 10
  • 14