Regarding vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flavourofbru
    New Member
    • May 2007
    • 48

    #1

    Regarding vectors

    Hi,

    Please look at the following code:
    [code=cpp]
    dependencies(in t i)
    {
    string str;
    vector<string> fd;
    cout<<"enter the attributes of fd"<<i<<endl;
    cin>>str;
    fd.push_back(st r);
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    int num;
    cout<<"Enter the number of functionaldepen dencies"<<endl;
    cin>>num;
    for(int i=0; i<num; i++)
    {
    dependencies(i) ;
    }
    return 0;
    }
    [/code]

    In the above code I am calling the function "dependenci es" depending on the given input for number of functional dependencies. In the "dependenci es" function, whatever i am inputting is pushed into the vector. But everytime I call the function, the input that I give gets into the same vector.

    But what i want to do is, everytime I call the function dependencies, it should create a new vector and push that iteration inputs into new vector instead of pushing into the same old vector.

    Is it possible to do that way??

    Please help!!

    Thanks!!
    Last edited by sicarie; Sep 11 '07, 03:26 PM. Reason: Code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by flavourofbru
    dependencies(in t i)
    {
    string str;
    vector<string> fd;
    cout<<"enter the attributes of fd"<<i<<endl;
    cin>>str;
    fd.push_back(st r);
    }
    Very odd function. The vector is a local variable which means it's created anew each time the function is called. The data is put in the vector and then the vector is destroyed. Nothing is returned.

    Also, the function has an implied return int, which is not supported by C++.

    If you want a new vector each time, then create it on the heap and return the address of the vector.

    Comment

    Working...