Quantcast

Maximum PC

It is currently Sat Nov 22, 2014 4:24 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: C++ question: working with a vector of stuctures ???
PostPosted: Tue Mar 01, 2005 9:03 pm 
Little Foot
Little Foot

Joined: Thu Dec 23, 2004 12:54 pm
Posts: 150
Hi, I'm assuming that I can use a vector of structures, but how do I add data onto it?

struct myStruct {
int x;
int y;
};

int main() {
myStruct hello; // creates a myStruct Structure called hello

// now I'd like to add an element onto the structure containing 5 for x, and 3 for y. Do I use hello.pushback()? Do I use hello.x.pushback(), or hello.pushback.x? How would I write that one line?

}


Thanks...


Top
  Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 9:35 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Probably the most straightforward way is:

Code:
#include <vector>

struct mystruct
{
   int x;
   int y;
};

int main()
{
   std::vector< mystruct > thevect;
   mystruct temp;

   temp.x = 5;
   temp.y = 3;

   thevect.push_back(temp);

   return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 9:40 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
If you don't mind using a class instead, here's a better way:

Code:
#include <vector>

class mystruct
{
public:
   mystruct(int a, int b);
   int x;
   int y;
};
mystruct::mystruct(int a, int b)
{
   x = a;
   y = b;
}

int main()
{
   std::vector< mystruct > thevect;

   thevect.push_back(mystruct::mystruct(5,3));

   return 0;
}

I mean, you ARE using C++ anyway.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:13 pm 
Little Foot
Little Foot

Joined: Thu Dec 23, 2004 12:54 pm
Posts: 150
I'm just beginning, haven't gotten to classes yet...

Though I know them in Java... whatever.

I think I see what you did. You created the entire structure as a temp, and then pushed it all back. Thanks!!


UPDATE: worked like a charm! Thanks again!


Top
  Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 2:59 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Sure, no problem.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 2:35 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
UTjunkie wrote:
I'm just beginning, haven't gotten to classes yet...

Though I know them in Java... whatever.

I think I see what you did. You created the entire structure as a temp, and then pushed it all back. Thanks!!


UPDATE: worked like a charm! Thanks again!

A struct and a class are basically the same thing. The only difference is that all of a struct's methods and fields are public by default.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.