容器类vs类- c++

Container Class vs Class - C++

本文关键字:c++ vs 容器类      更新时间:2023-10-16

我是一个编程新手,刚刚遇到这个作业

创建一个包含20个person对象的数组的容器类族。

我一直在网上和我的书中寻找,但我仍然不能弄清楚容器类和c++中的类之间的区别。

我怎么能同时创建一个家庭类和20个人对象?

"容器类"不是什么官方术语;只是"class"这个词旁边有一个英文描述词。作业要求你创建一个类,其中包含一些其他的东西;即一个包含20个person对象的数组。

在最基本的情况下,结果可以像这样简单:
class family
{
public:
   person people[20];
};

在现实生活中,您可能会这样做:

#include <array>
using family = std::array<person, 20>;

似乎不太可能每个家庭(甚至大多数家庭)都有20个人,所以我个人最终会这样做:

#include <vector>
std::vector<person> family;

白马王子;并适当地操作向量

c++是一种鼓励你"封装"的面向对象语言。第一步是根据概念的数据值和可在该数据上执行的操作将概念分组为对象。

因此可以定义一个Account类,它由id和balance以及存取款功能组成。这个定义形成了类型,但是当你"实例化"(创建一个实例)这个类型时,也就是

Account a;

则变量a指的是一个类型为Account对象

有时候你需要一个类来存储和跟踪其他类型的对象。这就是所谓的"容器类",通常它结合了store和count。

假设我们想要存储一些float。我们可以这样写:

float store[64];
std::cout << "Enter the first number: ";
std::cin >> store[0];

但是我们如何跟踪我们有多少浮动呢?我们可能需要一个计数器,

float store[64];
int stored = 0;
std::cout << "Enter the first number: ";
std::cin >> store[0];
stored++;
std::cout << "Enter the second number: ";
std::cin >> store[1];
stored++;

这是可行的,并不是非常困难,但是如果你正在编写一个函数,期望取一个store和它的大小,你该如何表达呢?

void myFunction(std::string label, float* store, int count);

这需要两个参数,并不是很明确。

c++是关于封装的:这个带有内容计数的"store"的想法可以封装到一个类中:

struct Store {
    float store_[64] {};
    int count_ {0};
};

这是容器。现在可以编写函数,该函数接受一个对象,该对象包含带有单个形参的其他值:

void myFunction(std::string label, Store& store);  // & here = by reference

如果这是'C',你会写代码直接操作存储中的值:

store.store_[N] = 1;
store.count_++;

但是那很乱,我们没有检查有空间。在c++中,可以用成员函数将其封装到类描述中,并隐藏成员变量,以便通过禁止的接口来操作数据。

#include <iostream>
class Store {
    enum { MaxCount = 64 };
    float store_[MaxCount] {};
    size_t count_ = 0;
public:
    // return the maximum number of elements we can store
    size_t capacity() const { return MaxCount; }
    // true/false: is the store empty?
    bool empty() const { return count_ == 0; }
    // return the current count
    size_t size() const { return count_; }
    bool add(float value) {
        if (count_ >= capacity()) {
            std::cerr << "store is full!n";
            return false;
        }
        store_[count_] = value;
        ++count_;
    }
    // reset
    void clear() {
        count_ = 0;  // we don't actually need to change the store
    }
    // allow array-like usage
    const float& operator[](size_t index) const { return store_[index]; }
    float& operator[](size_t index) { return store_[index]; }
    // provide bounds-checked array-ish access
    float at(size_t index) const {
        if (index >= count_)
            throw std::invalid_argument("array index out of bounds");
        return store_[index];
    }
};
int main() {
    Store store;
    for (size_t i = 0; i < store.capacity(); ++i) {
        std::cout << "Enter number #" << i << " or -ve to stop: " << std::flush;
        float f = -1;
        std::cin >> f;
        std::cout << "n" << f << "n";
        if (f < 0)
            break;
        store.add(f);
    }
    std::cout << "You entered " << store.size() << " values:";
    for (size_t i = 0; i < store.size(); ++i) {
        std::cout << ' ' << store[i];
    }
    std::cout << 'n';
}

实时演示:http://ideone.com/boE3Ki

它们告诉你创建一个类作为数组的容器。在编程中,尤其是刚开始编程时,你会看到许多被称为容器的元素,因为你的老师希望你把变量、类和数组(以及其他编程工具)看作是你存储数据的容器。

当您进入更复杂的概念(如指针)时,以这种方式查看编程可以更容易地将信息概念化。

所以,长话短说,创建一个包含数组的类。如果您正在学习构造函数和重载,请确保根据传入的数据对其进行初始化。如果没有,应该只需要几行代码就可以完成项目。