store class objects in vector

what if there are many data members

Page 1 of 1

2 Replies - 2489 Views - Last Post: 01 September 2008 - 01:23 AM Rate Topic: -----

#1 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

store class objects in vector

Posted 31 August 2008 - 08:24 PM

I have been googling looking for a tutorial how to store class objects in a vector (STL container). What I got is only one example that store an object of class that has one member data.

What if I have many member data in the class, is this possible?

Example I got with one member data:
#include <iostream>
#include <vector>
using namespace std;

class Demo {
  double d;
public:
  Demo() { 
     d = 0.0; 
  }
  Demo(double x) { 
     d = x; 
  }

  Demo &operator=(double x) {
    d = x; return *this;
  }

  double getd() { 
    return d; 
  }
};

bool operator<(Demo a, Demo B)/>
{
  return a.getd() < b.getd();
}

bool operator==(Demo a, Demo B)/>
{
  return a.getd() == b.getd();
}

int main()
{
  vector<Demo> v;
  int i;

  for(i = 0; i <10; i++)
    v.push_back(Demo(i/3.0));

  for(i = 0; i <v.size(); i++)
    cout << v[ i ].getd() << " ";

  cout << endl;

  for(i = 0; i <v.size(); i++)
    v[ i ] = v[ i ].getd() * 2.1;

  for(i = 0; i <v.size(); i++)
    cout << v[ i ].getd() << " ";

  return 0;
}
 


I have tried adding more member variables and changing the parameters in the constructors but I got errors.

A class something like this, then its objects are stored in vector.

class Friend
{
    public:
        //constructors and destructor.
        //functions here
        
    private:
        string name;
        int age;
        float height;
};


Is This A Good Question/Topic? 0
  • +

Replies To: store class objects in vector

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4406
  • View blog
  • Posts: 12,264
  • Joined: 18-April 07

Re: store class objects in vector

Posted 31 August 2008 - 09:54 PM

Here is an example building on your code. Keep in mind that it doesn't really matter how many data members a class has, they can be put into a vector just like any other object. Here we created a vector full of your Friend's class.

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

using namespace std;

class Friend
{
    public:
        // Some nice mutator methods
		void setName(string friendName) {
			name = friendName;
		}

		void setAge(int friendAge) {
			age = friendAge;
		}

		// Nice function to print out the Friend
		void printFriendInfo() {
			cout << "Friend's name is: " << name << endl;
			cout << "Friend's age is: " << age << endl;
		}
        
    private:
		// Notice we have tons of member data here
        string name;
        int age;
        float height;
};

int main()
{
   // Create a vector of Friend objects
   vector<Friend> list;
   
   // Setup to friends from DIC (Martyr2 and Capty are awesome)
   Friend f1;
   f1.setName("Martyr2");
   f1.setAge(189);

   Friend f2;
   f2.setName("Capty99");
   f2.setAge(5);

   // Store them in the vector
   list.push_back(f1);
   list.push_back(f2);

   // Now setup an iterator loop through the vector
   vector<Friend>::iterator it;

   for ( it = list.begin(); it != list.end(); ++it ) {
      // For each friend, print out their info
      it->printFriendInfo();
   }

   return 0;
}



Read through the code and see how it works. I created some DIC friends and put them in our vector list. I then loop through the list and call each object's print method to print out their name and age. Oh yes, I am really that old! I am immortal!

Hope this helps you out. Enjoy!

"At DIC we be DIC friends of a lot of people... especially those ladies on the corner of 29th and Broadway!" :snap:
Was This Post Helpful? 1

#3 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Re: store class objects in vector

Posted 01 September 2008 - 01:23 AM

Thank you very much Martyr2. That is what I am looking for.

Actually I am working with my linklist without the use of "struct" keyword, which means evrything will be done in a class. A class for my data and another class as a conatainer. Then I was thinking if it is possible in STL. Now I got it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1