Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ list/vector help

Tags:

c++

I'm new to C++ so this is probably a very simple question, but I haven't been able to find any examples online that have helped.

I've defined my own Bubble class and I need to create a vector/list (I'm used to C# and Java, so I'm not sure which is correct) to dynamically store Bubble objects in. Here's my code so far:

#include "Bubble.h"
#include <vector>
#include <list>

int backgroundImages[10]; 
list<Bubble> bubbles;
vector<Bubble> bubbles_two;
Bubble b;

void AppMain()
{
    loadImages();
    ViewAdd(backgroundImages[8], 0,0);
    b = Bubble();
    b.velocity = Vector2D(9,4);

    //I know this can't be right..
    bubbles.add(b);
    bubbles_two.add(b);
}

Neither the list nor the vector works - it says "list/vector is not a template" in my error list.

Which should I use, list or vector? And how do I correctly implement it?

like image 546
Cbas Avatar asked Jun 24 '11 02:06

Cbas


People also ask

Is list better than vector?

Since a list uses non-contiguous memory, the time taken to insert an element inside a list is a lot more efficient than in the case of its vector counterpart because reallocation of memory is avoided. It consumes extra memory to store pointers to the element before and after a particular element.

How do I add a list to a vector?

Adding elements to a list. Additional vectors can be added by specifying the position in the list where we wish to append the new vector. The new elements are concatenated at the end of the list. Multiple elements can also be added to a list with the use of a 'for' or a 'while' loop.

Is vector same as list?

A list holds different data such as Numeric, Character, logical, etc. Vector stores elements of the same type or converts implicitly. Lists are recursive, whereas vector is not. The vector is one-dimensional, whereas the list is a multidimensional object.

What is the difference between std :: list and std::vector?

In vector, each element only requires the space for itself only. In list, each element requires extra space for the node which holds the element, including pointers to the next and previous elements in the list. Insertion at the end requires constant time but insertion elsewhere is costly.


2 Answers

The functions vector.add() and list.add() do not exist.

#include "Bubble.h"
#include <vector>
#include <list>

int backgroundImages[10]; 
std::list<Bubble> bubbles(); // use the std namespace and instantiate it
std::vector<Bubble> bubbles_two(); // same here
Bubble b;

void AppMain()
{
    loadImages();
    ViewAdd(backgroundImages[8], 0,0);
    b = Bubble();
    b.velocity = Vector2D(9,4);

    //I know this can't be right..
    bubbles.push_back(b); // std::list also defines the method push_front
    bubbles_two.push_back(b);
}

There are almost no obvious differences between the vector and the list, but functionally, there are.

Compared to the other base standard sequence containers (deques and lists), vectors are generally the most efficient in time for accessing elements and to add or remove elements from the end of the sequence. For operations that involve inserting or removing elements at positions other than the end, they perform worse than deques and lists, and have less consistent iterators and references than lists.

Compared to other base standard sequence containers (vectors and deques), lists perform generally better in inserting, extracting and moving elements in any position within the container, and therefore also in algorithms that make intensive use of these, like sorting algorithms.

like image 78
foxy Avatar answered Nov 15 '22 14:11

foxy


They are in the std namespace. As are all parts of the C++ standard library. So they are correctly called std::list and std::vector.

They also don't have member functions called add. You may want to look up a C++ reference.

like image 25
Nicol Bolas Avatar answered Nov 15 '22 13:11

Nicol Bolas