æ¨æºã®é¢æ°ã«ããã½ã¼ã
ãã½ã¼ããã®ãã®ãèªåã§å®è£ ããã®ã¯æ°å¹´åã«ãã£ãããããããæ¥ã»ã¨ãã©ã®è¨èªã§ã©ã¤ãã©ãªå ã«ç¨æããã¦ããã®ã§ããã£ã½ã©ã®ãã¨ããªãéãèªä½ãããã¨ã¯ãªããªãã¾ããããï¼ããã°ã©ãã³ã°ã®ç·´ç¿åé¡ã«ã¯ããåºã¦ãã¾ããä»£å ¥ã»æ¯è¼ã»äº¤æã»ç¹°ãè¿ããªã©ãããã°ã©ãã³ã°ã®"ããã¯"ãè©°ã¾ã£ã¦ã¾ããããï¼ã
ãæ¨æºC++ã®ã½ã¼ãé¢æ°std::sortã®ä½¿ãæ¹ã¯ã¨ã£ã¦ãç°¡åãã½ã¼ã対象ã¨ãªãé åï¼vectorï¼dequeãªã©ã®è¦ç´ åã®å é ã¨æ«å°¾ï¼ã®ç´å¾ï¼ã¨ãå¼æ°ã«ä¸ããã ãã§ãè¦ç´ a, bã«å¯¾ãa < bãªãã°aãbããåã®å é ã«è¿ãä½ç½®ã«ç½®ãããããï¼ã¤ã¾ãæé ï¼å°ããé ï¼ã«ã½ã¼ãããã¾ããé¨åã½ã¼ãstd::partial_sortãå®å®ãªã½ã¼ãstd::stable_sortãåæ§ã§ãã
// data[N] ãæé ã«ã½ã¼ããã double data[N]; std::sort(data, data+N); // ããã㯠// std::sort(std::begin(data), std::end(data));
ãstd::sortã«ã¯ç¬¬3å¼æ°ã«è¦ç´ ã®ä¸¦ã³é ãå¤å®ããé¢æ°ãªãã¸ã§ã¯ããä¸ãããã¨ãã§ãã¾ããé¢æ°ãªãã¸ã§ã¯ãã¯2ã¤ã®è¦ç´ a, bãå¼æ°ã¨ãã¦å¼ã³åºããããã®ã¨ãã«trueãè¿ãã°aã¯bããå é å¯ãã«é ç½®ããã¾ãã
// éé ã«ã½ã¼ããã double data[N]; sort(begin(data), end(data), [](double a, double b) { return b < a; });
// æååé·ã®çãé ã«ã½ã¼ããã string data[N]; sort(begin(data), end(data), [](const string& a, const string& b) { return a.length() < b.length(); });