syntax - c++ - iterating through a map of 3 elements -


i'm new use of stl containers in c++.

i have map of 3 elements (2 strings pair - acting key, , int acting value.)

map<pair<string, string>, int> wordpairs; 

but when try iterate through this:

  (map<pair<string, string>, int> iterator = wordpairs.begin(); != wordpairs.end(); i++) {       cout << i->first << " " << i->second << "\n";     } 

the compiler throwing errors:

     error: expected ‘;’ before ‘i’          (map<pair<string, string>, int> iterator = wordpairs.begin(); != wordpairs.                                                       ^     error: name lookup of ‘i’ changed iso ‘for’ scoping [-fpermissive]     a7a.cpp:46:50: note: (if use ‘-fpermissive’ g++ accept code)     error: cannot convert ‘std::map<std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, int>::iterator {aka std::_rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, int> >}’ ‘int’ in assignment          (map<pair<string, string>, int> iterator = wordpairs.begin(); != wordpairs.                                                         ^     error: no match ‘operator!=’ (operand types ‘int’ , ‘std::map<std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, int>::iterator {aka std::_rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, int> >}’)          (map<pair<string, string>, int> iterator = wordpairs.begin(); != wordpairs.                                                                                ^          error: expected ‘)’ before ‘;’ token      pair<string, string>, int> iterator = wordpairs.begin(); != wordpairs.end(); i++) {                                                                                     ^    error: expected ‘;’ before ‘)’ token      pair<string, string>, int> iterator = wordpairs.begin(); != wordpairs.end(); i++) { 

not sure i'm doing wrong here - should simple fix though.

  1. you got type wrong (you used spaces instead of ::).

  2. map iterator gives key value pair -- , key pair! have pair pair member. here's example want do.

    #include <iostream> #include <map> #include <string> #include <utility> using namespace std;  int main() {   pair<string, string> my_key("to", "be");   map<pair<string, string>, int> wordpairs { { {"hello", "world"}, 33} };   (const auto& kv : wordpairs) {     cout << kv.first.first << ", "           << kv.first.second << static_cast<char>(kv.second);   }   return 0; } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -