相关疑难解决方法(0)

Pretty-print C++ STL containers

Please take note of the updates at the end of this post.

Update: I have created a public project on GitHub for this library!


I would like to have a single template that once and for all takes care of pretty-printing all STL containers via operator<<. In pseudo code, I'm looking for something like this:

template<container C, class T, String delim = ", ", String open = "[", String close = "]">
std::ostream & operator<<(std::ostream & o, const C<T> …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading pretty-print c++11

379
推荐指数
5
解决办法
4万
查看次数

在编译时确定类型是否为STL容器

我想编写一个模板来确定类型是否是编译时的stl容器.

我有以下一点代码:

struct is_cont{};
struct not_cont{};

template <typename T>
struct is_cont { typedef not_cont result_t; };
Run Code Online (Sandbox Code Playgroud)

但我不确定如何创建必要的专业化std::vector<T,Alloc>, deque<T,Alloc>, set<T,Alloc,Comp>等...

c++ templates stl template-meta-programming

18
推荐指数
5
解决办法
1万
查看次数

模板模板参数,不指定内部类型

我想知道是否有可能拥有具有以下行为的代码:

int main()
{
    func<vector>(/*some arguments*/);
}
Run Code Online (Sandbox Code Playgroud)

也就是说,我希望用户能够指定容器而不指定其操作的类型.

例如,可能定义的某些(元)代码(不适用于上述代码)func将如下所示:

template<typename ContainerType>
int func(/*some parameters*/)
{
    ContainerType<int> some_container;
    /*
        operate on some_container here
    */
    return find_value(some_container);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates template-templates

18
推荐指数
2
解决办法
738
查看次数

使用模板化运算符&lt;&lt;输出一个集合的集合

我想通过操作符<<调用一次外部集合来输出集合的集合(在这种情况下为向量的向量)

当我' 'operator<<()函数中删除the时,它可以工作,但是我希望每行的每个输出元素之间都有一个空格。我尝试用(也包括字符串头文件)替换' '" "但得到相同的错误。

有办法解决吗?

#include <iostream>
#include <vector>

using namespace std;

vector<vector<bool>> lookup(10, vector<bool>(10, true));

template <typename T>
ostream& operator<< (ostream& out, const T& collection)
{
    for (const auto& elem : collection)
        out << elem << ' ';
    return out << endl;
}

int main()
{
    cout << lookup << endl;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>test.cpp
1>c:\users\user\source\repos\codechef\practice\beginner\test\test\test.cpp(16): error C2593: 'operator <<' is ambiguous …
Run Code Online (Sandbox Code Playgroud)

c++ templates vector output

2
推荐指数
1
解决办法
45
查看次数