[英]Inserting an element into multi dimension vector errors
我正在寫一個程序,用多維向量創建一個實驗室。 到目前為止,我有一個構造函數,要求用戶輸入實驗室的數量和大小。 在該程序中,我正在創建一個名為addLab的函數,該函數將在指定位置添加一個新實驗室,其中包含用戶輸入的計算機數量。
在測試程序時,我讓用戶輸入Lab的大小數為3,然后嘗試將Lab 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()函數有關,但我不太確定。 誰能幫我弄清楚我的問題及其解決方案?
代碼中的主要問題是在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();
}
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
的大小都會增加,但永遠不會清除(程序末尾除外)。 如果打印整個行而不是僅打印三行,則會得到以下信息:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.