如何打印一个n维c++ STL容器?容器是数组的数组或向量的向量



我有一个代码,我想显示张量表示为向量的向量或std::数组的std::数组。这样做的目的是用numpy打印的方式来打印它们。我还在用c++学习元编程,并想探索如何使用函数模板打印n-dim容器,该函数模板可以接受这个容器的容器并递归地迭代它,并返回一个字符串,我可以稍后计算。

Numpy例子:

>>> np.ones([2,2])
array([[1., 1.],
[1., 1.]])
>>> np.ones([2,2,4])
array([[[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 1., 1.],
[1., 1., 1., 1.]]])
>>> np.ones(4)
array([1., 1., 1., 1.])
>>> 

试过了:我尝试了这里接受的响应:

在c++中递归地打印STL向量

它确实为我工作了2个dim向量,但当我将printContainer()的调用更改为printContainerV2()中的printContainerV2()时,我的编译失败了3d向量:

:

#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>
using if_not_cont = std::enable_if<!is_cont<T>::value, T>;
template<typename T>
using if_cont = std::enable_if<is_cont<T>::value, T>;
template <typename T, typename std::enable_if<!is_cont<T>::value, 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 std::enable_if<is_cont<T>::value, 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 + printContainerV2(*it) + "}";
else
str = str + printContainerV2(*it) + ",";
return str;
}
int main()
{
std::vector<int> A({2,3,6,8});
std::vector<std::vector<int>> M(2,A);
std::vector<std::vector<std::vector<float>>> m3{{{1,2}, {3,4}},{{5,6}, {7,8}},{{1,2}, {5,9}}};
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 !
std::cout << printContainerV2(m3) << std::endl; // not working
}

命令:clang++ --std=c++17 test.cppTest.cpp是上面代码的名称。

我得到了这个错误:

test.cpp:45:20: error: no matching function for call to 'begin'
for (auto it = std::begin(container); it != std::end(container); ++ it)
^~~~~~~~~~
test.cpp:59:29: note: in instantiation of function template specialization 'printContainerV2<int, nullptr>'
requested here
str = str + printContainerV2(*it) + "}";
^
test.cpp:59:29: note: in instantiation of function template specialization 'printContainerV2<std::__1::vector<int,
std::__1::allocator<int> >, nullptr>' requested here
test.cpp:80:19: note: in instantiation of function template specialization
'printContainerV2<std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >,
std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >, nullptr>' requested here
std::cout << printContainerV2(M) << std::endl; // not working !
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/initializer_list:99:1: note: 
candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'int'
begin(initializer_list<_Ep> __il) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1753:1: note: 
candidate template ignored: could not match '_Tp [_Np]' against 'const int'
begin(_Tp (&__array)[_Np])
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1771:1: note: 
candidate template ignored: substitution failure [with _Cp = const int]: member reference base type
'const int' is not a structure or union
begin(_Cp& __c) -> decltype(__c.begin())
^                              ~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1779:1: note: 
candidate template ignored: substitution failure [with _Cp = int]: member reference base type 'const int' is
not a structure or union
begin(const _Cp& __c) -> decltype(__c.begin())
^                                    ~
1 error generated.

下面是一个打印任意维度向量的简单方法:

template<typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T> &vec){
out << "[ ";
for(const auto& t: vec){
out << t << " ";
}
out << "] ";
return out;
}

对于任何其他容器,您都可以执行相同的操作。

使用:

int main()
{
std::vector<int> v{1,2,3};
std::cout << v << std::endl;
std::vector<std::vector<std::vector<int>>> v_ds
{
{{{1,2,3},{4,5,6}},{{7,8},{9,10}}, {{1,2,3},{4,5,6}},{{7,8},{9,10}}}
};
std::cout << v_ds << std::endl;
return 0;
}
编辑:

下面是operator<<的一个版本可以输出任何可以迭代的对象(比如std::array、std::vector、std::list,甚至是边界已知的c数组):

