我正在编写一个程序,我正在创建一个具有多维向量的实验室。到目前为止,我有一个构造函数,要求用户输入实验室的数量和大小。在程序中,我创建了一个名为addLab的函数,该函数在指定位置添加一个新实验室,用户输入的计算机数量。
在测试程序时,我让用户输入实验室的大小为3,然后我尝试将实验室5添加到向量中,但是我得到以下输出,它没有正确编译:
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()函数,但我并不完全确定。任何人都可以帮我弄清楚我的问题和解决方法吗?
答案 0 :(得分:1)
代码中的主要问题在于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();
}
答案 1 :(得分:0)
numoflab = 5; // number of labs increased by 2 (from 3)
for(int i = 0; i < numofcomp; i++)
{
row.push_back("empty");
}
labs.insert(labs.begin() + 3, row); // you added only one lab (or row), (the program expects one more to 5)
您必须再插入一行,例如再次执行labs.insert(labs.begin() + 4, row)
。 (labs.begin() + 3
或labs.begin() + 4
也完全有效。)
您的代码中也有一个非常愚蠢的错误,它使用vector<string> row
作为成员。现在,每次push_back
,row
的大小增加,但它从未被清除(除非在程序结束时)。如果你打印的是整行而不是3行,你就会得到这个:
labs: Computer Stations:
1: 1:empty 2:empty 3:empty
2: 1:empty 2:empty 3:empty 4:empty 5:empty 6:empty
3: 1:empty 2:empty 3:empty 4:empty 5:empty 6:empty 7:empty 8:empty 9:empty
非常糟糕。您不需要row
成为成员,您可以将其设置为本地(在您插入行的每个函数中创建并销毁它)。在ComputerLabs
构造函数中,它甚至可以在外部循环的范围内:
for (int i = 0; i < numoflab; i++) {
vector<string> row; // constructed every iteration
for (int j = 0;j < numofcomp; j++)
row.push_back("empty");
labs.push_back(row); // row is copied
// row is destroyed
}