Please check error in following code to print 2 elements of a map at a time

#include
#include
#include

using namespace std;

int main()
{

// empty map container
map<string, int> gquiz1;

// insert elements in random order
gquiz1.insert(pair<string, int>("Delhi", 40));
gquiz1.insert(pair<string, int>("Bombay", 30));
gquiz1.insert(pair<string, int>("Amritsar", 60));
gquiz1.insert(pair<string, int>("Ambala", 20));
gquiz1.insert(pair<string, int>("Chandigarh", 50));
gquiz1.insert(pair<string, int>("Jaipur", 50));
gquiz1.insert(pair<string, int>("Maharashtra", 10));

// printing map gquiz1

cout << "\nThe map gquiz1 is : \n";
cout << "\tKEY\t\tELEMENT\n";
for (auto itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
	cout << '\t' << itr->first
		<< '\t'<<'\t' << itr->second;
  auto j=itr+1;
  cout<<j->first;
  
      cout<< '\n';
}

return 0;

}

hello @sahilkhan2312000131

u cant add 1 to map iterator to get next iterator. as they are not overload(map does not have random access iterators, only bidirectional iterators) .

use next function to get next iterator

#include <bits/stdc++.h>
#include
#include

using namespace std;

int main()
{

// empty map container
map<string, int> gquiz1;

// insert elements in random order
gquiz1.insert(pair<string, int>("Delhi", 40));
gquiz1.insert(pair<string, int>("Bombay", 30));
gquiz1.insert(pair<string, int>("Amritsar", 60));
gquiz1.insert(pair<string, int>("Ambala", 20));
gquiz1.insert(pair<string, int>("Chandigarh", 50));
gquiz1.insert(pair<string, int>("Jaipur", 50));
gquiz1.insert(pair<string, int>("Maharashtra", 10));

// printing map gquiz1

cout << "\nThe map gquiz1 is : \n";
cout << "\tKEY\t\tELEMENT\n";
for (auto itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
	cout << '\t' << itr->first
		<< '\t'<<'\t' << itr->second;
  auto j=std::next(itr, 1);
  cout<<j->first;
  
      cout<< '\n';
}

return 0;

}
Still getting error

check now_>

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.