-
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.
-
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()]++;
-
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.
-
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.