首页 > 解决方案 > 对向量来说是全新的。向量下标超出范围

问题描述

对于我的任务,我应该取一组姓名和号码并将它们放入向量中,然后将它们用作电话簿。值得一提的是,我正在将代码从原始版本转换为每个分配使用数组的代码。现在,不幸的是,我的教授提供的讲座只涵盖了一些绝对基础知识,几乎没有展示任何细节,所以我不得不去挖掘大部分内容。我知道错误可能与向量的大小有关,但我不知道我做错了什么或如何修复它。

int main()
{
    //variables
    string eleName;
    int elePhone;
    char choice;
    vector<string> name;
    name.push_back(string(10, 10));
    name = { "Andrico", "Bonnie", "Charles",
                        "Debbie", "Enrique", "Felicia" };
    vector<int> phone;
    phone.push_back(10);
    phone = { 5551234,
                        5555678,
                        5552468,
                        5551379,
                        5559876,
                        5554321 };



    do
    {
        //call function showPhoneBook
        showPhoneBook(name, phone, MAX);

        //get user request
        cout << "\n\nWho\'s phone number do you want to see?" << endl;
        cout << "remember, type \'e\' for edit and \'x\' for exit, otherwise just type the number of the person." << endl;
        cin >> choice;

        //call function toChoice
        elePhone = toChoice(choice);
        if (elePhone != -1)
        {
            cout << "The phone number for " << name[elePhone] << " is " << phone[elePhone] << ".\n\n\n\n\n\n";
        }
        if (choice == 'e')
        {
            int choice1;
            cout << "Which entry do you want to edit?  ";
            cin >> choice1;
            editNum(name, phone, MAX, choice1);
        }
    } while (choice != 'x');

    return 0;
}

void showPhoneBook(vector<string> N, vector<int> P, int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << i << "  " << N[i] << "\t" << P[i] << endl;
        cout << "Size of the phone book " << N.size() << endl;

    }
    return;
}

int toChoice(char c)
{
    int num=0;
    if (c != 'e' && c != 'x')
    {
        switch (c)
        {
        case '0':
            num = 0;
            return num;
        case '1':
            num = 1;
            return num;
        case '2':
            num = 2;
            return num;
        case '3':
            num = 3;
            return num;
        case '4':
            num = 4;
            return num;
        case '5':
            num = 5;
            return num;
        case '6':
            num = 6;
            return num;
        case '7':
            num = 7;
            return num;
        case '8':
            num = 8;
            return num;
        case '9':
            num = 9;
            return num;

        }

    }
    else
        return -1;
    return num;
}

void editNum(vector<string> N, vector<int> P, int size, int c1)
{
    string name;
    int number;
    cout << "What is the name for entry " << c1 << "?:  ";
    cin >> name;
    cout << "What is the number for entry " << c1 << "?:  ";
    cin >> number;

    N[c1] = name;
    P[c1] = number;
    return;
}

标签: c++vector

解决方案


vector<int> phone;
    phone.push_back(10);
    phone = { 5551234,
                        5555678,
                        5552468,
                        5551379,
                        5559876,
                        5554321 };

phone.push_back(10) 实际上就像 phone[0] = 10;
我认为您打算使用
“phone.resize(10);”,这意味着“电话”的大小为 10。


推荐阅读