容器类vs类- c++
Container Class vs Class - C++
我是一个编程新手,刚刚遇到这个作业
创建一个包含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的对象。
假设我们想要存储一些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
它们告诉你创建一个类作为数组的容器。在编程中,尤其是刚开始编程时,你会看到许多被称为容器的元素,因为你的老师希望你把变量、类和数组(以及其他编程工具)看作是你存储数据的容器。
当您进入更复杂的概念(如指针)时,以这种方式查看编程可以更容易地将信息概念化。
所以,长话短说,创建一个包含数组的类。如果您正在学习构造函数和重载,请确保根据传入的数据对其进行初始化。如果没有,应该只需要几行代码就可以完成项目。
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 数据成员SFINAE的C++17测试:gcc vs clang
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 正在VS调试器中监视映射条目
- Confusion: decltype vs std::function
- 将IBM Rhapsody模型集成到VS 2019中
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 使用VS Code和CMake Tools运行自定义命令
- 修改 VS Code 中的默认C++代码段
- 如何使用c++在VS 2019上运行SQL查询
- vs 2015 constexpr变量不恒定,但与2019相比还好吗
- 完美前进使用 std::forward vs RefRefCast
- 从VS 2015更新3更新到VS2015更新3 d后浮点计算行为不同的原因
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- consteval wrapper vs. source_location
- VS Code C++:不准确的系统包括路径错误(wchar.h,boost/lambda/lambda.hpp)
- QStringList vs list<shared_ptr<QString>> 性能比较C++
- VS 2017 使用交叉编译器构建 x64 项目