我正在编写一个程序,在其中创建一个包含多维 vector 的实验室。到目前为止,我有一个构造函数,它要求用户输入实验室的数量和大小。在程序中,我创建了一个名为 addLab 的函数,它在指定位置添加一个新实验室,其中包含用户输入的计算机数量。
在测试程序时,我让用户输入 lab 的大小数量为 3,然后我尝试将 lab 5 添加到 vector 中,但我得到以下无法正确编译的输出:
labs: Computer Stations:
1: 1:empty 2:empty 3:empty
2: 1:empty 2:empty 3:empty
3: 1:empty 2:empty 3:empty
Where do you want the lab inserted at?
这是我的头文件
#ifndef __grade_weight_calculator__ComputerLabs__
#define __grade_weight_calculator__ComputerLabs__
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class ComputerLabs{
public:
ComputerLabs();
void show_labs();
void add_lab();
private:
vector<vector<string>> labs;
int numoflab;
int numofcomp;
int lab_numba;
int comp_numba;
};
#endif
这是我的实现cpp文件
#include "ComputerLabs.h"
ComputerLabs::ComputerLabs()
{
cout<<"Input the number of labs"<<endl;
cin>>numoflab;
cout<<"Input the size of the labs"<<endl;
cin>>numofcomp;
for (int i=0;i<numoflab; i++){
vector<string> row;
for (int j=0;j<numofcomp; j++)
row.push_back("empty");
labs.push_back(row);
}
}
void ComputerLabs::show_labs()
{
cout<<"labs: "<<"Computer Stations:"<<endl;
int a=0;
int j;
for (int i=0;i<numoflab; i++){
cout<<a+1<<": ";
j=0;
while(j<numofcomp){
cout<<" ";
cout<<j+1<<":"<<labs[i][j];
++j;
}
a++;
cout<<endl;
}
}
void ComputerLabs::add_lab()
{
cout<<"Where do you want the lab inserted at?"<<endl;
numoflab=5;//this makes it add lab 5
vector<string> row;
for(int i=0;i<numofcomp;i++)
{
row.push_back("empty");
}
labs.insert(labs.begin()+4,row);
}
这是我的 main.cpp 文件:
#include "ComputerLabs.h"
int main()
{
ComputerLabs mycomp;
mycomp.show_labs();
mycomp.add_lab();
mycomp.show_labs();
}
我认为可能导致此问题的另一个错误与 show_labs() 函数有关,就我尝试使用的算法而言,但我不太确定。任何人都可以帮我找出我的问题和解决问题的解决方案吗?
最佳答案
代码中的主要问题出在 add_lab()
函数中。
void ComputerLabs::add_lab()
{
cout<<"Where do you want the lab inserted at?"<<endl;
// PROBLEM LINE
// Not sure why you needed to do that.
// When the initial value of numoflab is 3,
// numoflabs can be changed to 4 but not 5. If you set it
// to 5 here, you run into problem in show_lab() since
// there aren't 5 rows in your 2D vector.
numoflab=5;//this makes it add lab 5
vector<string> row;
for(int i=0;i<numofcomp;i++)
{
row.push_back("empty");
}
labs.insert(labs.begin()+4,row);
}
将该函数更改为:
void ComputerLabs::add_lab()
{
cout<<"Where do you want the lab inserted at?"<<endl;
int pos;
cin >> pos;
// Make sure that the input position is valid.
if ( pos > numoflab )
{
cout << "Invalid position." << endl;
return;
}
// Increment numoflab by 1
++numoflab;
std::vector<std::string> tempRow;
for(int i=0;i<numofcomp;i++)
{
tempRow.push_back("empty");
}
labs.insert(labs.begin()+pos, tempRow);
}
一般改进
您不需要将numoflab
作为成员变量存储在您的类中。它可以从 labs
派生。
我看到您已经更新了代码,row
不再是该类的成员变量。
作为一个好的设计,最好为类提供它的数据,而不是期望类从 stdin
或 cin
获取数据。
这是您程序的更新版本。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class ComputerLabs {
public:
ComputerLabs(int numoflab, int numofcomp);
void show_labs();
void add_lab(int pos);
private:
vector<vector<string>> labs;
int numofcomp;
int lab_numba;
int comp_numba;
};
ComputerLabs::ComputerLabs(int numoflab, int numofcomp) : numofcomp(numofcomp)
{
for (int i = 0; i < numoflab; i++){
std::vector<std::string> row;
for (int j = 0; j < numofcomp; j++)
{
row.push_back("empty");
}
labs.push_back(row);
}
}
void ComputerLabs::show_labs()
{
cout << "labs: " << "Computer Stations:" << endl;
int numoflab = labs.size();
for (int i = 0; i<numoflab; i++){
cout << i+1 << ": ";
for (int j = 0; j < numofcomp; j++)
{
cout<<" ";
cout << j+1 << ":" << labs[i][j];
}
cout<<endl;
}
}
void ComputerLabs::add_lab(int pos)
{
int numoflab = labs.size();
if ( pos < 0 || pos > numoflab )
{
cout << "Invalid position." << endl;
return;
}
std::vector<std::string> row;
for(int i=0;i<numofcomp;i++)
{
row.push_back("empty");
}
labs.insert(labs.begin()+pos,row);
}
int main()
{
int numoflab;
int numofcomp;
cout << "Input the number of labs" << endl;
cin >> numoflab;
cout << "Input the size of the labs" << endl;
cin >> numofcomp;
if (!cin )
{
// Deal with error.
}
ComputerLabs mycomp(numoflab, numofcomp);
mycomp.show_labs();
cout << "Where do you want the lab inserted at?" << endl;
int pos;
cin >> pos;
if (!cin )
{
// Deal with error.
}
mycomp.add_lab(pos);
mycomp.show_labs();
}
更新
如果您希望新添加的实验室的大小与现有实验室的大小不同,您可以执行以下操作:
- 删除作为成员变量的
numofcomp
。 - 向
add_lab
添加另一个参数,numofcomp
。 - 在调用
add_lab
之前,询问用户实验室的组件数量。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class ComputerLabs {
public:
ComputerLabs(int numoflab, int numofcomp);
void show_labs();
void add_lab(int pos, int numofcomp);
private:
vector<vector<string>> labs;
};
ComputerLabs::ComputerLabs(int numoflab, int numofcomp)
{
for (int i = 0; i < numoflab; i++){
std::vector<std::string> row;
for (int j = 0; j < numofcomp; j++)
{
row.push_back("empty");
}
labs.push_back(row);
}
}
void ComputerLabs::show_labs()
{
cout << "labs: " << "Computer Stations:" << endl;
int numoflab = labs.size();
for (int i = 0; i<numoflab; i++){
cout << i+1 << ": ";
int numofcomp = labs[i].size();
for (int j = 0; j < numofcomp; j++)
{
cout<<" ";
cout << j+1 << ":" << labs[i][j];
}
cout<<endl;
}
}
void ComputerLabs::add_lab(int pos,
int numofcomp)
{
int numoflab = labs.size();
if ( pos < 0 || pos > numoflab )
{
cout << "Invalid position." << endl;
return;
}
std::vector<std::string> row;
for(int i=0;i<numofcomp;i++)
{
row.push_back("empty");
}
labs.insert(labs.begin()+pos,row);
}
int main()
{
int numoflab;
int numofcomp;
cout << "Input the number of labs" << endl;
cin >> numoflab;
cout << "Input the size of the labs" << endl;
cin >> numofcomp;
if (!cin )
{
// Deal with error.
}
ComputerLabs mycomp(numoflab, numofcomp);
mycomp.show_labs();
cout << "Where do you want the lab inserted at?" << endl;
int pos;
cin >> pos;
cout << "Input the size of the lab" << endl;
cin >> numofcomp;
if (!cin )
{
// Deal with error.
}
mycomp.add_lab(pos, numofcomp);
mycomp.show_labs();
}
关于c++ - 将元素插入多维 vector 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30946461/