Your browser does not seem to support CSS. If images appear below, please disregard them.
It appears that you're running an Ad-Blocker. This site is monetized by Advertising and by User Donations; we ask that if you find this site helpful that you whitelist us in your Ad-Blocker, or make a Donation to help aid in operating costs.
Previous Thread
Next Thread
Print Thread
Rate This Thread
#17268 - 04/27/03 09:13 PM Vectors of Objects  
Joined: Jan 2003
Posts: 8
HitYouth Offline
Junior Member
HitYouth  Offline
Junior Member

Joined: Jan 2003
Posts: 8
Hi,

I was just wondering if anyone knew the correct syntax for creating a vector of Objects and from there, accessing the data members of said objects.

Thanks

Sponsored Links
#17269 - 04/27/03 09:46 PM Re: Vectors of Objects  
Joined: Mar 2002
Posts: 1,136
pergesu Offline
UGN Elite Poster
pergesu  Offline
UGN Elite Poster

Joined: Mar 2002
Posts: 1,136
Pimpin the Colorizzle
Are you talking about STL vectors? An STL vector is just like any other template class, so you can use whatever data type you want with it. Here's a little example program:
Code:
#include <iostream.h>
#include <vector.h>

int main()
{
        // Create a vector of integers
        vector<int> v(10);

        // Fill it up with stuff
        for(int i = 0; i < 10; i++)
                v[i] = i;

        // Display some data from the vector
        cout<<"Max size of vector: " <<v.max_size() <<endl;
        cout<<"Actual size of vector: " <<v.size() <<endl <<endl;

        // Traverse it with an iterator
        vector<int>::iterator iter = v.begin();
        while(iter != v.end())
        {
                // Access the element
                cout<< *iter++ <<" ";
        }

        cout<<endl;

        return 0;
}
Output of the program on my OS X machine:
Code:
bash-2.05a$ ./vector 
Max size of vector: 1073741823
Actual size of vector: 10

0 1 2 3 4 5 6 7 8 9 
bash-2.05a$ 
Since it's STL, all you need to do is change vector to vector and change the assignment loop.

#17270 - 04/28/03 10:39 PM Re: Vectors of Objects  
Joined: Jan 2003
Posts: 8
HitYouth Offline
Junior Member
HitYouth  Offline
Junior Member

Joined: Jan 2003
Posts: 8
Many thanks! How does the assignment method change when trying to create a vector of objects from within an instantiation of an object? for example a vector of employees instantiated within an employee database.

Do I need to utilise the push_back() function because I seem to be having difficulties in utilizing the standard array style subscript method?

#17271 - 04/28/03 10:42 PM Re: Vectors of Objects  
Joined: Jan 2003
Posts: 8
HitYouth Offline
Junior Member
HitYouth  Offline
Junior Member

Joined: Jan 2003
Posts: 8
Nevermind figured it out.. Thanks anyway for your help!


Member Spotlight
Gremelin
Gremelin
Portland, OR; USA
Posts: 7,195
Joined: February 2002
Show All Member Profiles 
Forum Statistics
Forums45
Topics47,460
Posts82,630
Average Daily Posts8
Members2,159
Most Online1,567
Apr 25th, 2010
Latest Postings
Top Posters(All Time)
UGN Security 40,624
Gremelin 7,195
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Liked Users (All Time)
§intå× Likes: 1
Cold Sunn Likes: 1
Crime Likes: 1
Cyrez Likes: 1
Ghost Likes: 1
Gremelin Likes: 4
Ice Likes: 1
unreal Likes: 1
Top Liked Users (30 Days)
No Data Found
Powered by UBB.threads™ PHP Forum Software 7.6.0
(Snapshot build 20160902)