Vector withing map: accessing values

Discussion in 'C++' started by Marcus, Feb 2, 2006.

  1. Marcus

    Marcus Guest

    I have a vector within a vector within a map. The innermost vector
    houses a struct with some ints and floats. The map keys off the
    secondary vector which acts as a wrapper for multiple vector struct
    entries for a specific key.

    std::vector<Object> vec;
    std::vector<std::vector <Object> > vecMain;
    std::map<std::string,std::vector <std::vector <Object> > > map;

    //Assuming I have loaded the vectors and map, how do I get at the vec
    values if I have a key value?

    std::map<std::string,std::vector <vector <Object> > >::iterator pos;
    pos = map.find("abc");
    if (pos != map.end()){

    //...how should I handle the multiple loops here?

    Any help would be very appreciated.
     
    Marcus, Feb 2, 2006
    #1
    1. Advertising

  2. Marcus

    Mark P Guest

    Marcus wrote:
    > I have a vector within a vector within a map. The innermost vector
    > houses a struct with some ints and floats. The map keys off the
    > secondary vector which acts as a wrapper for multiple vector struct
    > entries for a specific key.
    >
    > std::vector<Object> vec;
    > std::vector<std::vector <Object> > vecMain;
    > std::map<std::string,std::vector <std::vector <Object> > > map;
    >
    > //Assuming I have loaded the vectors and map, how do I get at the vec
    > values if I have a key value?
    >
    > std::map<std::string,std::vector <vector <Object> > >::iterator pos;
    > pos = map.find("abc");
    > if (pos != map.end()){
    >
    > //...how should I handle the multiple loops here?


    Just take it one level at a time. It may be easier to organize the
    computation if you use references at each level. For example:

    vector<vector<Object> >& vvo = pos->second;

    // work with vvo as needed... loops, algorithms, indicies, etc.
     
    Mark P, Feb 2, 2006
    #2
    1. Advertising

  3. Marcus

    Dakka Guest

    Marcus wrote:
    > I have a vector within a vector within a map. The innermost vector
    > houses a struct with some ints and floats. The map keys off the
    > secondary vector which acts as a wrapper for multiple vector struct
    > entries for a specific key.
    >
    > std::vector<Object> vec;
    > std::vector<std::vector <Object> > vecMain;
    > std::map<std::string,std::vector <std::vector <Object> > > map;
    >
    > //Assuming I have loaded the vectors and map, how do I get at the vec
    > values if I have a key value?
    >
    > std::map<std::string,std::vector <vector <Object> > >::iterator pos;
    > pos = map.find("abc");
    > if (pos != map.end()){
    >
    > //...how should I handle the multiple loops here?
    >
    > Any help would be very appreciated.
    >

    This isn't that efficient and does nothing but I think it will give you
    the idea....
    -------------------------------------------------
    #include <map>
    #include <vector>
    #include <string>
    typedef std::map<std::string,std::vector <std::vector <int> > > Mvv;

    int main()
    {
    Mvv mvv;
    std::string key("test");
    Mvv::iterator mvvit(mvv.find(key));

    if (mvvit != mvv.end())
    {
    for (int ii(0); ii < mvvit->second.size(); ++ii)
    for (int jj(0); jj < mvvit->second[ii].size(); ++jj)
    mvvit->second[ii][jj] = 1;
    }

    return 0;
    }
    ---------------------------------------------------

    --dakka

    *Dykstra's Observation:*
    /If debugging is the process of removing bugs, then programming must be
    the process of putting them in./
     
    Dakka, Feb 2, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ericoli
    Replies:
    0
    Views:
    492
    Chris Ericoli
    Oct 30, 2003
  2. =?Utf-8?B?dHRob21wc29uMjAwNw==?=

    Options for viewing word docs and editing from withing asp.net

    =?Utf-8?B?dHRob21wc29uMjAwNw==?=, Sep 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    =?Utf-8?B?dHRob21wc29uMjAwNw==?=
    Sep 20, 2004
  3. J.K. Bach
    Replies:
    0
    Views:
    355
    J.K. Bach
    Jun 15, 2005
  4. Replies:
    1
    Views:
    323
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Sep 9, 2005
  5. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
Loading...

Share This Page