我如何完全擦除数组并将其替换为矢量
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();
}
最简单的方法应该是替换
menuItem mi[MAXCOUNT];
std::vector<menuItem> mi;
并在构造函数中正确初始化
Menu::Menu()
:count(0)
,mi(MAXCOUNT)
{
}
假设您之前有工作代码,这应该可以无缝地替换原始数组。
在当前的c++标准中,您甚至可以简单地使用
std::array<menuItem,MAXCOUNT> mi;
甚至不需要在构造函数中初始化
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 如何用转义符替换字符串中的所有特殊字符
- 如何找到大小'x'数组是否完全填充,在C++?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 类模板的成员功能的定义在单独的TU中完全专业化
- Python中的for循环与C++有何不同
- 是否应该在模板化代码中完全避免const
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 有没有办法知道Tracer是否成功地完全连接到了jaegerclientcpp中的jaeger后端服务器
- 在一个读写器队列中,我可以用volatile替换原子吗
- 用符号版本替换对函数的所有调用
- 从类型std::函数传递变量失败,尽管调用方期望的类型完全相同
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 替换基于地图的所有引用
- 按平均值替换数组中的元素
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- 将方法传递给方法并替换(几乎完全相同)重复代码
- 当完全替换一个函数时,有可能得到一个清晰的gitdiff吗
- 试图替换C++字符串中的字符,完全被卡住了
- 我如何完全擦除数组并将其替换为矢量