尽管有push_back调用,C++ Sizeless vector仍不能存储数据

xuo3flqw  于 2023-05-02  发布在  其他
关注(0)|答案(2)|浏览(116)

我一直有一些问题与向量,特别是那些没有大小。我正在制作一个程序,以添加汽车,和他们的价格到一个列表,并无法获得添加数据功能正常工作。当打印数据时,它不打印任何东西,矢量本身是空的,因为当试图删除数据时,它无法找到我输入的项目。下面是我的代码:
主要:

int main (void) {
    vector<Car> carVect;
    int input = 0;

    do{
        displayMenu();
        cin >> input;
        if (input == 1){ 
            addData(carVect);
        } else if (input == 2){ 
            deleteData(carVect);
        } else if (input == 3){
            printData(carVect);
        } else if (input == 4){
            break; // breaks out of loop
        } else {
            cout << "invalid selection" << endl;
        }
    }
    while (input != 4);
}

addData函数:

bool addData(vector<Car>& carVect){
    Car carObj;
    cout << "Make of car, and price of car, separated by spaces: ";
    cin >> carObj.carMake >> carObj.carPrice; 
    for (int i = 0; i < carVect.size(); i++) { 
        if (carVect[i].getCarMake().empty()){ 
            carVect.push_back(carObj); 
            cout << "Car Entry Added" << endl;
            return true;
        } 
    }
        cout << "There are no open Elements in the array" << endl;
}

头文件:

#include <iostream>
#include <string>
using namespace std;

class Car {
    public:
    string carMake;
    int carPrice;

    Car (string make,int price) {
        carMake = make;
        carPrice = price;
    }
    Car () { //default contructor

    }
    string getCarMake () {
        return carMake;
    }
    int getCarPrice () {
        return carPrice;
    }
    void setCarMake (string x){
        carMake = x;
    }
};

我知道vector没有大小,但我认为addData中的第一个if语句将为true,并且push_back将为它提供一个包含数据的元素。任何帮助将不胜感激。

vnzz0bqm

vnzz0bqm1#

您的vector最初是空的,即它根本没有Car对象。
addData()中,你的循环正在检查向量的size(),它将为0,所以循环体将永远不会被输入,因此将无法找到任何“空”的Car(不存在),因此根本不会将新的carObj推入vector
只要完全摆脱循环,它对你没有任何好处,例如:

bool addData(vector<Car>& carVect){
    Car carObj;
    cout << "Make of car, and price of car, separated by spaces: ";
    if (cin >> carObj.carMake >> carObj.carPrice) { 
        carVect.push_back(carObj); 
        cout << "Car Entry Added" << endl;
        return true;
    }
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Bad input!" << endl;
    return false;
}

如果出于某种原因,你必须使用循环,那么你必须用默认的Car对象 * 预填充 * vector,例如:

const int MaxCars = ...; // <-- desired count here
vector<Car> carVect(maxCars);

然后你的循环将正确运行,并且能够找到“empty”Car s。然而,当你找到一个“空”的Car时,你是在把新的carObj推到向量的末尾,而不是替换你找到的“空”的Car。因此,在对addData()的后续调用中总是会找到相同的“空”Car。所以,你需要摆脱push_back()来解决这个问题,例如:

bool addData(vector<Car>& carVect){
    for (int i = 0; i < carVect.size(); i++) {
        if (carVect[i].getCarMake().empty()){ 
            Car carObj;
            cout << "Make of car, and price of car, separated by spaces: ";
            if (cin >> carObj.carMake >> carObj.carPrice) {
                //carVect.push_back(carObj);
                carVect[i] = carObj;
                cout << "Car Entry Added" << endl;
                return true;
            }
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Bad input!" << endl;
            return false;
        } 
    }
    cout << "There are no open Elements in the array" << endl;
    return false;
}
qhhrdooz

qhhrdooz2#

向量的起始大小为0,其中包含0个元素。您正在遍历空的汽车列表,并检查名称是否为空,然后向列表中添加一辆新车。因为列表是空的,所以它永远不会在列表中找到空字符串。相反,您应该只将Car对象添加到向量中,而不对其进行循环。如果你想拥有最多的汽车,你也可以这样做。

bool addData(vector<Car>& carVect){
    if (carVect.size() >= 10) {
        cout << "You cant have more than 10 cars!" << endl;
        return false;
    }
    
    Car carObj;
    
    cout << "Make of car, and price of car, separated by spaces: ";
    cin >> carObj.carMake >> carObj.carPrice; 

    carVect.push_back(carObj);
    cout << "Car Entry Added" << endl;

    return true;
}

有一种方法可以修复你的原始代码,但我不确定它是否是你的意图:

int main (void) {
    vector<Car> carVect;
    carVect.resize(10);
    ....
}

// inside addData replace
carVect.push_back(carObj);
// with:
carVect[i] = carObj;

现在您可以使用原始方法添加最多10辆汽车。

相关问题