前提
AtcoderABC282のC問題に関する質問です。
公式放送(Youtube)の模範解答で連想配列を用いていたので、連想配列に格納された値を確かめようとした際以下のエラーメッセージが発生しました。
実現したいこと
連想配列のvalueとキーを出力したい
発生している問題・エラーメッセージ
エラーメッセージ ./Main.cpp: In function ‘int main()’: ./Main.cpp:17:16: error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::vector<int>’) 17 | cout << itr->second << endl; | ~~~~ ^~ ~~~~~~~~~~~ | | | | | std::vector<int> | std::ostream {aka std::basic_ostream<char>} In file included from /usr/include/c++/9/istream:39, from /usr/include/c++/9/sstream:38, from /usr/include/c++/9/complex:45, from /usr/include/c++/9/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54, from ./Main.cpp:1: /usr/include/c++/9/ostream:108:7: note: candidate: ‘std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]’ 108 | operator<<(__ostream_type& (*__pf)(__ostream_type&)) | ^~~~~~~~ /usr/include/c++/9/ostream:108:36: note: no known conversion for argument 1 from ‘std::vector<int>’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&)’ {aka ‘std::basic_ostream<char>& (*)(std::basic_ostream<char>&)’} 108 | operator<<(__ostream_type& (*__pf)(__ostream_type&)) | ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
該当のソースコード
C++
1ソースコード 2 3 #include <bits/stdc++.h> 4using namespace std; 5#define rep(i,n) for (int i = 0; i < (n); ++i) 6 7int main() { 8 int n; 9 cin >> n; 10 vector<int> x(n), y(n); 11 rep(i,n) cin >> x[i] >> y[i]; 12 string s; 13 cin >> s; 14 map<int,vector<int>> mp; 15 rep(i,n) { 16 mp[y[i]].push_back(i); //連想配列に格納された値を知りたい 17 } 18 for(auto itr=mp.begin(); itr!=mp.end(); itr++) { 19 cout << itr->second << endl; //itr->firstのときは作動したがitr->secondだとエ ラー発生 20 } 21 22 23 return 0; 24}
試したこと
itr->firstのときは作動したがitr->secondだとエラー発生
また標準入力において入力例1のとおり
3
2 3
1 1
4 1
RRL
と入力し、itr->firstとしてプログラムを実行すると、
1
3
と出力された。
この1と3が何を意味するのかも分からない。
この問題はy[i]において等しい値を見つけることが初めのステップとなっている。
となれば1,3ではなく1,2と出力されなければおかしいのではないかと思ってしまう。
またmp[y[i]].push_back(i)で何を行っているのかを教えてくれると助かります。
補足情報(FW/ツールのバージョンなど)
c++(GCC 9.2.1)
回答2件