template<typename Container, typename = 
std::enable_if_t<std::is_same_v<std::void_t<
decltype(static_cast<typename Container::const_iterator (*)(const Container&)>(&std::cbegin)),
decltype(static_cast<typename Container::const_iterator (*)(const Container&)>(&std::cend))>, void>
&& !std::is_same_v<std::string, Container>>>
std::ostream& operator<<(std::ostream& out, const Container &vec)
{
out << "[ ";
for(const auto& t: vec){
out << t << " ";
}
out << "] ";
return out;
}

很乱,我知道,但这是语法糖:)

另一个编辑:我之前发布的版本在使用c数组时是微妙的错误。以下是更新后的版本:

template<class...> constexpr bool true_t = true; // replacement of void_t
template<typename Container>
auto operator<<(std::ostream& out, Container&& vec) -> 
std::enable_if_t<
true_t<
decltype(std::cbegin(vec)),
decltype(std::cend(vec))
>
&& !std::is_convertible_v<Container, std::string_view>,
std::ostream&
>
{
out << "[ ";
for(const auto& t: vec){
out << t << " ";
}
out << "] ";
return out;
}

当您的第一个printContainerV2被启用时,!is_cont<T>::valuetrue,这意味着您的T此时不再是container类型。在你的例子中,它们是int,所以你不能在int上调用std::begin,你应该直接返回std::to_string(value)

template <typename T, typename std::enable_if<!is_cont<T>::value, T>::type* = nullptr>
std::string printContainerV2(T const& value)
{
return std::to_string(value);
}

演示。

对于c++ 17中的嵌套容器,区分类型,您可以这样做:

#include <bits/stdc++.h>
using namespace std;
template <typename, typename = void> constexpr bool is_iterable{};
template <> constexpr bool is_iterable<string> = false;
template <typename T, size_t N> constexpr bool is_iterable<T[N]> = true;
template <typename T> constexpr bool is_iterable<T, void_t<decltype(declval<T>().begin()), decltype(declval<T>().end())>> = true;
template <typename> constexpr bool is_set{};
template <typename T> constexpr bool is_set<set<T>> = true;
template <typename T, typename U> constexpr bool is_set<map<T, U>> = true;
template <typename> constexpr bool is_tuple{};
template <typename... T> constexpr bool is_tuple<tuple<T...>> = true;
template <typename T, typename U> constexpr bool is_tuple<pair<T, U>> = true;
inline string to_string(string s) { return '"' + s + '"'; }
inline string to_string(char c) { return ''' + string(1, c) + '''; }
template<typename T>
string print(const T& t, string&& s = string(), size_t depth = 0) {
constexpr size_t TAB_SPACES = 2;
if constexpr (!is_tuple<T> and !is_iterable<T>) {
if (s.back() == 'n') s.pop_back();
s += to_string(t) + ", ";
} else {
pair<string, string> braces = is_tuple<T> ? pair("(", ")") : is_set<T> ? pair("{", "}") : pair("[", "]");
s += string(TAB_SPACES * depth, ' ') + braces.first + 'n';
if constexpr (is_tuple<T>)
if constexpr (tuple_size_v<T> == 0) s.push_back('');
else apply([&s, depth](const auto&... x){ ((s = print(x, move(s), depth + 1)), ...); }, t);
else if (begin(t) == end(t)) s.push_back('');
else for_each(begin(t), end(t), [&s, depth](const auto& x){ s = print(x, move(s), depth + 1); });
s.erase(s.length() - 2);
if (s.back() == ')' or s.back() == '}' or s.back() == ']') s += 'n' + string(TAB_SPACES * depth, ' ');
s += braces.second + ",n";
}
if (depth == 0) cout << s.erase(s.length() - 2) << endl;
return move(s);
}

:

vector<map<int, string>> ex = {{{1, "a"}, {2, "b"}}};
print(ex);

将打印:

[
{
(1, "a"),
(2, "b")
}
]

用于优先考虑简单性和无外部深度的脚本模板。