map容器实现一对多
生活随笔
收集整理的這篇文章主要介紹了
map容器实现一对多
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:需求描述
我們希望一個數字或則其他字符串可以對應 一串數,
#include<iostream> #include<map> #include<vector> using namespace std; int main(){map<int,vector<int> > m;map<int,vector<int> >:: iterator t;vector<int> v;for(int i = 0; i < 3; i++){for(int j = 0; j < 2; j++){int num;cin >> num;v.push_back(num);}m[i] = v;v.clear();}//遍歷map數組 for(t = m.begin(); t != m.end(); t++){cout << t->first << ' ';vector<int> :: iterator vt;for(vt = t->second.begin(); vt != t->second.end(); vt++){cout << *vt << " "; }cout << endl;}}這里0 對應兩個數 6 7
1對應兩個數 8 9
2對應兩個數 10 11
總結
以上是生活随笔為你收集整理的map容器实现一对多的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯如何打造新基建时代高可扩展的区块链引
- 下一篇: 7-3 模板题 (10 分)(思路+详解