Use a class defined vector to store "paired" data in each element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    Use a class defined vector to store "paired" data in each element

    The exercise from my book reads:
    'Define a class named Name_value that holds a string and a value (int)and a vector named Name_value. Give it a constructor. Store name and value "pairs" in each vector element instead of using two separate vectors... as were
    used in ex 4_19.' The following is the debris left from my many previous attempts. I would appreciate some specific direction on how to proceed as I'm stumped. I have Googled extensively without joy. There is no urgency.
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    
    class Name_value
    {
    public:
           string name;
           int value;
           Name_value (vector<Name_value>v);      
          
          
    };      
    
    int main()
    {
    Name_value  v;           
    
    system("pause");
    return 0;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I think this is a very poorly worded question and I suspect it means

    Define a class named Name_value that holds string and value (int) pairs using a vector named Name_value. Give it a constructor and a copy constructor. Store name and value "pairs" in each vector element instead of using two separate vectors.

    However this would end up just being a very simple wrapper for a vector storing the relevant type, i.e. no real need for this class.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Thanks Banfa..Yes I think you are quite correct with your restatement of the question(but I can`t comment on the need for a copy constructor). Are you saying that it is not possible to write some code that achieves the basic requirement of storing two different (primitive) types under one class name (type) into one vector element?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        No It is possible, I am saying that vector does such a good job there seems little point wrapping another class round the vector.

        Instead of

        Code:
        class Name_value
        {
        public:
            string name;
            int value;
            Name_value (vector<Name_value>v);      
        };
        I personally would have

        Code:
        struct Name_value
        {
            string name;
            int value;
        };
        I know classes and structs are the same thing but I tend to use struct where the data is public especially if there are not methods on the structure (i.e. where a classic C structure can be used).

        Then

        Code:
        std::vector<Name_value> Name_value;
        Appears to solve the question, no need for a wrapper class.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Thanks Banfa. Will do some work using your approach.

          Comment

          Working...