Shadow
Shadow

Reputation: 23

How do I effectively access list information in C++?

I originally made this game in Python and then for a school project converted it over to C++.

The problem is that C++'s std::list does not allow me to access items like Python's list does (list[1]) and more so, accessing items of lists that are inside of a list (list[2][5]). I have not been able to find an effective way to be able to do this or an alternative to lists that will work.

Upvotes: 2

Views: 117

Answers (4)

5gon12eder
5gon12eder

Reputation: 25409

Don't use a std::list. It implements a doubly linked list which does not provide a subscript operator because random access for linked lists cannot be implemented with constant complexity.

Instead, use a std::vector which implements a dynamically growing array with subscript operator defined.

As @ShadowRanger has commented, you might also find std::deque useful. It supports constant time random access like std::vector but in addition has the ability to release storage automatically when you remove elements. (For std::vector, you have to do it explicitly by calling shrink_to_fit and that can easily go the wrong direction.) It is also superior when you need to append elements both at the beginning and the end. But as I understand, you don't want to change the number of elements in the container anyway.

Also note another difference between Python and C++. In Python, if you write

my_things = [1, 2, 3, 4]
your_things = [5, 6, 7]
our_things = [my_things, your_things]

then your_things and our_things[1] refer to the same list. This is because objects in Python are referred to by-reference. In C++, on the other hand, containers have value semantics. That is, if you write

std::vector<int> my_things = {1, 2, 3, 4};
std::vector<int> your_things = {5, 6, 7};
std::vector<std::vector<int>> our_things = {my_things, your_things};

our_things will contain copies of my_things and your_things and changing either will not affect the other. If this is not what you want, you could instead define the nested list at once.

std::vector<std::vector<int>> our_things = {
  {1, 2, 3, 4},  // my things
  {5, 6, 7},     // your things
};

Upvotes: 12

woodtluk
woodtluk

Reputation: 963

If you don't need to change the size of the list you could use plain array.

That might even be the fastest option. But std::vector is similar in performance as long as you don't change size.

Upvotes: 0

GutiMac
GutiMac

Reputation: 2472

In C++, std::list does not support random access lookup via operator []. If you are going to continue to use a list, you should look at the std::advance function.

list<string>::iterator itr = whichList.begin();
std::advance(itr, i);
if (itr->empty())
{
 /* ... assuming that you're testing for an empty string */
}

If possible, you might want to consider other standard containers, such as std::vector or std::deque, both of which provide random access lookup via operator [].

Upvotes: 0

Aydin Gerek
Aydin Gerek

Reputation: 292

You can use vectors instead of lists.

Upvotes: 0

Related Questions