Re: [問題] 三維map的find要怎麼做?

看板C_and_CPP作者 (Willy)時間9年前 (2017/01/07 15:13), 編輯推噓2(204)
留言6則, 3人參與, 最新討論串2/2 (看更多)
: 我想用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://www.ptt.cc/bbs/C_and_CPP/M.1483773191.A.73A.html

01/07 16:42, , 1F
謝謝喔!原來find是這樣用,我看範例都是用iterator,自己
01/07 16:42, 1F

01/07 16:42, , 2F
用都怪怪的
01/07 16:42, 2F

01/07 16:50, , 3F
你看到的是 general 的 find, map 因為元素有序所以有更好
01/07 16:50, 3F

01/07 16:50, , 4F
的 find 法, 因此自己會提供一個 find 給用
01/07 16:50, 4F

01/07 16:55, , 5F
了解了,謝謝喔
01/07 16:55, 5F

01/09 17:06, , 6F
為何不將 x,y,z 併起來變成一個字串 x:y:z 當成 key ?
01/09 17:06, 6F
文章代碼(AID): #1OS9K7Sw (C_and_CPP)
文章代碼(AID): #1OS9K7Sw (C_and_CPP)