在结构中初始化向量
Initialization of a vector inside a struct
struct geopoint {
double x;
double y;
const char * description;
};
struct georectangle {
double left_x;
double bottom_y;
double right_x;
double top_y;
const char * description;
};
struct geomap {
vector < geopoint * > geopointList;
vector < georectangle * > georectangleList;
};
struct geomap * geomap_new() {
struct geomap * newGeoMap = (struct geomap * ) malloc(sizeof(struct geomap));
return newGeoMap;
}
void geomap_delete(struct geomap * m) {
printf("%lu\n", m->geopointList.size());
for (int i = 0; i < m->geopointList.size(); i++) {
free(m->geopointList[i]);
}
printf("%lu\n", m->georectangleList.size());
for (int i = 0; i < m->georectangleList.size(); i++) {
free(m->georectangleList[i]);
}
free(m);
}
int main () {
struct geomap * m = geomap_new();
assert(m);
geomap_delete(m);
}
我是 C++ 的新手,我对这种语言中的对象初始化感到非常困惑...在 Java 中,当您初始化一个不是 A 的对象时,您总是使用 new
关键字原始类型。在 C++ 中,在我看来,有时默认构造函数会自动执行,有时则不会。
在上面的代码片段中,我通过 geomap_new()
函数创建了一个 struct geomap
的实例,其中包含两个指针向量。
我的问题如下:
如何将这两个向量初始化为全新的空向量?在 Java 中,我会使用 new
关键字... C++ 中是否也有这样的东西?
我问这个问题是因为如果我不以任何方式初始化它们,当我在 geomap_delete
函数中打印这两个向量的大小时,geopointList
的大小为 0,因为它应该是,但是 georectangleList
的大小是一个很大的随机数。在我看来,只有第一个向量正在初始化。
另一个问题...
如果开始在向量中添加很多东西,这些向量就会开始增长。它们的大小是否有可能变得大于结构本身的大小?该结构是否要 realloc
?
您可以将代码简化为
#include <iostream>
#include <string>
#include <vector>
struct geopoint {
double x;
double y;
std::string description;
};
struct georectangle {
double left_x;
double bottom_y;
double right_x;
double top_y;
std::string description;
};
struct geomap {
std::vector<geopoint> geopointList;
std::vector<georectangle> georectangleList;
};
int main () {
geomap m;
std::cout << "m.geopointList.size(): " << m.geopointList.size() << '\n';
std::cout << "m.georectangleList.size(): " << m.georectangleList.size() << '\n';
m.geopointList.push_back({1, 2, "Description"});
m.georectangleList.push_back({1, 2, 3, 4, "Description"});
std::cout << "m.geopointList.size(): " << m.geopointList.size() << '\n';
std::cout << "m.georectangleList.size(): " << m.georectangleList.size() << '\n';
}
避免此类问题。避免动态内存分配和释放。不要使用 malloc
、free
、new
和 delete
。
"How do I initialize these two vectors to be fresh new empty vectors?" 默认构造函数会为您完成此操作。
"Is it possible that their size will become bigger than the size of the struct itself? Is the struct going to ```realloc``" 该结构具有固定大小并包含两个向量。两个向量都包含一个 reference/pointer 到结构外部的动态内存。结构和两个向量都在堆栈上创建(在我的示例代码中),向量的动态内存在堆上。
struct geopoint {
double x;
double y;
const char * description;
};
struct georectangle {
double left_x;
double bottom_y;
double right_x;
double top_y;
const char * description;
};
struct geomap {
vector < geopoint * > geopointList;
vector < georectangle * > georectangleList;
};
struct geomap * geomap_new() {
struct geomap * newGeoMap = (struct geomap * ) malloc(sizeof(struct geomap));
return newGeoMap;
}
void geomap_delete(struct geomap * m) {
printf("%lu\n", m->geopointList.size());
for (int i = 0; i < m->geopointList.size(); i++) {
free(m->geopointList[i]);
}
printf("%lu\n", m->georectangleList.size());
for (int i = 0; i < m->georectangleList.size(); i++) {
free(m->georectangleList[i]);
}
free(m);
}
int main () {
struct geomap * m = geomap_new();
assert(m);
geomap_delete(m);
}
我是 C++ 的新手,我对这种语言中的对象初始化感到非常困惑...在 Java 中,当您初始化一个不是 A 的对象时,您总是使用 new
关键字原始类型。在 C++ 中,在我看来,有时默认构造函数会自动执行,有时则不会。
在上面的代码片段中,我通过 geomap_new()
函数创建了一个 struct geomap
的实例,其中包含两个指针向量。
我的问题如下:
如何将这两个向量初始化为全新的空向量?在 Java 中,我会使用 new
关键字... C++ 中是否也有这样的东西?
我问这个问题是因为如果我不以任何方式初始化它们,当我在 geomap_delete
函数中打印这两个向量的大小时,geopointList
的大小为 0,因为它应该是,但是 georectangleList
的大小是一个很大的随机数。在我看来,只有第一个向量正在初始化。
另一个问题...
如果开始在向量中添加很多东西,这些向量就会开始增长。它们的大小是否有可能变得大于结构本身的大小?该结构是否要 realloc
?
您可以将代码简化为
#include <iostream>
#include <string>
#include <vector>
struct geopoint {
double x;
double y;
std::string description;
};
struct georectangle {
double left_x;
double bottom_y;
double right_x;
double top_y;
std::string description;
};
struct geomap {
std::vector<geopoint> geopointList;
std::vector<georectangle> georectangleList;
};
int main () {
geomap m;
std::cout << "m.geopointList.size(): " << m.geopointList.size() << '\n';
std::cout << "m.georectangleList.size(): " << m.georectangleList.size() << '\n';
m.geopointList.push_back({1, 2, "Description"});
m.georectangleList.push_back({1, 2, 3, 4, "Description"});
std::cout << "m.geopointList.size(): " << m.geopointList.size() << '\n';
std::cout << "m.georectangleList.size(): " << m.georectangleList.size() << '\n';
}
避免此类问题。避免动态内存分配和释放。不要使用 malloc
、free
、new
和 delete
。
"How do I initialize these two vectors to be fresh new empty vectors?" 默认构造函数会为您完成此操作。
"Is it possible that their size will become bigger than the size of the struct itself? Is the struct going to ```realloc``" 该结构具有固定大小并包含两个向量。两个向量都包含一个 reference/pointer 到结构外部的动态内存。结构和两个向量都在堆栈上创建(在我的示例代码中),向量的动态内存在堆上。