Multimap confusion; how does it work?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Multimap confusion; how does it work?

 
0
  #1
Sep 26th, 2005
Hello.

I've just discovered that I didn't think a cunning plan all the way through.

I've got a grid with a bunch of columns and rows holding CStrings. What I'm doing is trying to filter the grid to only display certain strings. The user selects a location in the grid, and all the rows with the same string in the same column are filtered from the grid.

I've got that working. Mostly.

I've got a map using the CString entry to be filtered out as the key, and the column number as the value. This works fine and dandy, and lets me take back filters if necessary without reloading the entire grid's contents.

The problem is: I can get the same string in two columns, and the code is written such that it won't allow the same key to be filtered twice.
This seemed smart at the time.

Now, I've just realized that there may be cases when the user wants several (probably " ") strings filtered out, but they will be prevented from doing so by the code.

At this point, I'm thinking a multimap would be better, because I think that a multimap would allow several instances of " " to be entered as a key to several columns.

True?

If so, how in blazes does one know if the results of multimapname.find("Whatevah") will give me access to the correct value (ie, column) I want to check?

I haven't adapted this program for multimap yet; I'm just trying to figure out how it would work if I could use the multimap container.

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Multimap confusion; how does it work?

 
0
  #2
Sep 26th, 2005
>how in blazes does one know if the results of multimapname.find
>("Whatevah") will give me access to the correct value (ie, column) I want to check?
One doesn't know. The order of duplicates in a multimap is unspecified.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Multimap confusion; how does it work?

 
0
  #3
Sep 26th, 2005
Oh. Well, that's not very good for this application, then... And it seems like my plan really was pretty dumb.

Can anyone suggest a better storage class for this problem?

Otherwise, I'll have to go with a multimap and an iterative search by key and value. I don't think the user will generate thousands of filters, so it shouldn't be too expensive to implement.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Multimap confusion; how does it work?

 
0
  #4
Sep 26th, 2005
How about you keep the same setup except instead of a single column number as the map value, use a vector of column numbers?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Multimap confusion; how does it work?

 
0
  #5
Sep 26th, 2005
Ah, that would be usable, considering the vectors would be for ints, and not require any fooling around with the operator overloads.
Nice.

But.. Um... Would that be a declaration like this?

  1.  
  2. typedef pair <const CString,vector<int>> cCStr2IntVect;
  3.  
  4. //Map for recording single string filters and the columns. Strings are the keys, because they don't repeat.
  5. // The second entry is the column number the filter was performed in.
  6. map <CString,vector<int>> Stringfilt;
  7. map <CString,vector<int>>:: iterator strIter;

The compiler's unhappy with that, even though it's got the vector header.
error C2146: syntax error : missing ',' before identifier 'cCStr2IntVect'
error C2065: 'cCStr2IntVect' : undeclared identifier
error C2143: syntax error : missing '>' before ';'
error C2208: 'struct std:: pair' : no members defined using this type

And that's just for the typedef pair line. Any idea what I'm messing up?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Multimap confusion; how does it work?

 
0
  #6
Sep 26th, 2005
>Any idea what I'm messing up?
Put a space between the closing angle brackets. The parser is reading that as a bitwise right shift:
  1. typedef pair <const CString,vector<int> > cCStr2IntVect;
Lather, rinse, repeat.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Multimap confusion; how does it work?

 
0
  #7
Sep 26th, 2005
...
That's horrifying. Thanks!

It seems to mostly be compiling. I'm doing the additions thusly:

  1.  
  2. strIter = Stringfilt.find(filterStr);
  3. if(strIter!= Stringfilt.end())
  4. {
  5. for(vIter=strIter->second.begin();vIter!=strIter->second.end(); vIter++)
  6. {
  7. if(*vIter==fCell.col) //Have already encountered this string and column pairing.
  8. return;
  9. }
  10. strIter->second.push_back(fCell.col);//.insert(fCell.col);
  11. }
  12. else
  13. {
  14. strIter->second.push_back(fCell.col);
  15. }

