C++ list/vector help
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&开发者_运维百科lt;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?
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.
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.
Vector and list are part of the std namespace. So you should declare your vector and your list like this:
std::list<Bubble> bubbles;
std::vector<Bubble> bubbles_two;
Also, the member function to add an element is push_back.
list
and vector
are in the std
namespace, and you accordingly must look for them there.
std::vector<Bubble> bubbles;
In either case, you use .push_back
to append to the container. When in doubt, you should generally prefer vector
.
You are missing the namespace here. Both list and vector is part of the standard namespace which Means that you can include the namespace on a global basis by writing using namespace std;
once in the beginning of the file or write std::list
and std::vector
every where you are using the variables.
Try
std::list<Bubble> bubbles;
std::vector<Bubble> bubbles_two;
Lists and vectors are defined in the std
namespace.
#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;
}
}
精彩评论