Thread: map iterator

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    map iterator

    I have a map and a vector.

    I need to loop through the map and see if my vector has a matching record.

    I'm using this:
    Code:
    	std::map<std::string, int> mpDetailRecords;
    	std::vector<nsSVector::csRecord>::iterator startSDV = schooldistinctvector.begin();
    	std::string mapval;
    
    	while (startSDV != schooldistinctvector.end()) 
    	{
    		//add trim
    			mapval = ((startSDV)->corp + (startSDV)->grade);
    			trim(mapval);
    			mpDetailRecords[mapval.c_str()]++;
    			++startSDV;
    	}
    
    //------------------------------------------
    this is the part with the problem
    std::map<std::string, int>::const_iterator itDetailRecords;
        for (itDetailRecords=mpDetailRecords.begin(); itDetailRecords != mpDetailRecords.end(); ++itDetailRecords) 
    	{
    		rval = findDesiredValue(schoolsummaryvector, itDetailRecords->first) ;
    		if (rval == 0)
    		{
    			std::cout << itDetailRecords->first << " not found or found with 0 count."<<std::endl;
    		}
    		else if (rval != itDetailRecords->second)
    		{
    			std::cout << itDetailRecords->first << ":" << rval << " not equal "<< itDetailRecords->second <<std::endl;
    		}
    	}
    For whatever reason, it doesn't like "itDetailRecords->first "

    I've tried
    (itDetailRecords)->first
    (*itDetailRecords)->first
    itDetailRecords.first
    *itDetailRecords.first
    and none of these are working. I have sample code that says it should work...but I can't get it to work in my application.

    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What does the prototype for findDesiredValue look like? itDetailRecords->first should return a const string&, which is usually what you want.

    Also, post the error message you are getting.

    BTW, earlier in the code, there is no need for .c_str() in this line: mpDetailRecords[mapval.c_str()]++;

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    The problem is when I step through the code and go to the first line within the for loop, my debug watch value shows "overloaded operator -> not supported." for itDetailRecords->first

    However, if I view the details for itDetailRecords, I find first under Val under Ptr. To me, that says pointer.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Oh, well that's just a "feature" of your debugger. There's not a whole lot you can do about that. Sometimes the watch window doesn't work on some variables, and it usually doesn't work with -> for pointers or iterators.

    If you want to see the string in your watch window, there are lots of tricks you can try. Maybe just expand itDetailRecords and look for first until you see its value like you did. If you can see the value from first by doing that, try dragging that line and dropping it on a blank line in the watch window. It will figure out the correct syntax to allow you to see the value without expanding the iterator. You might also be able to modify the autoexp.dat file, although you'd have to do some research on that to get it to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to assign a map iterator to an int variable?
    By Masterx in forum C++ Programming
    Replies: 22
    Last Post: 05-02-2009, 09:05 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. problem with a map iterator
    By anykey in forum C++ Programming
    Replies: 17
    Last Post: 04-29-2005, 11:49 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM