Thread: How to use vector of structure ?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    How to use vector of structure ?

    Code:
    struct structtid
    {
      int tid; //transaction id list
      int *tidptr;  // pointer to the transaction
      int transnum; //size of each transaction
    };
    
    <vector>structtid t;
    
    structtid temp;
    temp.tid = 1;
    temp.transnum = 10;
    t.push_back(temp);
    I tried to add a structure into t but there is error. I also try to declare <vector>int tidptr in sttucttid, but there is error too..

    Can anyone help me? Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, maybe because the syntax is vector<structtid> and vector<int>?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Using:

    #include <vector>

    Will allow you to declare a vector from the Standard Template Library:

    std::vector<int> Ages(10);

    Writing std:: all the time though could be a little annoying. So after the include, writing:

    using namespace std;

    Will import the std namespace and now you can just:

    vector<int> Ages(10);

    To declare 10 spots for an integer in the vector called Ages.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    I understand that a vector of integer can be declared and elements in integer can be easily added to it. But what about a vector of structure? Where the structure is defined by me. Can it be done too?

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I see absolutely no reason why you could not I also added onto your code. A nifty feature of C++ is the fact the structs and classes are exactly the same (public vs. private, whatev). This means you can have a struct constructor, reducing the need for your temporary struct to push onto the vector. This code works fine for me.

    Code:
    struct structtid
    {
    	int tid;       // transaction id list
    	int *tidptr;   // pointer to the transaction
    	int transnum;  // size of each transaction
    
    	structtid(int tid, int transnum)
    	{
    		this->tid      = tid;
    		this->transnum = transnum;
    	}
    };
    
    int main(int argc, char *argv[])
    {
    	std::vector<structtid> t;
    	t.push_back(structtid(1, 10));
    
    	return 0;
    }
    I see absolutely no reason why you could not

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    whoa! thanks, i think this is what i need. i will try it later. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM