Using Vector for Members of a Class

This is a discussion on Using Vector for Members of a Class within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to make a very simple program that declares a new type of class, that contains a vector. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Question Using Vector for Members of a Class

    Hi,

    I'm trying to make a very simple program that declares a new type of class, that contains a vector. The idea is that I will be able to make objects of this class for different chat bots, that are just a name, and then a list of numbers.

    Anyway, after scouring numerous forums I've been able to figure out how to declare the vector. But I can't figure out how to do anything with it. Based on my code (below), can someone give me some example code for how to:

    1. put stuff into the vector inside the class.
    2. access that stuff?
    3. if I used a list of these objects, would I access them the same way?

    None of the tutorials I've seen deal with using vectors inside a class, so I would really appreciate any help. Thanks.

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class cretin {
    public:
    	cretin() : d_v(3) {};
    private:
    	std::vector<int> d_v;
    	vector<int>::iterator Iter;
    };
    
    int main() {
    	return 0;
    }

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    1. Vector::push_back to add to the end of the vector. (Check your help file for all members of std::vector)
    2. Either use std::vector::iterator or you can access a vector just like an array using subscripting.
    3. Yes.

    Code:
    std::vector<int> d_v;
    vector<int>::iterator Iter;
    Stay consistent.

    Code:
    std::vector<int> d_v;
    std::vector<int>::iterator Iter;
    And since you are doing this:
    Code:
    using namespace std;
    The std:: scope resolution is not needed in this module. Choose one way but don't mix them as it gets confusing in larger programs. I don't recommend bringing the entire std namespace into scope either unless you really have to.
    Last edited by VirtualAce; 12-01-2007 at 11:37 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Before we talk about using a vector member, do you know how to use vectors? If you don't, have you read tutorials/references such as cppreference.com's entry on vectors?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Thanks for your advice guys. I now understand enough to get the following going, which is what I wanted.

    For something like the following code, for some reason I thought I would be able to access the stuff inside the class with say, ben->status[1] or something like that. Evidently that's not how it works.

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class cretin {
    public:
    	cretin() : status() {};
    
    void cretin::setstatus(int a)
    {	
    	status.push_back(a);
    }
    
    void cretin::getstatus()
    {
    	for(int x=0; x<status.size(); x++) 
        {
            cout<<status[x]<<" ";
        };
    }
    
    private:
    	vector<int> status;
    	vector<int>::iterator Iter;
    };
    
    int main()
    {
    	cretin ben;
    	ben.setstatus(3);
    	ben.getstatus();
        return 0;
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you set the size of status, you will have a vectore with zero [or whatever the default value is for the type/class your vector consists of] contents in the size number of elements.

    Something like:
    Code:
    cretin() : status(10) {};
    will give you a ten element vector.

    If you want to set elements to a specific value, you can do:
    Code:
    cretin() : status(10, 7) {};
    This makes a vector of ten elements, all with a value of 7.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you know how to use vectors in general, using them inside a class is not really any different than any other type like int or string.

    Are you confused about how to use the iterator? Think of the iterator like the index of a for loop. For example, if you have an array, you might loop with for (int i = 0; i < size; ++i), right? You wouldn't make i a member variable of your class, you'd just declare it in your function.

    The same thing is true of the iterator. Don't declare it as a member variable. If you need to iterate over your vector in one of your member functions, just make a local iterator variable there to do that.

    All you need for the vector in your class is to declare it as a member variable like you have done, then in the constructor or other member functions use the vector's functions to work with it like the tutorials show.
    Last edited by Daved; 12-01-2007 at 02:26 PM.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Thanks for that, matsp, that's really useful because I saw those two numbers in the brackets in a tutorial and was wondering about them.

    Dave I get your general point but in the second paragraph speaking of the iterator you've put "Don't declare it as a member variable" and then in the last paragraph you've put do declare it as one. I'm guessing this is a typo.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bengreenwood View Post
    Thanks for that, matsp, that's really useful because I saw those two numbers in the brackets in a tutorial and was wondering about them.

    Dave I get your general point but in the second paragraph speaking of the iterator you've put "Don't declare it as a member variable" and then in the last paragraph you've put do declare it as one. I'm guessing this is a typo.
    No, the last paragraph talks about declaring the VECTOR as part of your class.

    The iterator shouldn't be part of the class any more than for example a "int i" is part of the class if you use it to iterate through an array that is part of the class.

    A class should ALWAYS contain the minimum of members necessary to do it's job. This means, an iterator should be local to the function that iterates over the vector, not part of the class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> then in the last paragraph you've put do declare it as one. I'm guessing this is a typo.
    I was referring to the vector, not the iterator. Not a typo, but I should have been more clear.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Thanks for your help with this but now I'm having trouble with using the vector as part of a list! The following code doesn't work and I don't understand why.

    Code:
    #include <iostream>
    #include <vector>
    #include <list>
    using namespace std;
    
    class cretin {
    
    private:	
    	string name;
    	int id;	
    	vector<int> status;
    public:
    	cretin() : status(4, 1) {};
    	cretin(const string& nm, int i)
    	{
    		name = nm;
    		id = i;
    	}
    	string getname()	{ return name; };
    	int getid()				{ return id; };
    
    void cretin::getstatus(int x)
    {
            cout<<status[x]<<" ";
    }
    };
    
    int main()
    {
    	list<cretin> cretinlist;
    	cretinlist.push_back(cretin("Arnold",1));
    	list<cretin>::iterator iter2 = cretinlist.begin();
    	iter2->getstatus(1);
        return 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    The problem is that you have two constructors that initialise rather differently. The default constructor initialises status to have 4 elements, all 1. The other constructor lets status be default initialised.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Okay, I understand, thanks. How do I construct the vector as a member variable of the class with the other stuff then? I tried the following but it doesn't work. It compiles with just the constructor but not when I try to create a cretin object.

    Code:
    #include <iostream>
    #include <vector>
    #include <list>
    #include <string>
    using namespace std;
    
    class cretin {
    
    private:	
    	string name;
    	int id;	
    	vector<int> status;
    public:
    	cretin(const string& nm, int i, vector<int> s)
    	{
    		name = nm;
    		id = i;
    		status = s;
    	}
    
    void cretin::getstatus(int x)
    {
            cout<<status[x]<<" ";
    }
    };
    
    int main()
    {
    	list<cretin> cretinlist;
    	cretin arnold("arnold", 1, (1, 1));
    	cretinlist.push_back(arnold);
    	list<cretin>::iterator iter2 = cretinlist.begin();
    	iter2->getstatus(1);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21