Thread: Vectors of structs

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    Vectors of structs

    Hi, just a quick question about vectos of structs here. If I have this struct:


    Code:
    typedef struct _MyStruct
    {
    
    int Var1;
    int Var2;
    
    } MyStruct;

    and I have this vector:


    Code:
    vector<MyStruct> MyVectorOfStructs;

    and we'll say I did this:


    Code:
    MyStruct TempStruct;
    
    for ( int i=0; i<10; i++ ) // Just assigning values to the vector.
    {
     TempStruct.Var1=i;
     TempStruct.Var2=i;
    
     MyVectorOfStructs.push_back( TempStruct );
    }

    If I want to get MyVectorOfStructs fourth position Var1 (if you get me, it should be 5 or something), would I have to assign the fourth position of MyVectorOfStructs to a struct, and retrive the Var1 value from that, or is there a way I can directly retrive the value of the fourth position's Var1?

    Thanks.

    EDIT: I just wrote that down, didn't compile anything, so if there are errors, assume they're due to that.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well vectors have an overloaded subscript operator so
    Code:
    MyVectorOfStructs[3].var1
    should work. And there really is no reason to typedef a struct in C++. Another option is to use an iterator, but in this case, I'd say that's pointless
    Code:
    vector<MyStruct>::iterator vecIter = MyVectorOfStructs.begin() + 3;
    
    vecIter->Var1;
    Last edited by SlyMaelstrom; 06-14-2006 at 03:22 PM.
    Sent from my iPadŽ

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I can't believe I didn't try that. I feel like a complete idiot now!

    Sorry for waisting the space of the forum!

    I think the typedef thing is just habbit. Is there anything wrong with it in C++?

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by twomers

    I think the typedef thing is just habbit. Is there anything wrong with it in C++?
    there's nothing especially wrong with it, it's just unnecessary. (you don't write "signed int" every time you declare an int, do you? )

    OTOH, if the struct is in a header shared between C and C++ sources, then it's actually a good thing.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> OTOH, if the struct is in a header shared between C and C++ sources, then it's actually a good thing.

    Good point. Meh, it's ... 7 letters, no biggie.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    There's also 'at':
    Code:
    MyVectorOfStructs.at(3).Var1;
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    (STL == options++) == true;
    Sent from my iPadŽ

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I think the typedef thing is just habbit. Is there anything wrong with it in C++?
    It's automatic in C++

    So all you needed was
    Code:
    struct MyStruct
    {
    int Var1;
    int Var2;
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vectors of structs
    By Zosden in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2008, 01:37 AM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. help using vectors with structs
    By Swordsalot in forum C++ Programming
    Replies: 15
    Last Post: 04-09-2008, 11:14 AM
  4. allocating structs within STL vectors
    By aoiOnline in forum C++ Programming
    Replies: 20
    Last Post: 12-05-2007, 02:49 PM
  5. Variable scopes; Vectors of struct's
    By relyt_123 in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2007, 10:07 PM