Esempio n. 1
0
int main() {
    //Ryan's test
    {
        std::vector<int> ivec{1, 4, 9, 16, 25, 36};
        std::vector<std::string> svec{"hello", "good day", "goodbye"};

        for (auto e : zip_longest(ivec, svec)) {
            std::cout << std::get<0>(e) << std::endl; 
            //has to deref iter and the optional object
            std::cout << std::get<1>(e) << std::endl; 
        }
    }
    //Aaron's test
    {
        std::array<int,4> i{{1,2,3,4}};
        std::vector<float> f{1.2,1.4,12.3,4.5,9.9};
        std::vector<std::string> s{"i","like","apples","alot","dude"};
        std::array<double,5> d{{1.2,1.2,1.2,1.2,1.2}};
        std::cout << std::endl << "Variadic template zip_longest" << std::endl;
        for (auto e : iter::zip_longest(i,f,s,d)) {
            std::cout << std::get<0>(e)
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
            **std::get<1>(e)=2.2f; //modify the float array
        }
        std::cout<<std::endl;
        for (auto e : iter::zip_longest(i,s,f,d)) {
            std::cout << std::get<0>(e) 
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
        }
        std::cout << std::endl << "Try some weird range differences" << std::endl;
        std::vector<int> empty{};
        for (auto e : iter::zip_longest(empty,f,s,d)) {
            std::cout << std::get<0>(e) 
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
        }
        std::cout<<std::endl;
        for (auto e : iter::zip_longest(f,empty,s,d)) {
            std::cout << std::get<0>(e) 
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
        }
        std::cout<<std::endl;
        for (auto e : iter::zip_longest(f,s,i,d)) { 
            std::cout << std::get<0>(e) 
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
        }
        std::cout<<std::endl;
    }
    return 0;
}
Esempio n. 2
0
template <typename... Ts>
using const_opt_tuple = std::tuple<boost::optional<const Ts&>...>;

TEST_CASE("zip longest: correctly detects longest at any position",
        "[zip_longest]") {

    const std::vector<int> ivec{2, 4, 6, 8, 10, 12};
    const std::vector<std::string> svec{"abc", "def", "xyz"};
    const std::string str{"hello"};

    SECTION("longest first") {
        using TP = const_opt_tuple<int, std::string, char>;
        using ResVec = std::vector<TP>;

        auto zl = zip_longest(ivec, svec, str);
        ResVec results(std::begin(zl), std::end(zl));
        ResVec rc = {
            TP{{ivec[0]},  {svec[0]},   {str[0]}},
            TP{{ivec[1]},  {svec[1]},   {str[1]}},
            TP{{ivec[2]},  {svec[2]},   {str[2]}},
            TP{{ivec[3]},  {},          {str[3]}},
            TP{{ivec[4]},  {},          {str[4]}},
            TP{{ivec[5]},  {},          {}    }
        };

        REQUIRE( results == rc );
    }

    SECTION("longest in middle") {
        using TP = const_opt_tuple<std::string, int, char>;