C++列表/矢量帮助
C++ list/vector help
我是C++的新手,所以这可能是一个非常简单的问题,但我在网上找不到任何有帮助的例子。
我已经定义了自己的Bubble
类,我需要创建一个vector
/list
(我习惯了C#和Java,所以我不确定哪一个是正确的)来动态存储Bubble
对象
#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);
}
list
和vector
都不起作用——它在我的错误列表中说"列表/矢量不是模板"。
我应该使用list
还是vector
?我该如何正确地实现它?
函数vector_add()和list.add()不存在。
#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);
}
向量和列表之间几乎没有明显的区别,但在功能上是有区别的。
与其他基本标准相比序列容器(deques和列表),矢量通常是最多的高效的访问时间元素,并添加或删除元素从序列的末尾开始。对于涉及插入或移除其他位置的元素他们的表现比deques和list,并且拥有更少一致迭代器和引用比列表多。
与其他基本标准相比序列容器(矢量和deques),列表通常执行在插入、提取和在容器,因此也在密集使用的算法这些,就像排序算法。
它们位于std
命名空间中。就像C++标准库的所有部分一样。因此,它们被正确地称为std::list
和std::vector
。
它们也没有名为add
的成员函数。您可能需要查找一个C++引用。
Vector和list是std命名空间的一部分。所以你应该这样声明你的向量和你的列表:
std::list<Bubble> bubbles;
std::vector<Bubble> bubbles_two;
此外,添加元素的成员函数是push_back。
list
和vector
位于std
命名空间中,因此您必须在那里查找它们。
std::vector<Bubble> bubbles;
在任何一种情况下,都可以使用.push_back
附加到容器中。如果有疑问,您通常应该更喜欢vector
。
此处缺少命名空间。list和vector都是标准名称空间的一部分,这意味着您可以通过在文件开头写入using namespace std;
一次或在使用变量的每个位置写入std::list
和std::vector
来全局包含名称空间。
尝试
std::list<Bubble> bubbles;
std::vector<Bubble> bubbles_two;
列表和矢量在std
命名空间中定义。
#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;
}
}
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 需要帮助设置在C++中使用的Potrace
- C 需要帮助的作业程序,该程序在文件列表中读取并在每个双重的双重列表中添加一个字符串
- C 指针节点帮助 - 将参数复制到链接的列表节点更改该节点的不同部分
- 寻找一种更快的方法来帮助减少/创建大量字符串列表
- 链接列表帮助C
- 我需要帮助对此双链接列表(C )进行分类
- C++ 指针帮助缺少参数列表使用 &帐户::获取余额
- 逻辑帮助:比较值并取最小距离,同时将其从"available to compare"列表中删除
- 需要帮助了解列表容器
- 家庭作业帮助:用户制作的列表输出
- 使用C++列表标准库来帮助链接列表程序
- 链接列表帮助,迭代器问题
- C++列表/矢量帮助
- 在C++链接列表中帮助.show记录功能启动无限循环
- 卡住了,需要帮助从矢量列表返回时间跨度