int index = 2;
auto it = mymap.begin();
for(int i = 0; i < index && it != mymap.end(); ++i)
++it;
if(it != mymap.end())
std::cout << it->first<<" =>"<< it->second << '\n';
else
std::cout << "not found";
In function `int main()':
24 ISO C++ forbids declaration of `it' with no type
24 cannot convert `std::_Rb_tree_iterator<std::pair<const std::string, int> >' to `int' in initialization
25 no match for'operator!=' in 'it != (&mymap)->std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = std::string, _Tp = int, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, int> >]()'
27 no match for'operator!=' in 'it != (&mymap)->std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = std::string, _Tp = int, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, int> >]()'
base operand of `->' is not a pointer
28 base operand of `->' is not a pointer
Well, move loop and printing in own function and call it with different parameters
(alternatively you can print values from the loop body if you would check if it has enough elements beforehand.)
Now could you please tell how I can do the next
I need to assign the value of the second which is integer to a variable each time. so if the first second is 8 I need a=8 and if the second second is 4 I need b=4 so I can use them later