C++ Iterator 練習 - 印出不重複的輸入字元。

input data
12333444221118834
executed in console
$ cat 1.in | ./a.out
12348$
src code
#include <iterator>
#include <iostream>
#include <set>
#include <algorithm>

int main(int argc, char** argv)
{
    std::copy_if(std::istream_iterator<char>(std::cin),     // cin iter begin
                 std::istream_iterator<char>(),             // cin iter end
                 std::ostream_iterator<char>(std::cout,""), // cout iter begin
                 [](char c) { static std::set<char> cache;   // store showed char
                 return (cache.find(c) == cache.end()) &&    // not found then cached
                         cache.insert(c).second; });         //           then allow copy
    return 0;
}
comments powered by Disqus