Putting multiple structures in a vector

This is a discussion on Putting multiple structures in a vector within the C++ Programming forums, part of the General Programming Boards category; I have a struct Code: struct Point { int x; int y; }; I also have another struct called Code: ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Putting multiple structures in a vector

    I have a struct
    Code:
    struct Point {
    int x;
    int y;
    };
    I also have another struct called
    Code:
    struct Table {
    vector<points> vec;
    };
    I have to put a fixed amount of points into the vector when the program now starts up. Is there anyway I can do this other than writing out this long thing...

    Code:
    Table createTable() {
    Table temp;
    Point tmpPoint;
     
    tmpPoint.x = whaterver;
    tmpPoint.y = whatever;
    temp.push_back(tmpPoint);
     
    tmpPoint.x = whaterver2;
    tmpPoint.y = whatever2;
    temp.push_back(tmpPoint);
     
     
    tmpPoint.x = whaterver3;
    tmpPoint.y = whatever3;
    temp.push_back(tmpPoint);
     
    return temp;
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Beep

    Code:
    struct Point 
    {
    	int x;
    	int y;
    
    	Point(int X, int Y)
    	{
    		x = X;
    		y = Y;
    	}
    };
    Boop

    Code:
    Table createTable() 
    {
    	Table temp;
    	
    	temp.push_back(Point(x, y));
    	temp.push_back(Point(a, b));
    	temp.push_back(Point(i, j));
    
    	return temp;
    }
    This is C++, and struct constructors are allowed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 02:13 PM
  2. Multiple Structures Producing Error
    By IdioticCreation in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2007, 10:58 PM
  3. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 01:34 PM
  4. Vector Question
    By Death_Wraith in forum C++ Programming
    Replies: 59
    Last Post: 06-09-2004, 08:57 AM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM

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