Thread: 2D vector struct, 1D pointer?

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

    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Access from a pointer... or just data2D ?
    If the later..just use the [] operator...like "data2D[0][0].iA"
    else .. " pData1d->at(0).iA "

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

    Bingo!
    Thank you very much.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    (*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
    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
    98
    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, 05:38 PM
  3. Vector of a struct containing another vector
    By Todd88 in forum C++ Programming
    Replies: 61
    Last Post: 12-05-2008, 06: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