challenge 条件を満たす行を取り除く

ファイルから1行ずつ読み込み、"#"で始まる行だけを取り除いてファイルに出力するコードを書いてください。

サンプル入力

hello!
# remove this
 # don't remove this
bye!
サンプル出力
hello!
 # don't remove this
bye!

Posted feedbacks - C++

何の変哲もないですが・・・
 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
#include <iostream>
#include <fstream>

void convert(const char* input, const char* output)
{
    std::ifstream fin(input);

    if (fin)
    {
        std::ofstream fout(output);

        std::string s;

        while (std::getline(fin, s))
        {
            if (s.empty() || s[0] != '#')
            {
                fout << s << std::endl;
            }
        }
    }
}

int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        std::cerr << "usage: input output" << std::endl;

        return -1;
    }

    convert(argv[1], argv[2]);

    return 0;
}

前投稿したのがあまり面白くなかったので、自前で行分解するイテレータを作ってSTLしてみました。(行数かさばってすみません(汗))
 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>

class line_iterator : public std::iterator<std::input_iterator_tag, std::string>
{
    std::istream* _in;
    std::string _line;

    void getline()
    {
        if (_in && !std::getline(*_in, _line))
        {
            _in = NULL;
        }
    }

public:
    explicit line_iterator(std::istream& in) : _in(&in) { getline(); }

    line_iterator() : _in(NULL) {}

    const std::string& operator*() const { return _line; }

    line_iterator& operator++()
    {
        getline(); return *this;
    }

    line_iterator operator++(int)
    {
        line_iterator tmp = *this; getline(); return tmp;
    }

    friend bool operator==(const line_iterator& lhs, const line_iterator& rhs)
    {
        return lhs._in == rhs._in;
    }

    friend bool operator!=(const line_iterator& lhs, const line_iterator& rhs)
    {
        return lhs._in != rhs._in;
    }
};

struct starts_with_sharp : std::unary_function<std::string, bool>
{
    bool operator()(const std::string& s) const
    {
        return !s.empty() && s[0] == '#';
    }
};

int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        std::cerr << "usage: input output" << std::endl;

        return -1;
    }

    std::ifstream fin(argv[1]);

    std::ofstream fout(argv[2]);

    std::remove_copy_if(
        line_iterator(fin),
        line_iterator(),
        std::ostream_iterator<std::string>(fout, "\n"),
        starts_with_sharp()
    );

    return 0;
}

最もシンプルに書くならこう。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <iostream>
#include <string>

int main(int,char**)
{
  std::string line;
  while(std::getline(std::cin,line))
    if(line.empty() || line[0]!='#')
      std::cout << line << std::endl;
  return 0;
}

Index

Feed

Other

Link

Pathtraq

loading...