Weird... But I think it'll work.

EDIT: Correction. It compiles, but has problems with the dbgheap... I need to think about this some more... ...
Crud. Day's nearly gone. Haven't gotten anything done 'cause of bad design.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Multimap confusion; how does it work?

 
0
  #8
Sep 26th, 2005
Here's what seems to work, but I'm nervous about memory leaks.

In the header, I have the following declarations.
  1. typedef pair <const CString,vector<int> > cCStr2IntVect;
  2.  
  3. vector <int>::iterator vIter;
  4.  
  5. //Map for recording single string filters and the columns. Strings are the keys, because they don't repeat.
  6. // The second entry is the column number the filter was performed in.
  7. map <CString,vector<int> > Stringfilt;
  8. map <CString,vector<int> >:: iterator strIter;
  9.  
  10. //Map for recording filter other calls.Strings are the keys, because they don't repeat.
  11. //The second entry is the column number the filter was performed in.
  12. map<CString,vector<int> > Otherfilt;
  13. map<CString,vector<int> >::iterator othIter;


Then, I assign new values to the filter map thusly.
I looked through the watch vectors, and they seem to be storing the information correctly. But I also see the vector destructor operating... I can only assume that's just on the local copy of v1, because the information persists in Stringfilt.
  1.  
  2. CString filterStr = m_pGrid->GetItemText(fCell.row,fCell.col);
  3.  
  4. strIter = Stringfilt.find(filterStr);
  5. if(strIter!= Stringfilt.end())
  6. {
  7. for(vIter=strIter->second.begin();vIter!=strIter->second.end(); vIter++)
  8. {
  9. if(*vIter==fCell.col) //Have already encountered this value
  10. return;
  11. }
  12. strIter->second.push_back(fCell.col);
  13. }
  14. else
  15. {
  16. //First occurance of this filter.
  17.  
  18. vector<int> v1( 1,fCell.col);
  19. Stringfilt.insert(cCStr2IntVect(filterStr,v1));
  20.  
  21. }
  22.  
  23. for(int i = 0;i<pDoc->vectlen; i++)
  24. {
  25. if(!pDoc->packlist[i].hidden)
  26. {
  27. if(filterStr==m_pGrid->GetItemText((i+1),fCell.col))
  28. {
  29. pDoc->packlist[i].hidden=HIDDEN;
  30. // m_pGrid->SetRowHeight((i+1),0); //Hide row.
  31. }
  32. else if(!firstflag &&(i>fCell.row-1)) //Find first instance of packet that won't be filtered AFTER the packet defining the filter
  33. {
  34. firstflag=1;
  35. pDoc->iter=&pDoc->packlist.at(i); //note, when i 13, packet number is 14.
  36. //break; //Exit loop.
  37. }
  38. }
  39.  
  40. if(!firstflag && (i==(pDoc->vectlen-1))) //At final packet, and it's hidden
  41. {
  42. //firstflag = 1;
  43. for(int z = fCell.row-2;z>0;z--)
  44. {
  45.  
  46. if(!pDoc->packlist[z].hidden) //Find packet closest to the filtered one that isn't filtered.
  47. {
  48. firstflag =2;
  49. pDoc->iter=&pDoc->packlist[z];
  50. break;
  51. }
  52. }//end for(z)
  53. }
  54. }//end for (i)







And I'm using this to clean up memory.
  1. void CGUISnoopView::FilterWipe()
  2. {
  3. for(strIter=Stringfilt.begin(); strIter!=Stringfilt.end();strIter++)
  4. {
  5. strIter->second.clear();
  6. }
  7. Stringfilt.clear();
  8.  
  9.  
  10. for(othIter=Otherfilt.begin(); othIter!=Otherfilt.end();othIter++)
  11. {
  12. othIter->second.clear();
  13. }
  14. Otherfilt.clear();
  15. }

I'm sure I'm messing up somewhere.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC