[英]How to access vector inside a structure?
I want to know how to access a vector inside a structure as defined below 我想知道如何在如下定义的结构内访问向量
#include<vector>
#include<iostream>
#include<stdlib.h>
using namespace std;
struct Hello{
vector<int>vec ;
};
struct Hello *h[1][1];
int main()
{
struct Hello *v = (struct Hello *)malloc(sizeof(struct Hello));
h[0][0]=v;
0][0]->vec.push_back(13);
//How to add 13 to vector vec?
//It shows an error. But why?
cout<<h[0][0]->vec.at(0)<<endl;
return 0;
}
This is your problem: 这是你的问题:
struct Hello *v = (struct Hello *)malloc(sizeof(struct Hello));
(As an aside, struct
is superfluous every time it was used in that line.) (
struct
一句,每次在该行中使用struct
都是多余的。)
It does allocate space for a Hello
, but does not construct it. 它确实为
Hello
分配了空间,但没有构造它。
And Hello
has a non-trivial ctor because its member std::vector
has one, thus omitting the call to the ctor is wrong. 而且
Hello
ctor不平凡,因为它的成员std::vector
有一个,所以忽略对ctor的调用是错误的。
Either use placement-new to in-place construct the Hello
, or better yet allocate and construct in one go the C++ way: 要么使用placement-new来就地构造
Hello
,要么更好地以C ++的方式进行分配和构造:
Hello *v = new Hello;
// andd later deallocate
delete v;
Best, just avoid dynamic allocation altogether: 最好,完全避免动态分配:
Hello h;
Member-access uses .
成员访问用途
.
or for pointers ->
. 或对于指针
->
。
That's not how you write code in C++. 那不是用C ++编写代码的方式。
First, as properly mentioned in the comments, do not use malloc
ever. 首先,正如注释中正确提到的,永远不要使用
malloc
。 Use new
if you need dynamic memory allocation. 如果需要动态内存分配,请使用
new
。 Otherwise you would have non-constructed objects, like you have already been told. 否则,您将拥有未构造的对象,就像您已经被告知的那样。
Second, do not use new
when you do not have to. 其次,不必使用
new
。 If you need dynamic memory allocation, most probably you want to manage this memory with a smart pointer , like std::unique_ptr
. 如果需要动态内存分配,则很可能希望使用智能指针 (例如
std::unique_ptr
来管理此内存。 To construct a unique_ptr<T>
, use std::make_unique<T>()
function. 要构造一个
unique_ptr<T>
,请使用std::make_unique<T>()
函数。 Using make_unique
will make you program leak-free and exception safe at zero cost. 使用
make_unique
将使您的程序无泄漏,并且以零成本保护异常安全。 Using make_unique()
will prevent some tricky situations with leaking due to exceptions and save you a couple of keystrokes when writing the code. 使用
make_unique()
可以防止由于异常而导致的一些棘手情况,并且在编写代码时可以省去几次击键。 At zero cost, of course. 当然,零成本。 See GotW #89 Solution: Smart Pointers .
请参阅GotW#89解决方案:智能指针 。
Third, most likely you don't even need dynamic memory allocation here. 第三,很可能您在这里甚至不需要动态内存分配。 This is not Java, we love to allocate things on the stack.
这不是Java,我们喜欢在堆栈上分配东西。 Remember that
std::vector
itself does dynamic memory allocation to store its buffer. 请记住,
std::vector
本身会进行动态内存分配以存储其缓冲区。 And due to move semantics it can move this storage effective at runtime. 并且由于移动语义,它可以在运行时有效地移动此存储。 There is no much sense to allocate
vector
itself in dynamic memory. 在动态内存中分配
vector
本身没有多大意义。 See Why should C++ programmers minimize use of 'new'? 请参阅C ++程序员为什么要尽量减少对“ new”的使用?
Do a search on each emphasized item for details. 在每个强调的项目上进行搜索以获取详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.