迹忆客 专注技术分享

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

在 C++ 中从指向向量的指针访问成员函数

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

本文将介绍几种在 C++ 中如何从指向向量的指针访问成员函数的方法。

使用 -> 记法从指针访问向量的成员函数

-> 运算符可以从指向向量的指针调用 vector 成员函数。在这种情况下,我们是把指向向量的指针传递给了一个不同的函数。作为成员函数,结构体/类的任何数据成员都需要使用 -> 符号来访问。需要注意的是,将对象的指针传递给不同的函数时,应在变量前使用运算符的地址(&)。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> *vec)
{
    for (auto i = 0; i < vec->size(); ++i) {
        cout << vec->at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<string> str_vec = {"bit", "nibble",
                              "byte", "char",
                              "int", "long",
                              "long long", "float",
                              "double", "long double"};

    printVectorElements(&str_vec);

    return EXIT_SUCCESS;
}

输出:

bit; nibble; byte; char; int; long; long long; float; double; long double;

使用 (*)vector.member 符号从指向向量的指针访问成员函数

访问指针所指向的值时,使用了解引用(dereference)操作,可以在功能上替代 -> 运算符,提高可读性。(*vec).at(i) 表达式基本上也可以完成成员访问的操作。注意,我们使用的是可以打印通用类型元素的向量的函数模板。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> *vec)
{
    for (auto i = 0; i < (*vec).size(); ++i) {
        cout << (*vec).at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<string> str_vec = {"bit", "nibble",
                              "byte", "char",
                              "int", "long",
                              "long long", "float",
                              "double", "long double"};

    printVectorElements(&str_vec);

    return EXIT_SUCCESS;
}

输出:

bit; nibble; byte; char; int; long; long long; float; double; long double;

另外,假设向量的指针是用 new 函数调用分配的,而变量没有传递给不同的函数。在这种情况下,可以选择上述任何一种符号。在下面的例子中,我们用 -> 运算符来演示向量元素的迭代和输出操作。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> *vec)
{
    for (auto i = 0; i < vec->size(); ++i) {
        cout << vec->at(i) << "; ";
    }
    cout << endl;
}

int main() {
    auto *i_vec = new vector<int>(10);
    printVectorElements(i_vec);
    for (int i = 0; i < 10; ++i) {
        i_vec->at(i) = i + 1;
        cout << i_vec->at(i) << "; ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

0; 0; 0; 0; 0; 0; 0; 0; 0; 0;
1; 2; 3; 4; 5; 6; 7; 8; 9; 10;

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便