# Maximum PC

 It is currently Fri Apr 17, 2015 11:50 pm

 All times are UTC - 8 hours

 Page 1 of 1 [ 6 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: C++ question: working with a vector of stuctures ???Posted: Tue Mar 01, 2005 9:03 pm
 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

 Post subject: Posted: Tue Mar 01, 2005 9:35 pm
 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

 Post subject: Posted: Tue Mar 01, 2005 9:40 pm
 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

 Post subject: Posted: Tue Mar 01, 2005 10:13 pm
 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

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

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

Top

 Post subject: Posted: Fri Mar 04, 2005 2:35 am
 Bitchin' Fast 3D Z8000*

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 6 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.