Like Tree3Likes
  • 2 Post By iMalc
  • 1 Post By smokeyangel

Iterating through a vector of maps and a map of vectors...

This is a discussion on Iterating through a vector of maps and a map of vectors... within the C++ Programming forums, part of the General Programming Boards category; Hey guys get this... I defined a vector of maps, like this: Code: typedef vector<map<string,unsigned int> > myvec; And a ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    4

    Iterating through a vector of maps and a map of vectors...

    Hey guys get this...
    I defined a vector of maps, like this:
    Code:
    typedef vector<map<string,unsigned int> > myvec;
    And a map of vectors (i called it index) like this:
    Code:
    typedef map<string,vector<unsigned int> > index;
    Then I did the following:
    In my class, called myCoogle, i declared
    Code:
    myvec the_vec;
    and I filled it with maps...
    In each map there's a word (string) and a number (unsinged int).
    So far so good.
    I declared also
    Code:
    index the_index;
    Now I want to copy all the different words from maps_vec to the_index.
    The words would be the strings...

    And for each vector<int> i will be adding the numbers stored in the vector of maps.

    For example:
    the_vec has 3 maps.
    the 1st has: chicken,1 | person,1 | elevator,5 | is,2 | ...
    the 2nd has: person,2 | icecream,3 | is,3 | ...
    the 3rd has: elevator,1 | bear,1 | is,4 |chicken,3 | ...
    *the words might be similar...

    So the_index should look like this:
    word,[vector of ints]
    chicken[1,0,3]
    person,[1,2,0]
    elevator[5,0,1]
    is[2,3,4]
    icecream[0,3,0]
    bear[0,0,1]

    OK here's my function:

    Code:
    void Coogle::make_index()
    {
        //SCAN THE FIRST MAP
        myvec::iterator myvec_iter;
        map<string,unsigned int>::iterator map_iter;
        index::iterator idx_iter = the_index.begin();
        for(map_iter=maps_vec[0].begin(); map_iter!=maps_vec[0].end(); ++map_iter)
        {
            the_index[map_iter->first].push_back(map_iter->second);
        }
    
    
        //SCAN THE OTHER MAPS
        myvec_iter=maps_vec.begin();
        myvec_iter++;
        int i=0; //FILE #
        while(myvec_iter!=maps_vec.end())
        {
            i++;
            for(map_iter=maps_vec[i].begin(); map_iter!=maps_vec[i].end(); ++map_iter)
            {
                string word=map_iter->first;
                cout << "DEALING WITH WORD \"" << word << "\"" << endl;
                index::iterator location;
                location=the_index.find(word);
                if(location!=the_index.end()) //if word found in the index
                {
                    cout << "WORD EXISTS!" << endl;
                    location->second[i]=map_iter->second;
                }
                else //if not found
                {
                    cout << "WORD DOES NOT EXIST! NEW WORD." << endl;
                    the_index[word].push_back(map_iter->second);
                }
            }
            cout << endl;
    
    
            ++myvec_iter;
        }
    }
    clarification: FILE# is the maps number... I'm working with files (*.txt files).

    Alright so after I scanned the first map, I tried to print the_index and all was fine.
    But I get this when trying to print after scanning also the other maps:

    Name:  the_bug.jpg
