C++のmultimapにはデータペアを削除する関数がないため、struct v tmp_vのキーに該当する部分をequal_rangeで取り出したのち、その部分からデータがtmp_v_valueに一致するものを削除するプログラムを書きました。以下にその部分のコードを掲載します。
しかしこのプログラムを実行したところ、equal_rangeのイテレータが空になってしまいました。確かにデータは追加したはずなのですが。
struct vと比較演算子の定義もコードの下に載せます。
アドバイスをお願いいたします。
C++
1/* 事前にstruct vを定義 */ 2 3multimap<struct v, struct v> *nmap = new multimap<struct v, struct v>; 4/* ------ いくつかのデータを追加。省略 ------*/ 5 6//nmapからtmp_v, tmp_v_valueのペアを持つデータのみを削除するプログラムが以下.... 7auto range = nmap->equal_range(tmp_v); //tmp_vはstruct v の変数 8for (auto it = range.first; it != range.second; ++it) { 9 if(it->second == tmp_v_value) { 10 nmap->erase(it); //tmp_v_value (tmp_v_valueはstruct vの変数)と一致したらそのイテレータを削除 11 break; 12 } 13} 14
以下に構造体の定義を追記いたします。
C++
1typedef struct v { 2 uint8_t first; 3 uint8_t second; 4 uint8_t third; 5 uint8_t fourth; 6 inline bool operator > (const struct v &i) const { 7 if (first > i.first) { 8 return true; 9 } else if(first == i.first && second > i.second){ 10 return true; 11 } else if(first == i.first && second == i.second && third > i.third){ 12 return true; 13 } else if(first == i.first && second == i.second && third == i.third && fourth > i.fourth){ 14 return true; 15 } else { 16 return false; 17 } 18 } 19 20 inline bool operator >= (const struct v &i) const { 21 if (first >= i.first) { 22 return true; 23 24 } else if(first == i.first && second >= i.second){ 25 return true; 26 27 } else if(first == i.first && second == i.second && third >= i.third){ 28 return true; 29 30 } else if(first == i.first && second == i.second && third == i.third && fourth >= i.fourth){ 31 return true; 32 33 } else { 34 return false; 35 36 } 37 } 38 39 inline bool operator < (const struct v &i) const { 40 if (first > i.first) { 41 return false; 42 } else if(first == i.first && second > i.second){ 43 return false; 44 } else if(first == i.first && second == i.second && third > i.third){ 45 return false; 46 } else if(first == i.first && second == i.second && third == i.third && fourth > i.fourth){ 47 return false; 48 } else { 49 return true; 50 } 51 } 52 53 inline bool operator <= (const struct v &i) const { 54 if (first >= i.first) { 55 return false; 56 } else if(first == i.first && second >= i.second){ 57 return false; 58 } else if(first == i.first && second == i.second && third >= i.third){ 59 return false; 60 } else if(first == i.first && second == i.second && third == i.third && fourth >= i.fourth){ 61 return false; 62 } else { 63 return true; 64 } 65 } 66 67 inline bool operator == (const struct v &i) const { 68 if (first == i.first && second == i.second && third == i.third && fourth == i.fourth) { 69 return true; 70 } else { 71 return false; 72 } 73 } 74 75 inline bool operator != (const struct v &i) const { 76 if (first != i.first || second != i.second || third != i.third || fourth != i.fourth) { 77 return true; 78 } else { 79 return false; 80 } 81 } 82}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。