0

I have for loop like below:

for (multimap<string,string>::iterator it2 = ppp.first;it2 != ppp.second; ++it2)
   {
       if(it2==ppp.second -1)
       str=str+it2->second.substr(0,(it2->second).find('-'));
       else
       str=str+it2->second.substr(0,(it2->second).find('-'))+'&';
   }

I am using the condition like below:

  if(it2==ppp.second -1)

for checking the last element and do some additional functionality if i found the last element.But the above condition doesnt work. it throw's me an error:

"000001.cc", line 50: Error: The operation "__rwstd::__rb_tree<std::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::basic_string<char, std::char_traits<char>, std::allocator<char>>>, __rwstd::__select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>::iterator - int" is illegal.
1 Error(s) detected.

Could anybody please help.

Vijay
  • 65,327
  • 90
  • 227
  • 319

3 Answers3

2

You have not posted the whole code fragment so I am not sure what ppp is but I believe it is something like a pair of iterators. The iterator for map does not define the operator-(int val), so you can not use that one. However you can fix the error doing something like this:

multimap<string,string>::iterator end_val = ppp.second;
--end_val;
for (multimap<string,string>::iterator it2 = ppp.first;it2 != ppp.second; ++it2)
{
    if(it2==end_val)
    str=str+it2->second.substr(0,(it2->second).find('-'));
    else
    str=str+it2->second.substr(0,(it2->second).find('-'))+'&';
}

Hope that helps.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

Try

end = ppp.second;
std::advance (end, -1);
if (it2 == end) {}
Sergey Miryanov
  • 1,820
  • 16
  • 29
-1

Assuming ppp is the multimap, you should get the iterator to the first element by

ppp.begin() 

or

ppp.rbegin() // to get a reverse iterator

In you loop:

multimap<string,string>::iterator it2 = ppp.first

pp.first will not give the an iterator, instead it will give the first element of the pair helper class pair<string, string>

Sanish
  • 1,699
  • 1
  • 12
  • 21