ã¤ãã¬ã¼ã¿ã®ã«ã¼ããæ¸ãã¨ãã®ã¹ã¿ã¤ã«
ãããããªãã¡ãããããããªã話ãªãã§ããã©ãC++ã§ã¤ãã¬ã¼ã¿ã®ã«ã¼ããæ¸ãã¨ãã«ãã«ã¼ãã®åæåã¨çµäºæ¡ä»¶ãæ¸ãè¡ãé·ããªããã¡ãªã®ã§ãæ¹è¡ã®ã¹ã¿ã¤ã«ãã©ããããããæ©ã¿ã¾ãã
â»ã«ã¼ãã®ä¸ã§vectorã«å¤åãªãå ´åãçµäºæ¡ä»¶ãæ¯åend()ãå¼ã³åºãã¦æ¤æ»ããã®ã¯ç¡é§ãªé¢æ°å¼ã³åºãã«ãªã£ã¦ãã¾ãå¯è½æ§ãããã®ã§ãå¤æ°ã«å
¥ãã¦ãã¾ããâC++ Coding Standards 9é
ãææå°æ©ã«æä¸é©åãã¦ãã¾ããªãã
æ¹è¡ããªã
for(std::vector<MyClass*>::iterator itr = m_elements.begin(), end = m_elements.end(); itr != end; ++itr){ // ã«ã¼ãã®ä¸èº« }
- è¡ãé·ããªã£ã¦èªã¿ã¥ãã
1ã¤ã®è¡ã«1ã¤ã®statement
for(std::vector<MyClass*>::iterator itr = m_elements.begin(), end = m_elements.end(); itr != end; ++itr){ // ã«ã¼ãã®ä¸èº« }
- åæåé¨åã ã2è¡ãã£ã¦ãçµäºæ¡ä»¶é¨åãæ´æ°é¨åã¨ã®å¯¾ç§°æ§ãæªã
è¡ã®é·ãã ããåºæºã«æ¹è¡
for(std::vector<MyClass*>::iterator itr = m_elements.begin(), end = m_elements.end(); itr != end; ++itr){ // ã«ã¼ãã®ä¸èº« }
- æå³ã¨è¡¨è¨ãã¡ãã¯ãã§èªã¿ã¥ãã
ä¸2ã¤ããã¤ããªããããæã
for(std::vector<MyClass*>::iterator itr = m_elements.begin(), end = m_elements.end(); itr != end; ++itr){ // ã«ã¼ãã®ä¸èº« }
- 3è¡ç®ã«çµäºæ¡ä»¶ã¨æ´æ°é¨åãæ¼ãè¾¼ã¾ãã¦ãã¦ã¡ãã£ã¨èªã¿ã¥ããããªâ¦ï¼
ã¤ãã¬ã¼ã¿ã®åãtypedefãã¦çããã
typedef std::vector<MyClass*>::iterator vi; for(vi itr = m_elements.begin(), end = m_elements.end(); itr != end; ++itr){ // ã«ã¼ãã®ä¸èº« }
- å±æçãªtypedefã£ã¦ãã³ã¼ããèªãã¨ãã«è³å ã¡ã¢ãªã«è¼ããªãã¨ãããªãæ å ±ãåçã«å¢æ¸ããããããã¾å¥½ããããªã
- ãã ãã©ããã¯æ £ããªããªã
ã©ããå¾®å¦ãªã¨ããããã£ã¦ããã ã¨ããã®ã決ãããã¾ããããä»ã®ã¨ãã4ã¤ãã®ããã¤ããªããããæããã使ã£ã¦ãã¾ãã
ãã¨ãããã¤ãã¬ã¼ã¿ã®ã«ã¼ããããªãã¦algorithm使ãããã¨ãã話ãããã¾ããããªããªããã綺éºã«ç½®ãæããããå ´åã°ããã§ã¯ãªãã¦ãè¤éãªãã¡ã³ã¯ã¿ä½ã£ã¦ç¡çã«algorithm使ãããã«ãã¦ããããã¯ããã§ä¿å®æ§æªããªã£ã¦ãã¾ãã§ããã(åãã¾ã STLããã¹ã¿ã¼ãã¦ããªãããä¸æãæ¸ããªãã¨ããã ãããããã¾ãããâ¦)ã