Views: 883
Size:  23.6 KB

    So something is wrong with my 2nd 'for' loop.

    Anyone can help please?

    Very sorry for the very long post...

    Thank you very much !!!
    Last edited by alaa_137; 08-17-2012 at 04:39 PM. Reason: variable correction: the_vec ==> maps_vec

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    As it says in brackets in the last line on that dialog, click Retry because you want to debug the application.
    This will break you into the debugger and it will be showing a line of code from vector.

    From the Debug menu, select Windows -> Call Stack (Alt + 7)
    Find the first item in the call stack that relates to code that you wrote and double-click it.
    Now, bring up Debug -> Windows -> Autos and/or Debug -> Windows -> Locals
    From these Windows you can see what the values of your variables are at the current point in that piece of code.

    Any closer to solving it now? Tell us what you're seeing.
    smokeyangel and Elysia like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    I don't have to do all that... I could use a Breakpoint... Much eaiser
    But anyway,
    I'm looking at the Autos... And man I see lotta weird stuff...

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,206
    The code is unnecessarily complicated. A hint to ponder: operator[] on a map will insert elements in certain circumstances.

    Your usage of the variable i is the problem I see (I haven't looked further). In the loop it is keeping track of the current index of myvec_iter. It is also being used to index elements of vectors inside the map of vectors, but you have taken no steps to ensure that i is a valid index of the vector location->second.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    my_vec is a vector of maps.
    then the_vec[0] is the 1st map, the_vec[1] is the second, and so on...
    the_vec contains 3 maps...
    Besides, the first 'for' loop is ok, because I tried to print that and it showed everything is ok

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,206
    You missed my point completely.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by iMalc View Post
    As it says in brackets in the last line on that dialog, click Retry because you want to debug the application.
    This will break you into the debugger and it will be showing a line of code from vector.

    From the Debug menu, select Windows -> Call Stack (Alt + 7)
    Find the first item in the call stack that relates to code that you wrote and double-click it.
    Now, bring up Debug -> Windows -> Autos and/or Debug -> Windows -> Locals
    From these Windows you can see what the values of your variables are at the current point in that piece of code.
    You should definitely take this advice on board!

    Quote Originally Posted by alaa_137 View Post
    I don't have to do all that... I could use a Breakpoint... Much eaiser
    True -- but since your program has already crashed, you'd have to set the breakpoint and restart the program. This might not be a problem for you right now, but if you have a longer running program that crashes intermittently, it's really useful to be able to debug from the crash.

    Anyway, breakpoint or iMalc's way....

    Quote Originally Posted by alaa_137 View Post
    my_vec is a vector of maps.
    then the_vec[0] is the 1st map, the_vec[1] is the second, and so on...
    the_vec contains 3 maps...
    Besides, the first 'for' loop is ok, because I tried to print that and it showed everything is ok
    You'll find that the line crashing is;
    Code:
    location->second[i]=map_iter->second;
    with word "is" and i=1.

    Your problem boils down to the fact that you can't add vector entries like that. You can't access vector entries that don't exist with the array notation, and you certainly can't add new entries using that notation.

    Simple example:
    Code:
     vector<int> v;
     v.push_back(1);
     v.push_back(2);
    
     v[0] = 5; //ok, overwrites first entry
     v[1] = 6; //ok, overwrites second entry
     v[2] = 10; // BANG - v[2] doesn't exist
     cout << v[3]; // BANG - v[3] doesn't exist either
    On the flipside, as grumpy said, using [i] notation on a map will add an entry if one doesn't exist already. But for vector you should use push_back.

    Since you want to store '0' in the index vector when the string wasn't present in the map, I think you'll have to add more code to handle this. Make sure 0s are pushed back to existing entries that had no matches, and make sure new entry creation fills the first N entries with 0s.

    You shouldn't really need a first and second loop -- if you write it properly, you should be able to just have 1, and on the first map it just goes through the "need new entry" path every time.
    iMalc likes this.

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    Oh thank you very much!
    I really mis used the [] operator. It doesn't work the way I expected with vectors.
    I fixed it, used push_back and all that...
    Now it works just perfect

    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector Of Vectors
    By Khadafi in forum C++ Programming
    Replies: 11
    Last Post: 12-30-2011, 11:04 AM
  2. Iterating over private vector
    By mrpringle in forum C++ Programming
    Replies: 9
    Last Post: 08-15-2010, 11:56 AM
  3. Iterating through a map that contains a vector
    By AngryStyro in forum C++ Programming
    Replies: 2
    Last Post: 06-08-2008, 05:01 AM
  4. vector of vectors
    By xddxogm3 in forum C++ Programming
    Replies: 11
    Last Post: 05-04-2007, 10:25 AM
  5. vector of vectors
    By Magos in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2004, 02:46 AM

Tags for this Thread


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