迹忆客 专注技术分享

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

C++ STL 中的向量容器

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

本文将介绍如何在 C++ 中使用 vector 容器的多种方法。

在 C++ 中使用 std::vector 来构建动态数组

std::vector 是容器库的一部分,它实现了可动态调整大小的数组,该数组将元素存储在连续的存储中。通常在结尾处添加 vector 元素,并带有 push_backemplace_back 内置函数。可以使用初始化程序列表轻松构建 vector 容器,如以下示例代码所示-arr 对象。同样,可以将静态初始化程序列表值存储在宏表达式中,并在需要时简洁地表达初始化操作。

#include <iostream>
#include <vector>

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

#define INIT { "Neque", "porro", "quisquam", "est", "qui", "dolorem", "ipsum", "quia" }

template<typename T>
void printVector(vector<T> &vec)
{
    for (const auto &item : vec) {
        cout << item << ", ";
    }
    cout << endl;
}

int main() {
    vector<string> arr = { "Neque", "porro", "quisquam", "est", "qui", "dolorem", "ipsum", "quia" };
    printVector(arr);

    cout << endl;

    vector<string> arr2 = INIT;
    printVector(arr2);

    cout << endl;

    return EXIT_SUCCESS;
}

在 C++ 中使用 std::vector 存储 struct 对象

std::vector 可以用来存储自定义结构体,并使用大括号列表表示法初始化它们,类似于前面的示例。注意,当使用 push_back 函数添加一个新的 struct 元素时,该值应作为支撑列表传递,并且如果 struct 中有多个嵌套结构,则应使用相应的符号。

#include <iostream>
#include <vector>

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

struct cpu {
    string wine;
    string country;
    int year;
} typedef cpu;

int main() {

    vector<cpu> arr3 = { {"Chardonnay", "France", 2018},
                         {"Merlot", "USA", 2010} };

    for (const auto &item : arr3) {
        cout << item.wine << " - "
             << item.country << " - "
             << item.year << endl;
    }

    arr3.push_back({"Cabernet", "France", 2015});
    for (const auto &item : arr3) {
        cout << item.wine << " - "
             << item.country << " - "
             << item.year << endl;
    }

    return EXIT_SUCCESS;
}

在 C++ 中使用 std::vector::swap 函数交换向量元素

std::vector 提供了多个内置函数来对其元素进行操作,其中之一就是 swap。可以从 vector 对象中调用它,并以另一个 vector 作为参数交换它们的内容。另一个有用的函数是 size,它可以在给定的向量对象中检索当前元素计数。但是请注意,有一个类似的函数叫 capacity,它返回当前分配的缓冲区中存储的元素数量。有时会为后者分配更大的大小,以避免不必要地调用操作系统服务。下面的示例代码演示了两个函数返回不同值的情况。

#include <iostream>
#include <vector>

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

template<typename T>
void printVector(vector<T> &vec)
{
    for (const auto &item : vec) {
        cout << item << ", ";
    }
    cout << endl;
}

int main() {

    vector<int> arr4 = { 1, 12, 45, 134, 424 };
    vector<int> arr5 = { 41, 32, 15, 14, 99 };

    printVector(arr4);
    arr4.swap(arr5);
    printVector(arr4);

    cout << "arr4 capacity: " << arr4.capacity() << " size: " << arr4.size()
         << endl;

    for (int i = 0; i < 10000; ++i) {
        arr4.push_back(i * 5);
    }

    cout << "arr4 capacity: " << arr4.capacity() << " size: " << arr4.size()
         << endl;
    arr4.shrink_to_fit();
    cout << "arr4 capacity: " << arr4.capacity() << " size: " << arr4.size()
         << endl;


    return EXIT_SUCCESS;
}

输出:

1, 12, 45, 134, 424,
41, 32, 15, 14, 99,
arr4 capacity: 5 size: 5
arr4 capacity: 10240 size: 10005
arr4 capacity: 10005 size: 10005

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便