ã³ã¬ã¯ã·ã§ã³å ã®è¦ç´ ãåé¤ããã«ã¯
ã³ã¬ã¯ã·ã§ã³å
ããç¹å®ã®è¦ç´ ãåé¤ããããã¤ãã®ææ³ãèãã¦ã¿ãã
ï¼Objective-C, Java, C++ ããããã®å ´åãï¼
æãåç´ãªã®ã¯ãã³ã¬ã¯ã·ã§ã³ã«å¯¾ãã¦ã¤ã³ããã¯ã¹ãæå®ãã¦åé¤ããã¨ãããã®ã ã
içªç®ã®è¦ç´ ãåé¤ããã
Objective-Cã®å ´å1
NSMutableArray* array = [NSMutableArray array]; [array addObject:[NSNumber numberWithInt:0]]; [array addObject:[NSNumber numberWithInt:1]]; [array addObject:[NSNumber numberWithInt:2]]; [array addObject:[NSNumber numberWithInt:3]]; [array addObject:[NSNumber numberWithInt:4]]; // ... [array removeObjectAtIndex:i];
Javaã®å ´å1
List<Integer> list = new ArrayList<>(); list.add(0); list.add(1); list.add(2); list.add(3); list.add(4); // ... list.remove(i);
C++ã®å ´å1
std::vector<int> vct; vct.push_back(0); vct.push_back(1); vct.push_back(2); vct.push_back(3); vct.push_back(4); // ... vct.erase(vct.begin() + i);
ä¸è¨ã®ããæ¹ã¯ç°¡åã§ã¯ããããå®éã«ã¯ãã¤ã³ããã¯ã¹ãæå®ãã¦åé¤ãããã¨ããããããã³ã¬ã¯ã·ã§ã³å
ã®è¦ç´ ã1ã¤ãã¤ãã§ãã¯ãã¦ãããæ¡ä»¶ãæºããå ´åã«åé¤ãããã¨ããã·ãã¥ã¨ã¼ã·ã§ã³ã®æ¹ãå§åçã«å¤ãã§ãããããã®ã¨ããåºæ¬çã«ã¯ããã³ã¬ã¯ã·ã§ã³è¦ç´ ã®ã¤ãã¬ã¼ã·ã§ã³ä¸ãã«ã³ã¬ã¯ã·ã§ã³ã®å
容ãå¤æ´ãã¦ã¯ãªããªãã
ãã¨ãã°ãæ´æ°å¤ã³ã¬ã¯ã·ã§ã³ããå¶æ°è¦ç´ ãåé¤ãã¦ã¿ãã
以ä¸ã®ããæ¹ã¯ãããããã¾ããããªãã
Objective-Cã®å ´å2
int i = 0; // ã«ã¼ãä¸ã«å®è¡æã¨ã©ã¼ã«ãªã for (NSNumber* n in array) { if (0 == [n integerValue] % 2) { [array removeObjectAtIndex:i]; } else { ++ i; } }
Javaã®å ´å2
int i = 0; // ã«ã¼ãä¸ã«å®è¡æã¨ã©ã¼ã«ãªã for (int n : list) { if (0 == n % 2) { list.remove(i); } else { ++ i; } }
C++ã®å ´å2
int i = 0; // å®è¡æã¨ã©ã¼ã«ã¯ãªããªããããã¾ããããªã std::for_each(vct.begin(), vct.end(), [&](int n) { if (0 == n % 2) { vct.erase(vct.begin() + i); } else { ++ i; } });
ä¸è¨ã®ããæ¹ã¯ãããããã«ã¼ãå
ã§ã³ã¬ã¯ã·ã§ã³ã®ãµã¤ãºãå¤æ´ãã¦ãã¾ã£ã¦ããããã«ãã¤ãã¬ã¼ã·ã§ã³ãéä¸ãããã¾ããããªããªã£ã¦ãã¾ã£ã¦ããã
åç´ãªè§£æ±ºæ¹æ³ã¨ãã¦ã¯ãã³ã¬ã¯ã·ã§ã³ãã³ãã¼ããä¸ã§ã¤ãã¬ã¼ã·ã§ã³ããã°ããã
Objective-Cã®å ´å3
int i = 0; // ARCã使ããªãã®ã§ããã°ã[[array copy] autorelease] ã¨æ¸ãã㨠for (NSNumber* n in [array copy]) { if (0 == [n integerValue] % 2) { [array removeObjectAtIndex:i]; } else { ++ i; } }
Javaã®å ´å3
int i = 0; for (int n : new ArrayList<>(list)) { if (0 == n % 2) { list.remove(i); } else { ++ i; } }
C++ã®å ´å3
int i = 0; std::vector<int> copy(vct); std::for_each(copy.begin(), copy.end(), [&](int n) { if (0 == n % 2) { vct.erase(vct.begin() + i); } else { ++ i; } });
ã¤ãã¬ã¼ã·ã§ã³ä¸ã«ã³ã¬ã¯ã·ã§ã³ãå¤æ´ããããããã ãã«ã³ã¬ã¯ã·ã§ã³ã丸ãã¨ã³ãã¼ãããªãã¦ã®ã¯ãããã«ãéå¹ççã«æããããã¾ã¨ãã«åããã¨ã¯åãããã ãããããæ¡å¼µforæãåããè¨èªã§ããã®ã«ãã«ã¼ãå¤ã«ã¤ã³ããã¯ã¹å¤æ°iãç¨æãã¦ãã¾ã£ã¦ããã®ãã©ãã«ãç ©ãããã
ãã¦ãããã¾ã§ããã¦3ã¤ã®è¨èªã§ä¼¼ããããªæ¸ãæ¹ããã¦ã¿ãããObjective-Cã¨Javaã«ãããã³ã¬ã¯ã·ã§ã³ã¨ããã®ã¯ãè¦ç´ ããã¹ã¦ãªãã¸ã§ã¯ãåã§ãããC++ã®vectorã¯ãã³ãã¬ã¼ãã§ãããä¸è¨ã®ä¾ã§ã¯è¦ç´ ã¯intå¤ã§ããã
Objective-CãJavaã®ã³ã¬ã¯ã·ã§ã³ã¯ã©ã¹ã«ã¯ãè«ççç価æ§æ¤è¨¼ï¼Objective-Cã®isEqual, Javaã®equalsï¼ã®ãªã¼ãã¼ã©ã¤ãï¼Objective-Cã®isEqual, Javaã®equalsï¼ã使ã£ã¦ãæå®ãããªãã¸ã§ã¯ããæ¢ãåºããåé¤ãããã¨ããã¡ã½ãããç¨æããã¦ããããã®æ©æ§ã使ãã°ãã¤ã³ããã¯ã¹ã«ã¦ã³ã¿ã使ããã«ãã³ã¬ã¯ã·ã§ã³ããè¦ç´ ãåé¤ãããã¨ãã§ããã
C++ã§åããããªãã¨ãããããã°ãoperator==ã¨remove_ifãªã©ã使ã£ã¦ãã£ã¨å¹ççã«å®ç¾ã§ããããvectorèªä½ã«ã¯ã©ããããæå®ããå¤ã¨==ãªè¦ç´ ãåé¤ãããã¨ããã¡ã½ããã¯ç¨æããã¦ããªãããã ãã¾ãããã£ã¨ãããããªãã®ã¯ãªãã¦ãå°ããªãã
Objective-Cã®å ´å4
for (NSNumber* n in [array copy]) { if (0 == [n integerValue] % 2) { [array removeObject:n]; // nã®isEqual:ãtrueã«ãªããªãã¸ã§ã¯ããæ¢ãã¦åé¤ãã } }
Javaã®å ´å4
for (Integer n : new ArrayList<>(list)) { if (0 == n % 2) { list.remove(n); // nã®equalsãtrueã«ãªããªãã¸ã§ã¯ããæ¢ãã¦åé¤ãã } }
Javaã«é¢ãã¦ã¯Auto Boxingã¨ãªã¼ãã¼ãã¼ããéãªã£ã¦ããã®ã§ãå°ãå¤ãã¥ãããããã£ã¦ãããã¨ã¯Objective-Cã®å ´åã¨ã»ã¼åãã§ããããJavaã®å ´å4ãã§å¼ã³åºãã¦ããremoveã¡ã½ããã¯ããJavaã®å ´å3ãã¾ã§ã§å¼ã³åºãã¦ãã
ãã¤ã³ããã¯ã¹ãæå®ãã¦åé¤ããã
ã¡ã½ããã§ã¯ãªãã
ãæå®ããè¦ç´ ã¨equalsãªæåã®è¦ç´ ãåé¤ããã
ã¡ã½ããã§ããããã®ããã«ãå¼æ°ã«intå¤ã§ã¯ãªããæ示çã«Integerã渡ãããã«å¤æ´ãã¦ããã
ããããªãããçµæçã«ããããã4ã®ããæ¹ã¯ã3ã®ããæ¹ãããããªãä½éã§ãããã³ã¬ã¯ã·ã§ã³ã®ã¤ãã¬ã¼ã·ã§ã³ä¸ã«ãããã«ã³ã¬ã¯ã·ã§ã³ã®å é è¦ç´ ãããtrueãè¿ãã¾ã§ãequalsãisEqualãå¼ã³åºãã¦ããã§ããããã¨ã¯æ³åããã«å®¹æããããããã3ã®æç¹ã§æ¢ã«ãã³ã¬ã¯ã·ã§ã³ãã³ãã¼ããã¨ããç¹ã§æããã«ç¡é§ã§ããã
ã¾ãã4ã®ããæ¹ãè¯ããªãã¦ã®ã¯ããã¡ããåè«ã ã
ããå°ãã¾ã¨ããããªæ¹æ³ãããã
Objective-Cã®å ´å5
// åé¤ããè¦ç´ ãã³ãã¼ãã¦ããããã®å¥ã®é åãç¨æ NSMutableArray* discards = [NSMutableArray array]; for (NSNumber* n in array) { if (0 == [n integerValue] % 2) { [discards addObject:n]; // åé¤æ¡ä»¶ãæºããè¦ç´ ãåé¤ç¨é åã«ä¿å } } [array removeObjectsInArray:discards]; // ä¸æ¬åé¤
ä¸è¨ã®ã³ã¼ãã¯ãã¤ãã¬ã¼ã·ã§ã³ã®ããã®ã³ãã¼ã¯é¿ãããã®ã®ãçµå±ä»ã®é åãç¨æãã¦ãã¾ã£ã¦ãããåé¤ã¡ã½ããã®å¼ã³åºãã1åã§æ¸ããã¨ã¨ãã³ã¬ã¯ã·ã§ã³ã丸ãã¨ã³ãã¼ããããã§ã¯ãªããã¨ãèããã°ãä»ã®Objective-Cã§ã®ã¤ãã£ãªã ã«æ¯ã¹ãã°ãå¹¾åãã·ãããããªããããããremoveObjectsInArrayã®ãã¸ãã¯ããããããã4ã®å ´åã¨åãããã«ãisEqualã®æ¤æ»ãå¤æ°èµ°ã£ã¦ããã¨æããããããçµæçã«ã©ã¡ããé«éã«ãªããã¯å¾®å¦ãªã¨ããã
Javaã«ã¯ããå°ãã¹ãã¼ããªããæ¹ãããã
ãããä»ããæ¡å¼µforæã®åªç¸ãã解ãæ¾ãããã¨ãã ã
Javaã®å ´å5
for (Iterator<Integer> itr = list.iterator(); itr.hasNext(); ) { Integer n = itr.next(); if (0 == n % 2) { itr.remove(); } }
å ç¥Iteratorã®åãæãç¥ã£ãããObjective-Cã®å ´å5ãã«æ¯ã¹ã¦ãç¡é§ãªã³ã¼ãããªãã¦èªã¿ãããããã¶ããJavaã§ã¯ãå¤ãã®å ´åã«ããã®ããæ¹ãä¸çªå¹ççã ã¨æãã
ãã¦ãæ¬é¡ã¯ããããã ã
ãC++ã®å ´å3ããæ¹è¯ãã¦ããJavaã®å ´å5ãã«è¿ãããæ¹ã«å¤æ´ãã¦ã¿ãã
C++ã®å ´å4
for (auto itr = vct.begin(); vct.end() != itr; ) { if (0 == *itr % 2) { itr = vct.erase(itr); } else { ++ itr; } }
ãã¤ã³ãã¯eraseã¡ã½ããã®æ»ãå¤ã使ããã¨ã§ãããeraseã¡ã½ããã¯ãè¦ç´ ãåé¤ããå¾ã次ã®è¦ç´ ãæãã¤ãã¬ã¼ã¿ã¼ãè¿ãã¦ããããããã¤ã使ã£ã¦ã¤ãã¬ã¼ã·ã§ã³ãå
ã«é²ããããã«ããã°ãeraseã«ãã£ã¦è¦ç´ ãåé¤ãã¦ãã¾ã£ã¦ããå¾ç¶ããè¦ç´ ãã¤ãã¬ã¼ã·ã§ã³ã§ããã¨ãã寸æ³ã ãã³ã¬ã¯ã·ã§ã³ã®ã³ãã¼ãç¡é§ã«çæãã¦ããã®ãæé¤ãããã¨ãã§ãããããä»ã¾ã§ã®ããæ¹ã®ä¸ã§ã¯æéã§ãããã
ããã¾ã ããå°ãã¹ãã¼ãã«è¨è¿°ã§ããã
vectorã®å é¨ã¯åçé åã®æ§é ããã¦ãããããç·å½¢ãªã¹ãæ§é ã«æ¯ã¹ã¦ãè¦ç´ ã®è¿½å ãåé¤ãé ããå³ã¡ããããvectorå ã«åé¤ãã¹ãè¦ç´ ãå¤æ°åå¨ããå¯è½æ§ãé«ãã®ã§ããã°ãè¦ç´ ã1ã¤ãã¤åé¤ããããããåé¤ãã¹ãè¦ç´ ã ã端ã«ã½ã¼ãããä¸ã§ä¸æ¬åé¤ããæ¹ãé«éã«ãªãå¯è½æ§ã¯é«ãã
C++ã®å ´å5
auto tail_itr = std::remove_if(vct.begin(), vct.end(), [](int n) { return 0 == n % 2; }); vct.erase(tail_itr, vct.end());
ãã®ããæ¹ãä¸çªå¹ççãªå¯è½æ§ãé«ãããèªã¿ããããã¨æãã
ããã«ãã©ã ãå¼ã使ã£ã¦ããã®ã§ããªãã¨ãªãã«ãã³ã¤ã¤ã
remove_ifã®ç¬¬3å¼æ°ã«ã¯ãbool operator()(int/* vectorã®è¦ç´ ã¨åãå */)ãæã¤é¢æ°ãªãã¸ã§ã¯ãããåãã·ã°ããã£ã®ã©ã ãå¼ã渡ãã¦ãå¼æ°ãæ¤è¨¼ã®ä¸ããã®å¤ãvectorå ããåé¤ãããªãã°true, ä¿æãããªãfalseãè¿ãããã«ãããremove_ifã¯å®éã«è¦ç´ ã®åé¤ãè¡ãããã§ã¯ãªããåé¤ãã¹ãè¦ç´ ãã³ã¬ã¯ã·ã§ã³ã®å¾ãã«éé¢ãã¦ãæå¹ãªè¦ç´ ã®æ«å°¾ã®å¾ããæãã¤ãã¬ã¼ã¿ã¼ãè¿ããå¾ã£ã¦ãå®éã«vectorå ããä¸è¦ãªè¦ç´ ãåé¤ããã«ã¯ãremove_ifå®äºå¾ã«ãeraseã§å®éã®åé¤ãå®è¡ããå¿ è¦ãããã
ããã«ç¡é§ãçããªãã°ãeraseã®å¼ã³åºããããé¿ãã¦ããã®å¾ãtail_itr以éã®è¦ç´ ãåç §ããªãããã«ãã¦ãã¾ãã°ããã
ãããã«ãã¦ããåé¤æ¡ä»¶ããç°¡æ½ã§ããã°ãç¡çãªã1è¡ã§æ¸ããã
ãã¨ãã°ããã¡ãã¨åé¤ããå ´åã
vct.erase(std::remove_if(vct.begin(), vct.end(), [](int n){return 0 == n % 2;}), vct.end());
ã¨ã¬ã¬ã³ãã
ããã³ããã¼ã¯ã
â»æ³¨æï¼ã³ã³ãã¤ã«ãªãã·ã§ã³ãªã©ã¯èª¿æ´ãã¦ããªãã®ã§ããã¾ãã¢ãã«ãªããªãæ°åã ã¨æãã¾ãã
10ä¸åã®ã©ã³ãã ãªæ´æ°å¤ã®ã³ã¬ã¯ã·ã§ã³ãæ§æãã
ï¼ããããè¨æ¸¬éå§ï¼
ã³ã¬ã¯ã·ã§ã³å
ããå¶æ°ã®è¦ç´ ãå®å
¨ã«åé¤
ï¼è¨æ¸¬çµäºï¼
å¦çæéã¨ã³ã¬ã¯ã·ã§ã³ã«æ®ã£ãè¦ç´ æ°ã表示ããã
è¦ç´ æ°ã5ä¸åå¾ã§ãããã¨ã確èªã
Objective-Cã®å ´å3 227ããªç§ Objective-Cã®å ´å4 10ç§ä»¥ä¸ï¼ä¸æï¼ Objective-Cã®å ´å5 245ããªç§ Javaã®å ´å3 438ããªç§ Javaã®å ´å4 2212ããªç§ Javaã®å ´å5 439ããªç§ C++ã®å ´å3 351ããªç§ C++ã®å ´å4 346ããªç§ C++ã®å ´å5 2ããªç§
Objective-Cã®3, 5ã®ããæ¹ããC++ã®3, 4ã®ããæ¹ã¨åç以ä¸ã«é«éã¨ããã®ã¯æå¤ã ã£ãããªã«ããæé©åãåãã¦ãããããªæ°ãããããC++ã®å ´å5ãã«é¢ãã¦ã¯ããªãªã¼ã¹ã¢ã¼ãã§ã³ã³ãã¤ã«ããã¨ã0ã1ããªç§ã¨ããçµæã«ãªã£ããã¾ãã«å§åçå®è¡é度ã
çµè«ã¨ãã¦ã¯ãã¯ããããããã®5ã®ããæ¹ããå¯èªæ§ãå®è¡å¹çãééãã«ãããã©ããåã£ã¦ãå®å®ãã¦è¯ããããªæ°ãããã
ããã¾ã