Chaining iterators for C++

前端 未结 7 2069
野性不改
野性不改 2020-12-01 12:24

Python\'s itertools implement a chain iterator which essentially concatenates a number of different iterators to provide everything from single iterator.

Is there so

7条回答
  •  天命终不由人
    2020-12-01 12:42

    Came across this question while investigating for a similar problem.

    Even if the question is old, now in the time of C++ 11 and boost 1.54 it is pretty easy to do using the Boost.Range library. It features a join-function, which can join two ranges into a single one. Here you might incur performance penalties, as the lowest common range concept (i.e. Single Pass Range or Forward Range etc.) is used as new range's category and during the iteration the iterator might be checked if it needs to jump over to the new range, but your code can be easily written like:

    #include 
    
    #include 
    #include 
    #include 
    
    int main()
    {
      std::deque deq = {0,1,2,3,4};  
      std::vector vec = {5,6,7,8,9};  
    
      for(auto i : boost::join(deq,vec))
        std::cout << "i is: " << i << std::endl;
    
      return 0;
    }
    

提交回复
热议问题