Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ STL容器作为函数中的模板参数,调用时出错_C++_Templates_Stl_C++03 - Fatal编程技术网

C++ STL容器作为函数中的模板参数,调用时出错

C++ STL容器作为函数中的模板参数,调用时出错,c++,templates,stl,c++03,C++,Templates,Stl,C++03,无法理解main中的代码、第二个函数定义或此函数的调用是什么? 我认为,但不确定,调用中存在问题,因为不调用代码编译得很好。编译器gcc #include <iostream> #include <vector> #include <algorithm> using namespace std; template<class T> void show_element(T ob) { cout << ob << "

无法理解main中的代码、第二个函数定义或此函数的调用是什么? 我认为,但不确定,调用中存在问题,因为不调用代码编译得很好。编译器gcc

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template<class T>
void show_element(T ob)
{
    cout << ob << " ";
}

template<template<class> class S, class T>
void show_sequence(S<T> sequence)
{
    for_each(sequence.begin(), sequence.end(), show_element<T>);    
}

int main(int argc, char const *argv[])
{
    std::vector<int> v(20, 0);

    //here the problem
    show_sequence<std::vector<int>, int>(v);

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
模板
无效显示元素(T ob)
{

cout
std::vector
不是一个参数的模板,它也采用分配器类型。您可以将其用作
vector
,因为第二个参数具有默认值(
std::allocator

正如所写的,模板函数不能接受任何标准容器,因为在我看来,没有一个容器只接受一个类型参数

一种可行且不需要知道容器需要多少模板参数的方法是接受容器类型(非模板),并从容器类型中收集值类型

template<class Seq>
void show_sequence(Seq const& sequence)
{
    typedef typename Seq::value_type T;
    for_each(sequence.begin(), sequence.end(), show_element<T>);    
}
模板
无效显示顺序(顺序常量和顺序)
{
typedef typename Seq::value_type T;
对于每个元素(sequence.begin()、sequence.end()、show_元素);
}

所有标准容器都有一个
value\u type
成员,因此这将适用于其中任何一个容器。此外,它将适用于从标准库获取线索的任何容器。

std::vector
不是一个参数的模板,它还需要一个分配器类型。您可以将其用作
vector
,因为第二个参数有一个默认值(
std::allocator

正如所写的,模板函数不能接受任何标准容器,因为在我看来,没有一个容器只接受一个类型参数

一种可行且不需要知道容器需要多少模板参数的方法是接受容器类型(非模板),并从容器类型中收集值类型

template<class Seq>
void show_sequence(Seq const& sequence)
{
    typedef typename Seq::value_type T;
    for_each(sequence.begin(), sequence.end(), show_element<T>);    
}
模板
无效显示顺序(顺序常量和顺序)
{
typedef typename Seq::value_type T;
对于每个元素(sequence.begin()、sequence.end()、show_元素);
}

所有标准容器都有一个
value\u type
成员,因此这将适用于其中任何一个容器。此外,它将适用于从标准库获取线索的任何容器。

问题是
std::vector
是一个模板,而
std::vector
是一个类型

当您为函数提供第二个类型时,您提供的是一种类型而不是模板

因此,您可以将函数重写为:

template<class S>
void show_sequence(S sequence)
模板
无效显示顺序(S顺序)

此外,vector不仅采用一个模板参数,还采用两个模板参数(参见StoryTeller答案)

问题在于
std::vector
是一个模板,而
std::vector
是一个类型

当您为函数提供第二个类型时,您提供的是一种类型而不是模板

因此,您可以将函数重写为:

template<class S>
void show_sequence(S sequence)
模板
无效显示顺序(S顺序)

此外,vector不仅使用一个模板参数,还使用两个模板参数(请参见StoryTeller答案)

它类似于此问题:

这是因为向量是

代码应该是

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<class T>
void show_element(T ob)
{
    cout << ob << " ";
}
template<template<class,class> class S, class T, class Allocator>
void show_sequence(S<T, Allocator> sequence)
{
    for_each(sequence.begin(), sequence.end(), show_element<T>);
}
int main(int argc, char const *argv[])
{
    std::vector<int> v(20, 0);

    //here problem solved
    show_sequence<vector, int, allocator<int> > (v);
    show_sequence(v);

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
模板
无效显示元素(T ob)
{

cout类似于这个问题:

这是因为向量是

代码应该是

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<class T>
void show_element(T ob)
{
    cout << ob << " ";
}
template<template<class,class> class S, class T, class Allocator>
void show_sequence(S<T, Allocator> sequence)
{
    for_each(sequence.begin(), sequence.end(), show_element<T>);
}
int main(int argc, char const *argv[])
{
    std::vector<int> v(20, 0);

    //here problem solved
    show_sequence<vector, int, allocator<int> > (v);
    show_sequence(v);

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
模板
无效显示元素(T ob)
{


cout algorithms.cpp:In function'int main(int,const char**)∶algorithms.cpp:29:40:错误:调用'show_sequence(std::vector&')算法时没有匹配的函数。cpp:29:40:注意:候选函数是:算法。cpp:18:6:注意:模板void show_sequence(S)将错误消息添加到问题中。我建议您从算法函数中得到提示(例如,您使用的)并让函数使用迭代器。然后您不仅可以使用标准容器,还可以使用数组或指针。我考虑过这种方法,但这并不是我真正想要的算法。cpp:In函数“int main(int,const char**)“:algorithms.cpp:29:40:error:no matching function for call to'show_sequence(std::vector&)”algorithms.cpp:29:40:note:candidate is:algorithms.cpp:18:6:note:template void show_sequence将错误消息添加到问题中。我建议您从算法函数(例如,您使用的)中得到提示让函数使用迭代器。然后,您不仅可以使用标准容器,还可以使用数组或指针。我考虑过这种方法,但这并不是我想要的。当您编写“std::vector不是一个参数的模板”时,您的意思是什么,我必须在模板声明中为任何stl容器编写模板?我知道分配器,但这些信息在当前情况下如何帮助我?@Anton-仅仅是因为如果接受模板,您必须精确指定它需要的参数,并且传递的任何模板都必须与这些参数完全匹配。接受模板(而不是容器类型)将始终限制您可以接受的容器。例如,如果您想显示一个
std::map
,该容器模板有4个参数。其中3个参数对您不感兴趣,但您的解决方案必须将它们考虑在内。使用
value\u type
将消除这些考虑因素。用户scinart告诉我需要什么,b但是你的变体更好)谢谢你的解释,非常有用,非常有用哈是的,因为数组使用std::size\u t模板参数…有一次,我写了一个trait是可编辑的(通过检查begin()和end()函数)。为了有一个模板“template parameter”,我只有一个带检查的模板参数(通过SFINAE)如果这个类型参数是iterable。而且它对我的使用来说已经足够好了:)。当您编写“std::vec”时,您是什么意思