在 c 中初始化对象数组-ag捕鱼王app官网

在 c 中初始化对象数组

作者:迹忆客 最近更新:2023/03/15 浏览次数:

本文将演示在 c 中用参数化构造函数初始化对象数组的多种方法。

使用 new 运算符在 c 中使用参数化构造函数初始化对象数组

new 运算符是 c 动态内存管理接口的一部分,它等效于 c 语言中的 malloc 函数。请注意,动态内存管理需要用户分配指定固定大小的内存并将其在不再需要时返回给系统的流程。

new 用于从系统请求固定数量的字节,如果调用成功,它将返回指向存储区域的指针。在下面的示例中,我们定义了一个名为 pair 的类,它具有两个构造函数和一个 printpair,以将数据成员输出到控制台。首先,我们需要使用 new 运算符分配一个由四对组成的 pairs 数组。接下来,我们可以使用 for 循环遍历数组,并且每次迭代都使用参数化的构造函数初始化该元素。请注意,应该在程序退出之前使用 delete 操作符释放分配的数组。还要注意,delete [] 符号对于取消分配数组的每个元素是必需的。

#include #include 
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
class pair {
    int x, y;
public:
    pair() = default;
    pair(int a, int b) : x(a), y(b) {}
    void printpair() const {
        cout << "x: " << x << endl << "y: " << y << endl;
    }
};
int main(){
    pair *arr = new pair[4];
    for (int i = 0; i < 4; i) {
        arr[i] = pair(i * 2, i * i);
    }
    for (int i = 0; i < 4; i) {
        arr[i].printpair();
    }
    delete [] arr;
    return exit_success;
}

输出:

x: 0
y: 0
x: 2
y: 1
x: 4
y: 4
x: 6
y: 9

使用 std::vector::push_back 函数初始化带有参数化构造函数的对象数组

另外,更多的无头方法是将对象存储在 std::vector 容器中,该容器将提供内置函数来动态初始化新元素。即,pair 定义保持不变,但是每次迭代时,我们将调用 push_back 函数,并将其从第二个构造函数传递给 pair 值。从好的方面来说,我们不必担心内存资源的释放。他们会自动清理。

#include #include 
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
class pair {
    int x, y;
public:
    pair() = default;
    pair(int a, int b) : x(a), y(b) {}
    void printpair() const {
        cout << "x: " << x << endl << "y: " << y << endl;
    }
};
int main(){
    vector<pair> array;
    for (int i = 0; i < 4; i) {
        array.push_back(pair(i * 2, i * i));
    }
    for (const auto &item : array) {
        item.printpair();
    }
    return exit_success;
}

输出:

x: 0
y: 0
x: 2
y: 1
x: 4
y: 4
x: 6
y: 9

使用 std::vector::emplace_back 函数初始化带有参数化构造函数的对象数组

用参数化构造函数初始化对象数组的另一种方法是利用 std::vector 类的 emplace_back 函数。这样,我们只需要传递 pair 构造函数的值,而 emplace_back 会自动为我们构造一个新元素。像以前的方法一样,该方法也不需要用户担心内存管理。

#include #include 
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
class pair {
    int x, y;
public:
    pair() = default;
    pair(int a, int b) : x(a), y(b) {}
    void printpair() const {
        cout << "x: " << x << endl << "y: " << y << endl;
    }
};
int main(){
    vector<pair> array;
    for (int i = 0; i < 4; i) {
        array.emplace_back(i * 2, i * i);
    }
    for (const auto &item : array) {
        item.printpair();
    }
    return exit_success;
}

输出:

x: 0
y: 0
x: 2
y: 1
x: 4
y: 4
x: 6
y: 9

上一篇:

下一篇:在 c 中从数组中删除元素

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:c

可以使用 exit(0),无限循环和 sleep_n0m1 库在 arduino 中停止循环。

arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:c

可以通过使用复位按钮,softwarereset 库和 adafruit sleepydog 库来复位 arduino。

发布时间:2024/03/13 浏览次数:181 分类:c

可以使用简单的方法 toint()函数和 serial.parseint()函数将 char 转换为 int。

arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:c

可以使用 serial.print()和 serial.println()函数在串口监视器上显示变量值。

arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:c

可以使用 if 语句检查 arduino 中的不同条件。

arduino icsp

发布时间:2024/03/13 浏览次数:214 分类:c

icsp 引脚用于两个 arduino 之间的通信以及对 arduino 引导加载程序进行编程。

发布时间:2024/03/13 浏览次数:151 分类:c

可以使用 arduino 中的循环制作计数器。

使用 c 编程 arduino

发布时间:2024/03/13 浏览次数:127 分类:c

本教程将讨论使用 arduino ide 在 c 中对 arduino 进行编程。

arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:c

可以通过在 arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图