迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 创建指针向量

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

本文将介绍几种在 C++ 中如何创建指针向量的方法。

使用 [] 符号在 C++ 中创建指针向量

由于指针类型可以很容易地修改,我们将在下面的例子中使用 int *来声明指针的向量。另外,如果你需要一个通用的地址类型来存储不透明的数据结构,我们也可以使用 void *指针,或者相反,使用一个指向自定义定义类的指针。

这个解决方案使用了一个 C 风格的数组符号-[],它声明了一个固定长度的数组。它与常规数组声明类似,但在这种情况下,我们对访问每个元素的地址感兴趣。我们使用&(取址)运算符来访问向量中的指针,并打印出来到控制台。注意,这些内存地址位于堆栈内存上。

#include <iostream>
#include <vector>

using std::cout;
using std::vector;
using std::endl;

constexpr int WIDTH = 8;

int main()
{
    int *pointers_vec[WIDTH];

    cout << "pointers_vec addresses:" << endl;
    for(auto &i : pointers_vec) {
        cout << &i << endl;
    }
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

pointers_vec addresses:
0x7ffecd7a00d0
0x7ffecd7a00d8
0x7ffecd7a00e0
0x7ffecd7a00e8
0x7ffecd7a00f0
0x7ffecd7a00f8
0x7ffecd7a0100
0x7ffecd7a0108

使用 new 操作符在 C++ 中创建动态内存上的指针向量

另一方面,我们可以利用 new 操作符来创建一个在堆上动态分配的指针向量。

这种解决方案要求程序员在程序退出前释放内存,否则,代码将受到内存泄漏的影响,这在长期运行的应用程序和资源受限的硬件环境中是一个巨大的问题。注意,我们使用 delete [] 符号来清理动态分配向量中的每个位置。

#include <iostream>
#include <vector>

using std::cout;
using std::vector;
using std::endl;

constexpr int WIDTH = 8;

int main()
{
    int *vector_of_pointers = new int[WIDTH];

    cout << "vector_of_pointers addresses:" << endl;
    for(int i = 0; i < WIDTH; ++i) {
        cout << &vector_of_pointers[i] << endl;
    }
    cout << endl;

    delete [] vector_of_pointers;
    return EXIT_SUCCESS;
}

输出:

vector_of_pointers addresses:
0x2561c20
0x2561c28
0x2561c30
0x2561c38
0x2561c40
0x2561c48
0x2561c50
0x2561c58

使用 std::vector 容器在 C++ 中创建指针向量

std::vector 提供了丰富的功能,可以分配一个指针向量,并通过多个内置函数操作向量。该方法为运行时的新元素创建提供了更灵活的接口。注意,我们用 nullptr 值初始化向量元素,如下例所示。

#include <iostream>
#include <vector>

using std::cout;
using std::vector;
using std::endl;

constexpr int WIDTH = 8;

int main()
{
    vector<int *> vector_p(WIDTH, nullptr);

    cout << "vector_p addresses:" << endl;
    for(int i = 0; i < WIDTH; ++i) {
        cout << &vector_p[i] << endl;
    }
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

vector_p addresses:
0x1255c20
0x1255c28
0x1255c30
0x1255c38
0x1255c40
0x1255c48
0x1255c50
0x1255c58

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

本文地址:

相关文章

Arduino 中停止循环

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

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

Arduino 复位

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

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

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 引导加载程序进行编程。

使用 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便