使用模板化运算符<<输出一个集合的集合

时间:2019-04-18 14:08:49

标签: c++ templates vector output

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

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

有没有办法解决这个问题?

#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;
}

我遇到以下错误:

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
1>c:\users\user\source\repos\codechef\practice\beginner\test\test\test.cpp(13): note: could be 'std::ostream &operator <<<char>(std::ostream &,const T &)'
1>        with
1>        [
1>            T=char
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\ostream(921): note: or       'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,_Elem)'
1>        with
1>        [
1>            _Elem=char
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\ostream(834): note: or       'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\ostream(749): note: or       'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)'
1>c:\users\user\source\repos\codechef\practice\beginner\test\test\test.cpp(16): note: while trying to match the argument list '(std::ostream, char)'
1>c:\users\user\source\repos\codechef\practice\beginner\test\test\test.cpp(22): note: see reference to function template instantiation 'std::ostream &operator <<<std::vector<std::vector<bool,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>>(std::ostream &,const T &)' being compiled
1>        with
1>        [
1>            _Ty=bool,
1>            T=std::vector<std::vector<bool,std::allocator<bool>>,std::allocator<std::vector<bool,std::allocator<bool>>>>
1>        ]
1>Done building project "test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:5)

问题在于模板中的.example-custom-date-class不限于特定类型或类型范围。编译器可以将其替换为所需的任何类型。

编写T时,编译器会寻找一个函数out << ' ';,并会找到两个这样的函数。其中一个来自标准库,另一个是您的函数。 编译器无法决定应使用哪个版本,因此它只会停止编译。

要解决此问题,您需要限制模板,以便它不会接受不需要的类型。 一种方法是制作一个仅接受ostream& operator<< (ostream& out, const char& collection)的模板:

vector

如果需要为更多类型的容器定义此函数,而不是多次复制,可以创建一个接受所有类型但其名称不与标准库冲突的模板。然后,您可以创建一些#include <iostream> #include <vector> using namespace std; vector<vector<bool>> lookup(10, vector<bool>(10, true)); template <typename T> ostream& operator<< (ostream& out, const vector<T>& collection) { for (const auto& elem : collection) out << elem << ' '; return out << endl; } int main() { cout << lookup << endl; } 的简单实例,它们仅调用您的通用函数。

operator<<

我认为,甚至不必为每种类型的容器分别定义功能,甚至有可能。但是,这将需要一些高级模板魔术。 您可以阅读此c++ template class; function with arbitrary container type, how to define it?来了解更多信息。