引用的向量不通过函数

时间:2010-05-11 18:02:56

标签: c++ vector reference

函数的引用向量不会将信息保存在内存中。我必须使用指针吗?

感谢。

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

void menu();
void addvector(vector<string>& vec);
void subvector(vector<string>& vec);
void vectorsize(const vector<string>& vec);
void printvec(const vector<string>& vec);
void printvec_bw(const vector<string>& vec);

int main()
{
    vector<string> svector;

    menu();

    return 0;
}
//functions definitions

void menu()
{
    vector<string> svector;
    int choice = 0;

        cout << "Thanks for using this program! \n"
             << "Enter 1 to add a string to the vector \n"
             << "Enter 2 to remove the last string from the vector \n"
             << "Enter 3 to print the vector size \n"
             << "Enter 4 to print the contents of the vector \n"
             << "Enter 5 ----------------------------------- backwards \n"
             << "Enter 6 to end the program \n";
        cin >> choice;

        switch(choice)
        {

                case 1:
                    addvector(svector);
                    menu();
                    break;
                case 2:
                    subvector(svector);
                    menu();
                    break;
                case 3:
                    vectorsize(svector);
                    menu();
                    break;
                case 4:
                    printvec(svector);
                    menu();
                    break;
                case 5:
                    printvec_bw(svector);
                    menu();
                    break;
                case 6:
                    exit(1);
                default:
                    cout << "not a valid choice \n";

            // menu is structured so that all other functions are called from it.
        }

}

void addvector(vector<string>& vec)
{
    //string line;

     //int i = 0;
        //cin.ignore(1, '\n');
        //cout << "Enter the string please \n";
        //getline(cin, line);
        vec.push_back("the police man's beard is half-constructed");    

}

void subvector(vector<string>& vec)
{
    vec.pop_back();
    return;
}

void vectorsize(const vector<string>& vec)
{
    if (vec.empty())
    {
        cout << "vector is empty";
    }
    else
    {
        cout << vec.size() << endl;
    }
    return;
}

void printvec(const vector<string>& vec)
{
    for(int i = 0; i < vec.size(); i++)
    {
        cout << vec[i] << endl;
    }

    return;
}

void printvec_bw(const vector<string>& vec)
{
    for(int i = vec.size(); i > 0; i--)
    {
        cout << vec[i] << endl;
    }

    return;
}

5 个答案:

答案 0 :(得分:5)

你的问题是每次调用menu()都会创建一个隐藏前一个向量的新向量,这就是为什么你觉得它们似乎是空的。如果你真的想以递归方式调用菜单,请将它传递给你在main中创建的向量引用。

所有这一切,菜单系统很少会递归。您可能需要循环调用main中的menu()循环,直到用户选择退出为止。

答案 1 :(得分:4)

您的menu函数是递归的。

这意味着menu的每次新调用都会创建自己的vector,并在完成后将其丢弃。

如果您想重复使用svector,则需要为菜单使用普通循环,或者将svector中创建的原始main传递给{{ 1}}使用pass-by-reference参数。

答案 2 :(得分:1)

问题是你调用另一个menu()在堆栈上分配新的svector(在内存的某些部分)。 可能原始代码是void menu(vector<string>&svector)

答案 3 :(得分:0)

您需要让svector成为全局(在任何函数之外声明)或将其作为参数传递给menu。 C ++没有函数的动态范围。

编辑:你也可以将所有这些包装在一个课程中并反复拨打menu()

答案 4 :(得分:0)

svector是共享的(是s代表什么?),并且必须将C ++中不同作用域之间共享的变量声明为extern,或者获得两个单独的局部变量

int main()
{
    extern vector<string> svector;
    …


void menu()
{
    extern vector<string> svector;
    …

开玩笑吧。 svector应该是一个论点。或者全球化就足够了。但是不要使用像这样的全局变量。