Like Tree1Likes
  • 1 Post By Elysia

2D vector struct, 1D pointer?

This is a discussion on 2D vector struct, 1D pointer? within the C++ Programming forums, part of the General Programming Boards category; Here what I'm doing/trying: Code: struct myStruct{ int iA; double dB; }; vector< vector<myStruct> > data2D; data2D.push_back(vector <myStruct>());// 0 myStruct ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    93

    2D vector struct, 1D pointer?

    Here what I'm doing/trying:
    Code:
    struct myStruct{ int iA; double dB; };
    vector< vector<myStruct> > data2D;
    
    data2D.push_back(vector <myStruct>());// 0
    myStruct row; row.iA=1; row.dB=2.3;
    data2D[0].push_back(row);// 0,0
    
    data2D.push_back(vector <myStruct>());// 1
    row.iA=4; row.dB=5.6;
    data2D[1].push_back(row);// 1,0
    
    vector <myStruct> *pData1D = &data2D[0];// must be wrong?
    
    // How do I access iA=1?
    
    pData1D[0].iA // of course not
    pData1D[0]->iA // nope
    pData1D->[0].iA // and nope
    Thanks

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Access from a pointer... or just data2D ?
    If the later..just use the [] operator...like "data2D[0][0].iA"
    else .. " pData1d->at(0).iA "
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    pData1D->at(0).iA

    Bingo!
    Thank you very much.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    (*pData1D)[0].iA should also work.
    Additionally, it is unclear why you use a pointer when you can use a reference instead:

    vector <myStruct>& pData1D = data2D[0];
    pData1D[0].iA
    motocross1 likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Alternatively
    Code:
    int i = (*pData1D)[0].iA;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    Elysia,
    Reference = wow.
    I actually use them as function parameters already.
    But, I never knew anything about them before.
    I think References are going to be my new favorite discovery.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 04:38 PM
  3. Vector of a struct containing another vector
    By Todd88 in forum C++ Programming
    Replies: 61
    Last Post: 12-05-2008, 05:38 PM
  4. Replies: 1
    Last Post: 05-05-2004, 06:58 AM
  5. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM

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