C ++ List / Vector Reference

I am new to C ++, so this is probably a very simple question, but I could not find any examples that helped.

I have 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 if this is correct) to dynamically store Bubble objects. Here is my code:

 #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 list nor vector works — it says “list / vector is not a template” in my error list.

What should I use, list or vector ? And how to implement it correctly?

+7
source share
7 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 exists.

Compared to another basic standard, sequence containers (deques and lists), vectors are usually the most time-efficient for accessing elements and adding or removing elements from the end of the sequence. For operations related to inserting or deleting elements in other places than the end, they perform worse than decks and lists, and have less consistent iterators and links than lists.

Compared with other basic standards, sequence containers (vectors and deques), lists generally perform better inserting, retrieving and moving elements in any position inside the container, and therefore in algorithms that use them intensively as sorting algorithms.

+7
source

They are in the std . Like all parts of the C ++ standard library. Therefore, they are correctly called std::list and std::vector .

They also do not have member functions called add . You can find the C ++ link.

+5
source

The vector and list are part of the std namespace. Therefore, you should declare your vector and your list as follows:

 std::list<Bubble> bubbles; std::vector<Bubble> bubbles_two; 

In addition, the member function for adding an item is push_back.

+4
source

list and vector are in the std , and you should accordingly look for them there.

std::vector<Bubble> bubbles;

In any case, you use .push_back to add to the container. When in doubt, you usually prefer vector .

+2
source

There is no namespace here. Both lists and the vector are part of the standard namespace, which means that you can include the namespace globally by writing using namespace std; once at the beginning of the file or write std::list and std::vector every time you use variables.

+2
source

Try

 std::list<Bubble> bubbles; std::vector<Bubble> bubbles_two; 

Lists and vectors are defined in the std .

+1
source
 #include <iostream> //Desouky// using namespace std; struct node { int data; node* link= NULL; }; int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); node* head = new node; node*temp = new node; head = temp; int x; cin >> x; temp->data = x; while (cin >> x) { node* temp1 = new node; temp1->data = x; temp->link = temp1; temp = temp->link; } temp = head; while (temp != NULL) { cout << temp->data << " "; temp = temp->link; } } 
0
source

All Articles