coincoin
coincoin

Reputation: 4685

Print simply STL vectors of vectors recursively in C++

I have used the following code which work well for printing a simple std::vector with printContainer().
I want now to extend it for nested containers with printContainerV2()
I have tried using template to determine if the type is a stl container but it does not seem to be the way to do it.

#include <iostream>
#include <iterator>
#include <vector>

template <typename Iter, typename Cont>
bool isLast(Iter iter, const Cont& cont)
{
    return (iter != cont.end()) && (next(iter) == cont.end());
}


template <typename T>
struct is_cont {
    static const bool value = false;
};

template <typename T,typename Alloc>
struct is_cont<std::vector<T,Alloc> > {
    static const bool value = true;
};


template <typename T>
std::string printContainer(T const& container)
{
    std::string str = "{";
    for (auto it = std::begin(container); it != std::end(container); ++ it)
        if (isLast(it, container))
                str = str + std::to_string(*it) + "}";
        else
                str = str + std::to_string(*it) + ",";
    return str;
}
/*
template <typename T>
std::string printContainerV2(T const& container)
{
    std::string str = "{";
    for (auto it = std::begin(container); it != std::end(container); ++ it)
        if (isLast(it, container))
            if (is_cont<decltype(*it)>::value == true)
                str = str + printContainer(*it);
            else
                str = str + std::to_string(*it) + "}";
        else
            if (is_cont<decltype(*it))>::value == true)
                str = str + printContainer(*it);
            else
                str = str + std::to_string(*it) + ",";
    return str;
}
*/
int main()
{
    std::vector<int> A({2,3,6,8});
    std::vector<std::vector<int>> M(2,A);
    M[1][0] ++;

    std::cout << is_cont<decltype(A)>::value << std::endl;  // returns true !

    for (auto it = std::begin(M); it != std::end(M); ++ it)
    {
        std::cout << printContainer(*it) << std::endl; // works well std::vector<int>
        std::cout << is_cont<decltype(*it)>::value << std::endl; // return false :(
    }

    // Want to use this for printing a std::vector<std::vector<int>>
   //  std::cout << printContainerV2(M) << std::endl; // not working !

}

For the moment it is ok if the code works only for std::vector type and up to one nested level (std::vector< std::vector>>). I am not sure it can be generic without efforts...

Upvotes: 8

Views: 1466

Answers (4)

dylan
dylan

Reputation: 289

I think my solution is recursive. I am able to print any nested vector.

main.cpp

#include "vector.h"

int main(){
    std::vector<std::vector<std::vector<int>>> e = {{{1,2,3},{4,5,6},{7,8,9}},{{9,8,7},{6,5,4},{3,2,1}}};
    std::cout << e << std::endl;
    return 0;
}

"vector.h"

#ifndef VECTOR_H
#define VECTOR_H

#include <vector>
#include <iostream>

template<typename T1>
std::ostream& operator<<(std::ostream& stream, std::vector<T1> r){
    if(r.size() == 0){
        return stream;
    }
    else{
        stream << "(";
        for(int i = 0; i < r.size(); i++){
            if(i < (r.size() - 1)){
                stream << r[i] << ", ";
            }
            else{
                stream << r[i] << ")";
            }
        }
    }
    return stream;
};

#endif

Upvotes: 2

AdamF
AdamF

Reputation: 2601

I modified your code and finally I have working solution:

#include <iostream>
#include <iterator>
#include <vector>
#include <string>

using namespace std;

template <class N>
struct is_vector { static const int value = 0; };

template <class N, class A>
struct is_vector<std::vector<N, A> > { static const int value = 1; };

struct container_true {};
struct container_false {};

template <typename T>
std::string print(T const& container);

template <typename T>
std::string printContainer(T const& container, container_true)
{
    std::string ret = "{";
    for (auto it = std::begin(container); it != std::end(container); ++it)
    {
        ret += "{";
        ret += print(*it);
        ret += "}";
    }

    ret += "}";
    return ret;
}


