作者hn12404988 (Willy)
看板C_and_CPP
標題Re: [問題] 三維map的find要怎麼做?
時間Sat Jan 7 15:13:09 2017
: 我想用map把一個int清單(vector<int>) 存在一個三維空間中
: 希望用 mymap[x][y][z] 可以對應到一個 vector<int>
: 但是現在有個問題是,我想要確認某(x0,y0,z0)位置是不是已經建立清單了
: 如果不是的話要生一個vector給它
: 網路上有查了find的用法,但是找不到類似問題的解決方法
: map生成如下,請問各位,find或有什麼方法能做到這件事?
: map<int, map<int, map<int, vector<int> > > > mymap;
using namespace std;
typedef map<int,map<int,map<int,vector<int> > > > mymap;
bool create(mymap &m,int x,int y,int z){
if(m[x][y].find(z)==m[x][y].end()){
vector<int> input;
m[x][y][z] = input;
return true;
}
else{
return false;
}
}
int main(int argc,char* argv[]){
mymap m;
if(create(m,1,1,1)==true){
cout << "Vector doesn't exist, and create one" << endl;
}
else{
cout << "Vector already exist." << endl;
}
if(create(m,1,1,1)==true){
cout << "Vector doesn't exist, and create one" << endl;
}
else{
cout << "Vector already exist." << endl;
}
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.133.16.181
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1483773191.A.73A.html
1F:推 Deltak: 謝謝喔!原來find是這樣用,我看範例都是用iterator,自己 01/07 16:42
2F:→ Deltak: 用都怪怪的 01/07 16:42
3F:→ LPH66: 你看到的是 general 的 find, map 因為元素有序所以有更好 01/07 16:50
4F:→ LPH66: 的 find 法, 因此自己會提供一個 find 給用 01/07 16:50
5F:推 Deltak: 了解了,謝謝喔 01/07 16:55
6F:→ pziyout: 為何不將 x,y,z 併起來變成一個字串 x:y:z 當成 key ? 01/09 17:06