2013-04-05 153 views
1

嘿,这里的人对你们来说都是一个有趣的挑战。 我收到一个文本文件,我应该逐行处理信息。处理部分是微不足道的,只要我可以获得单独的行。然而,这是挑战:逐行读取文件无需使用FOR/WHILE循环

  1. 我必须这样做,而没有在我的代码中使用任何FOR/WHILE循环。 (这包括递归)
  2. 我只允许使用标准C++库。

目前,现在我的最好的解决办法是这样的: Is there a C++ iterator that can iterate over a file line by line? 但我希望是一个好机会,不涉及创建我自己的迭代器类或者实现了的std :: string的代理。

P.S.这是一所学校的分配,这里的挑战是使用std功能和算法的组合来解决这个问题,但我不知道如何去解决它

+0

为什么不能使用'for'或'while'循环? – 2013-04-05 16:56:58

+0

这是很没意思的,如果你逐行解析你的文字循环遍历文件 – Paranaix 2013-04-05 16:58:24

+4

像这样愚蠢的要求,'goto'是最好的方法。 – nneonneo 2013-04-05 16:59:55

回答

6
ifstream input("somefile") 

if (!input) { /* Handle error */ } 

//MyDataType needs to implement an operator>> 

std::vector<MyDataType> res; 

std::istream_iterator<MyDataType> first(input); 
std::istream_iterator<MyDataType> last; 
std::copy(first,last, std::back_inserter(res)); 

//etc.. 

你的输入操作员可以是这样的:

std::istream& operator>>(std::istream &in,MyDataType & out) 
{ 
    std::string str; 
    std::getline(in,str); 
    //Do something with str without using loops 
    return in; 
} 

有很多循环在这里(你不”想要使用goto,不是吗?),但它们都隐藏在std::copystd::getline

+1

您是不是指'operator >>? – Angew 2013-04-05 17:06:45

+0

这实际上非常类似于我发布的链接的答案,如果我找不到任何东西,我会用它作为最后的手段 – 2013-04-05 17:12:37

+0

@Angew zzz ..对不起,我修正了它 – sbabbi 2013-04-05 17:15:20

0

这只是一个玩具。

它包含一个基于生成器的方式来吸取文件(我发现它很容易编写),以及一个将Generators变成迭代器的半工作适配器。

在此上下文中的生成器是一个函子,它接受一个lambda,并将该lambda传递给该函数并返回true,或返回false。它使得一个非常短的循环:

while(gen([&](std::string line) { 
    // code using line goes here 
})); // <- notice that this is an empty while loop that does stuff 

但你不允许使用它。 (这个设计灵感来自于python生成器,对于许多类型的问题,包括从文件中读取,我真的发现比C++迭代器更适合使用。

adpator使用此生成器并使其成为迭代器,因此您可以将它传递给for_each

这个问题的关键在于你的问题本身就是一个愚蠢的问题,所以我并没有做一些明显的事情(goto,或者直接在istream迭代器上使用for_each),我提议用一些愚蠢和不同的东西来搞乱。 :)

线发生器是好的,简称:

struct LineGenerator { 
    LineGenerator(std::ostream& os_):os(&os_) {} 
    std::ostream* os; 
    template<typename F> 
    bool operator()(F&& func) { 
    std::string s; 
    bool bRet = std::getline(*os, s); 
    if (!bRet) return false; 
    std::forward<F>(func)(s); 
    return true; 
    } 
}; 

适配器,在另一方面,是相当混乱:(而不是测试)

template<typename Generator> 
struct generator_iterator { 
    typedef generator_iterator<Generator> my_type; 
    typedef typename std::decay< decltype(std::declval<Generator>()()) >::type value_type; 
    Generator g; 
    bool bDone; 
    value_type val; 
    generator_iterator(Generator g_):g(g_), bDone(false) { 
    next(); 
    } 
    generator_iterator(): bDone(true) {} 
private: 
    void next() { 
    if (bDone) return; 
    bDone = g(val); 
    } 
public: 
    generator_iterator& operator++() { 
    next(); 
    return *this; 
    } 
    value_type operator*() { 
    return val; 
    } 
    const value_type operator*() const { 
    return val; 
    } 
    value_type* operator->() { 
    return &val; 
    } 
    value_type const* operator->() const { 
    return &val; 
    } 
    bool operator==(my_type const& o) { 
    if (bDone != o.bDone) return false; 
    if (!bDone && !o.bDone) return true; 
    return false; 
    } 
}; 
template<typename Generator> 
generator_iterator<Generator> make_gen_it(Generator&& g) { 
    return generator_iterator<Generator>(std::forward<Generator>(g)); 
} 
template<typename Generator> 
generator_iterator<Generator> make_end_gen_it() { 
    return generator_iterator<Generator>(); 
} 

我希望这个游乐!