File: remove.cc

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (49 lines) | stat: -rw-r--r-- 1,395 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <iterator>
    #include <vector>
    using namespace std;
    using StrVect = vector<string>;

    bool judge(string const &word)
    {
        return count(word.begin(), word.end(), 'a') > 1;
    }

    void show(auto const &begin, auto const &end)
    {
        copy(begin, end, ostream_iterator<string>(cout, ", "));
        cout << "\n";
    }

    int main()
    {
        StrVect words =
            {
                "kilo", "alpha", "lima", "mike", "alpha", "november",
                "alpha", "alpha", "alpha", "papa", "quebec"
            };

        auto src{ words };

        cout << "Removing all \"alpha\"s:\n";
        auto end = remove(src.begin(), src.end(), "alpha");
        show(src.begin(), end);
        cout << "Leftover elements are:\n";
        show(end, src.end());

        src = words;
        cout << "Remove_copy_if removes words having > 1 'a' chars:\n";
        StrVect kept;
        remove_copy_if(src.begin(), src.end(), back_inserter(kept), judge);
        show(kept.begin(), kept.end());

    }
    //  Displays:
    //    Removing all "alpha"s:
    //    kilo, lima, mike, november, papa, quebec,
    //    Leftover elements are:
    //    alpha, alpha, alpha, , ,
    //    Remove_copy_if removes words having > 1 'a' chars:
    //    kilo, lima, mike, november, quebec,