如何完全擦除数组并用向量替换它?(How do i completely erase an array and replace it with a vector?)

编程入门 行业动态 更新时间:2024-10-26 23:25:58
如何完全擦除数组并用向量替换它?(How do i completely erase an array and replace it with a vector?)

我已经为我的班级工作了大约一天,我已经完成了几乎所有事情。 我唯一缺少的是删除我的数组并用向量替换它。

#ifndef MENU #define MENU #include <vector> const int MAXCOUNT = 20; struct menuItem { void(*func)(); char decript[50]; }; class Menu { private: //vector <int> v; i tried replacing the "mi"'s in the menu.cpp file with v.push_back but i kept getting pointer errors menuItem mi[MAXCOUNT]; int count = 0; void runSelection(); public: Menu(); void addMenu(char *Description, void(*f)()); void runMenu(); void waitKey(); }; #endif

这是cpp文件。 我试图用v替换数组mi。我知道我错过了一些东西,但我无法解决它,所以我只是发布它使用数组。

Menu::Menu() :count(0) { } void Menu::addMenu(char *Description, void(*f)()) { if (count < MAXCOUNT) { this->mi[count].func = f; strcpy(this->mi[count].decript, Description); count++; } } void Menu::runMenu() { for (;;) { system("CLS"); for (int i = 0; i < count; i++) { cout << this->mi[i].decript << endl; } runSelection(); } } void Menu::waitKey() { cout << "Press any key to continue" << endl; while (!_kbhit()); fflush(stdin); } void Menu::runSelection() { int select; cin >> select; if (select <= count) this->mi[select - 1].func(); }

I've been working on this assignment for my class for about a day and I've completed just about everything. The only thing I'm missing is removing my array and replacing it with a vector.

#ifndef MENU #define MENU #include <vector> const int MAXCOUNT = 20; struct menuItem { void(*func)(); char decript[50]; }; class Menu { private: //vector <int> v; i tried replacing the "mi"'s in the menu.cpp file with v.push_back but i kept getting pointer errors menuItem mi[MAXCOUNT]; int count = 0; void runSelection(); public: Menu(); void addMenu(char *Description, void(*f)()); void runMenu(); void waitKey(); }; #endif

This is the cpp file. Im trying to replace the array mi with v. I know im missing something but I can't figure it out so I'm just posting it working with an array.

Menu::Menu() :count(0) { } void Menu::addMenu(char *Description, void(*f)()) { if (count < MAXCOUNT) { this->mi[count].func = f; strcpy(this->mi[count].decript, Description); count++; } } void Menu::runMenu() { for (;;) { system("CLS"); for (int i = 0; i < count; i++) { cout << this->mi[i].decript << endl; } runSelection(); } } void Menu::waitKey() { cout << "Press any key to continue" << endl; while (!_kbhit()); fflush(stdin); } void Menu::runSelection() { int select; cin >> select; if (select <= count) this->mi[select - 1].func(); }

最满意答案

最简单的方法应该是更换

menuItem mi[MAXCOUNT];

std::vector<menuItem> mi;

并在您的构造函数中正确初始化它

Menu::Menu() :count(0) ,mi(MAXCOUNT) { }

假设你之前有工作代码,这应该无缝地替换原始数组。


使用当前的c ++标准,您甚至可以简单地使用

std::array<menuItem,MAXCOUNT> mi;

这甚至不需要在构造函数中初始化。

The simplest way should be to replace

menuItem mi[MAXCOUNT];

with

std::vector<menuItem> mi;

and initialize it in your constructor properly

Menu::Menu() :count(0) ,mi(MAXCOUNT) { }

Supposed you had working code before, this should replace the raw array seamlessly.


With the current c++ standard you can even simply use

std::array<menuItem,MAXCOUNT> mi;

this wouldn't even require initialization in the constructor.

更多推荐

本文发布于:2023-07-17 04:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   数组   擦除   completely   vector

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!