template <typename T>
std::string printContainer(T const& container, container_false)
{
    std::string ret;
    for (auto it = std::begin(container); it != std::end(container); ++it)
    {
        ret += to_string(*it) + ",";
    }
    return ret.erase(ret.size() - 1);;
}

template <typename T>
std::string print(T const& container)
{
    typename std::conditional<is_vector<T::value_type>::value, container_true, container_false>::type mys;
    return printContainer(container, mys);
}

int main()
{
    std::vector<int> A({ 2, 3, 6, 8 });
    std::vector<std::vector<int>> M(2, A);
    M[1][0]++;
    std::vector<std::vector<std::vector<int>>> Z(2,  M);


    std::cout << print(A) << std::endl;
    std::cout << print(M) << std::endl;
    std::cout << print(Z) << std::endl;
}

Edit: Shorter solution:

#include <iostream>
#include <vector>
#include <string>

template <class N>
struct is_vector { static const int value = 0; };

template <class N, class A>
struct is_vector<std::vector<N, A> > { static const int value = 1; };

template <typename T>
std::enable_if_t< is_vector<typename T::value_type>::value, std::string>
printContainer(T const& container)
{
    std::string ret = "{";
    for (auto& a : container)
        ret += "{" + printContainer(a) + "}";
    return ret + '}';
}

template <typename T>
std::enable_if_t< !is_vector<typename T::value_type>::value, std::string>
printContainer(T const& container)
{
    std::string ret;
    for (auto& a : container)
        ret += std::to_string(a) + ",";
    return ret.erase(ret.size() - 1);;
}

int main()
{
    std::vector<int> A({ 2, 3, 6, 8 });
    std::cout << printContainer(A) << std::endl;
}

Upvotes: 3

Super-intelligent Shade
Super-intelligent Shade

Reputation: 6449

Add #include <type_traits> header and replace your PrintContainerV2 with this:

template<typename T>
using if_not_cont = std::enable_if<!is_cont<T>::value>;

template<typename T>
using if_cont = std::enable_if<is_cont<T>::value>;

template <typename T, typename if_not_cont<T>::type* = nullptr>
std::string printContainerV2(T const& container)
{
    std::string str = "{";
    for (auto it = std::begin(container); it != std::end(container); ++ it)
        if (isLast(it, container))
                str = str + std::to_string(*it) + "}";
        else
                str = str + std::to_string(*it) + ",";
    return str;
}

template <typename T, typename if_cont<T>::type* = nullptr>
std::string printContainerV2(T const& container)
{
    std::string str = "{";
    for (auto it = std::begin(container); it != std::end(container); ++ it)
        if (isLast(it, container))
                str = str + printContainer(*it) + "}";
        else
                str = str + printContainer(*it) + ",";
    return str;
}

Upvotes: 6

Barry
Barry

Reputation: 302862

The reason your solution doesn't work is here:

        if (is_cont<decltype(*it)>::value == true)
            str = str + printContainer(*it);
        else
            str = str + std::to_string(*it) + "}"; // <====

Even though the check you are making is compile time static - both branches of the if will be compiled anyway. The compiler will still try to evalute std::to_string(std::vector<T> ) and complain about that function not existing. What we need to do instead is use SFINAE: Substitution Failure Is Not An Error.

This is our version of print for containers:

template <typename T>
std::enable_if_t<is_cont<T>::value, std::string>
print(const T& container)
{
    // mostly the same as your code from printContainer() here, except instead of
    // std::to_string(*it), call print(*it).

    std::string str = "{";
    for (auto it = std::begin(container); it != std::end(container); ++ it)
    {
        str += print(*it);

        if (isLast(it, container)) {
            str += '}';
        }
        else {
            str += ',';
        }
    }

    return str;
}

And the non-container version:

template <typename T>
std::enable_if_t<!is_cont<T>::value, std::string>
print(const T& value)
{
    return std::to_string(value);
}

This is logically the same thing you were doing in your printContainerV2(), but this way the else branch - the to_string() one - won't get compiled for the actual container version.

Upvotes: 5

Related Questions