appending string or int to vector variable

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

    appending string or int to vector variable

    Hi,

    Is it possible to add an int or a string or a char to a vector variable??
    For example if I declare a vector variable as follows:
    vector<string> abc;

    Can I append an integer value or some string value to the vector variable such that every time I call a function that has this declaration, it creates a new vector variable abc appened with the int or string value?

    Is there any other method by which I can do above operation

    Please help.

    Thanks,
    Rishi
  • alind
    New Member
    • Sep 2007
    • 7

    #2
    i dont think tht u can append the vector variable like tht
    but if i m able to guess ur functionality then u can be better using array of vectors or vector of vectors. Then u can access the sub(contained) vectors like a[0], a[1] and so on. If u can clarify more on ur problem (or requirement) then may be we can help further.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      If I understand you correctly, you should be able to write a function template to do that:

      [code=cpp]
      template< typename T>
      vector<T>* MyFunction(T& arg)
      {
      vector<T>* temp = new vector<T>;
      temp->push_back(arg) ;
      return temp;
      }
      int main()
      {
      int i = 30;
      vector<int>* val = MyFunction(i);
      string str("Hello");
      vector<string>* val1 = MyFunction(str) ;
      }
      [/code]

      If I misunderstand, my apologies.

      Comment

      • flavourofbru
        New Member
        • May 2007
        • 48

        #4
        Hi,
        My exact problem is clearly defined below:
        Please look at the following code:

        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;
        }


        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!!

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          Take a close look at your dependencies function. Each time you call it, it will create a new and "local" vector of strings. Since it is local, each time you leave dependencies, the vector and its values are deleted. I doubt this is what you want.

          Now take a look at MyFunction in post #3. It creates a new vector on the "heap" each time it is called. It returns a pointer to that vector so that the calling program can use the vector. I think this is what you want.

          Add your loop to MyFunction or add the vector creation to dependencies and return the new vector.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            flavourofbru,

            I noticed you had posted a new thread containing this very same question. Please limit your question to one thread in the future - there's no need to have 2 discussions happening at once on the same subject!

            Thanks a lot.

            Comment

            Working...