#34196: C++ map iterator 解


alex950301 (alex0301)


這題使用map比較方便

Ex :

cin >> a >> b ;

map< int , int > mp ;

mp[a] += b ; // a 代表鍵值 , b 代表值

 

要排序時交給map就好了,map內建紅黑樹,可以用迭代器不用自己排

Ex1 :

for( map< int , int > ::iterator it = mp.begin() ; it != mp.end() ; ++it) {

    cout << it -> first << " " << it -> second << endl ;

}

Ex2 : 

for( const auto& [key , value] : mp ) {

    cout << key << " " << value << endl ;

}