结构向量 c++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18321605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vector of structs c++
提问by Samuel
I am having an issue with creating a vector of structs. In my function AddApp I am unable to "push_back" the newly allocated struct into the vector. I get the error message "Invalid Arguments". I looked around to see if someone had a similar issue but nothing that helped. Can anyone point out whats the flaw in my logic? Thx.
我在创建结构向量时遇到问题。在我的函数 AddApp 中,我无法将新分配的结构“推回”到向量中。我收到错误消息“无效参数”。我环顾四周,看看是否有人有类似的问题,但没有任何帮助。谁能指出我的逻辑有什么缺陷?谢谢。
class AppHolder
{
private:
struct Info
{
int refNumber;
string name;
string link;
};
vector<Info> dataBase;
public:
void AddApp(int ,string ,string );
};
void AppHolder::AddApp(int R, string N, string L)
{
Info *newApp = new Info;
newApp -> name = N;
newApp -> link = L;
newApp -> refNumber = R;
dataBase.push_back(newApp);
}
回答by milleniumbug
The reason your code fails is that std::vector<Info>::push_back
requires that you pass it object of type Info
, but you pass Info*
.
您的代码失败的原因是std::vector<Info>::push_back
要求您将类型为 object 的传递给它Info
,但是您传递了Info*
.
We can solve this by using Info
objects directly instead of using pointers to new
ed variables:
我们可以通过Info
直接使用对象而不是使用指向new
ed 变量的指针来解决这个问题:
void AppHolder::AddApp(int R, string N, string L)
{
Info newApp;
newApp.name = N;
newApp.link = L;
newApp.refNumber = R;
dataBase.push_back(newApp);
}
Info* newApp = new Info;
declares a pointer to Info, and initializing it with address of dynamically created object of type Info
, which must be deleted later, or destructor won't be called and memory won't be released.
Info* newApp = new Info;
声明一个指向 Info 的指针,并使用类型为动态创建的对象的地址对其进行初始化Info
,该对象必须稍后删除,否则不会调用析构函数并且不会释放内存。
Info newApp;
declares an automatic variable of type Info
which destructor is called at the end of scope, and its memory will be released.
Info newApp;
声明一个类型的自动变量,Info
在作用域结束时调用析构函数,其内存将被释放。
Using new
with pointers is discouraged in modern C++, as it requires manual delete
of pointers, it's exception-unsafe (if exception is thrown before delete, resources aren't deleted) and involves writing a lot of code (copy constructor, copy assignment operator, destructor, move constructor, move assignment operator) to ensure your class works correctly. There are, however, situations where pointer usage is required (storing instances of derived classes in containers), in that case you want to use smart pointerswhich can manage resources for you.
new
在现代 C++ 中不鼓励使用指针,因为它需要delete
指针手册,它是异常不安全的(如果在删除之前抛出异常,资源不会被删除)并且涉及编写大量代码(复制构造函数、复制赋值运算符、析构函数) 、移动构造函数、移动赋值运算符)以确保您的类正常工作。但是,在某些情况下需要使用指针(将派生类的实例存储在容器中),在这种情况下,您希望使用可以为您管理资源的智能指针。
回答by Jimbo
The declaration
声明
vector<Info> dataBase;
Declares a vector that takes Info
objects. In the initialiser,
声明一个接受Info
对象的向量。在初始化程序中,
Info *newApp = new Info;
newApp -> name = N;
newApp -> link = L;
newApp -> refNumber = R;
dataBase.push_back(newApp);
The statement dataBase.push_back(newApp);
tries to push a pointer to an Info
structureon to the vector (but your vector wants objects, not pointers to objects).
该语句dataBase.push_back(newApp);
试图将指向Info
结构的指针推送到向量上(但您的向量需要对象,而不是指向对象的指针)。
So, you either need to decalre a vector of pointers to Info
structures - vector<Info *>
- and remember to free each pointer in the class destructor. Or, just create an Info
structure on the stack (i.e. an actual object) and push that onto the vector (which will create a copy of the struct inside the vector - will use default struct copy constructor in your case, which is fine because the struct doesn't contain pointers etc or anything that requires or does not support a deep copy).
因此,您要么需要对指向Info
结构的指针向量进行 decalre vector<Info *>
- 并记住释放类 destructor 中的每个指针。或者,只需Info
在堆栈上创建一个结构(即实际对象)并将其推送到向量上(这将在向量内创建结构的副本 - 在您的情况下将使用默认的结构复制构造函数,这很好,因为结构不包含指针等或任何需要或不支持深拷贝的东西)。
The latter method is safer, because you don't have to remember to free anything, and also more efficient in this case, probably, because although you're copying a structure, you don't make an expensive system call to the OS to make the memory allocation.
后一种方法更安全,因为您不必记住释放任何东西,而且在这种情况下也更有效,可能是因为尽管您正在复制结构,但您不会对操作系统进行昂贵的系统调用进行内存分配。
回答by Terry G Lorber
The reason your compiler is complaining is that you defined dataBase to be a vector of Info
instances, and you are trying to push a pointer to an Info
instance.
您的编译器抱怨的原因是您将 dataBase 定义为Info
实例向量,并且您试图将指针推送到Info
实例。
Changing the vector<Info>
declaration to vector<Info*>
is one way, or change the AddApp
method:
将vector<Info>
声明更改vector<Info*>
为一种方法,或更改AddApp
方法:
void AppHolder::AddApp(int R, string N, string L)
{
Info newApp;
newApp.name = N;
newApp.link = L;
newApp.refNumber = R;
dataBase.push_back(newApp);
}
As others have pointed out, the use of new
in C++ is discouraged, so keeping your container definition is preferred. Change your implementation to use the correct type.
正如其他人指出的那样,new
不鼓励在 C++ 中使用,因此最好保留您的容器定义。更改您的实现以使用正确的类型。
A commenter clarifies why one should be careful when using new
in C++.
一位评论者澄清了为什么new
在 C++ 中使用时应该小心。
回答by Przemys?aw Kalita
You must turn this:
你必须转这个:
vector<Info> dataBase;
into this:
进入这个:
vector<Info*> dataBase;
as you want to push_back a pointer, and vector is declared to take objects.
因为你想 push_back 一个指针,并且 vector 被声明为接受对象。