创建实际对象数组C++
Create Array of Actual Objects C++
我想创建一个数组来存储实际对象,而不是C++中对象的指针?
有人能解释一下我该怎么做吗?是使用矢量更好还是直接像一样
Student s [10];
或
Student s [10][];
使用:
Student s [10];
创建一个包含10个Student
实例的数组。
我认为Student s [10][];
无效。
但是对于C++,我不会使用C类型数组,最好使用std::vector
或C++0x std::array
之类的类,这些类在没有最新标准库/编译器的情况下可能不可用。
std::vector
的上述示例
#include <vector>
...
std::vector<Student> students(10);
使用std::array
:
#include <array>
...
std::array<Student, 10> students;
不要使用数组。数组是C而不是C++。请改用std::vector
,这是处理此问题的C++方法。
如果您想使数组可增长,我建议使用std::vector,否则只使用Student students[10];对于10个对象。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '