Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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++;通过模板滥用 我决定尝试用模板编写C++中的函数映射实现,这就是我所提出的: template < class U, class V, template <class> class T > class T<V> WugMap( class T<U>::const_iterator first, class T<U>::const_iterator second, V (U::*method)() const) { class T<V> collection; while (first != second) { collection.insert(collection.end(), ((*(first++)).*method)()); } return collection; } 模板< U类, 第五类, 模板类T > T类WugMap( 类T::const_迭代器优先, 类T::常量迭代器秒, V(U::*方法)(常数) { T类收集; while(第一个!=第二个) { 插入(collection.end(),((*(first++).*方法)(); } 回收; }_C++_Templates_Collections_Functional Programming_Template Templates - Fatal编程技术网

功能C++;通过模板滥用 我决定尝试用模板编写C++中的函数映射实现,这就是我所提出的: template < class U, class V, template <class> class T > class T<V> WugMap( class T<U>::const_iterator first, class T<U>::const_iterator second, V (U::*method)() const) { class T<V> collection; while (first != second) { collection.insert(collection.end(), ((*(first++)).*method)()); } return collection; } 模板< U类, 第五类, 模板类T > T类WugMap( 类T::const_迭代器优先, 类T::常量迭代器秒, V(U::*方法)(常数) { T类收集; while(第一个!=第二个) { 插入(collection.end(),((*(first++).*方法)(); } 回收; }

功能C++;通过模板滥用 我决定尝试用模板编写C++中的函数映射实现,这就是我所提出的: template < class U, class V, template <class> class T > class T<V> WugMap( class T<U>::const_iterator first, class T<U>::const_iterator second, V (U::*method)() const) { class T<V> collection; while (first != second) { collection.insert(collection.end(), ((*(first++)).*method)()); } return collection; } 模板< U类, 第五类, 模板类T > T类WugMap( 类T::const_迭代器优先, 类T::常量迭代器秒, V(U::*方法)(常数) { T类收集; while(第一个!=第二个) { 插入(collection.end(),((*(first++).*方法)(); } 回收; },c++,templates,collections,functional-programming,template-templates,C++,Templates,Collections,Functional Programming,Template Templates,现在这些都很好,很漂亮,甚至可以编译。问题是,我不知道该怎么称呼它 尝试简单的方法会产生以下错误: prog.cpp:42: error: no matching function for call to ‘WugMap(__gnu_cxx::__normal_iterator<Container*, std::vector<Container, std::allocator<Container> > >, __gnu_cxx::__normal_iter

现在这些都很好,很漂亮,甚至可以编译。问题是,我不知道该怎么称呼它

尝试简单的方法会产生以下错误:

prog.cpp:42: error: no matching function for call to 
‘WugMap(__gnu_cxx::__normal_iterator<Container*, std::vector<Container, 
std::allocator<Container> > >, __gnu_cxx::__normal_iterator<Container*, 
std::vector<Container, std::allocator<Container> > >, int (Container::*)()const)’
prog.cpp:42:错误:没有用于调用的匹配函数
'WugMap(uu gnu cxx::u normal_迭代器,u gnu cxx::u normal_迭代器,int(容器::*)()常量)'
据我所知,所有的论点都是正确的。gcc根本没有提出任何替代方案,这让我相信我对WugMap的定义是可疑的,但它编译得很好,所以我相当迷茫。有谁能引导我度过这愚蠢的时刻吗

如果有人能提出一种更好的方法来编写这样一个函数,它将支持使用任何类型的集合,其中包含任何类型的对象,那么我将考虑修改它

我目前使用的是Ideone,它使用的是C++03,GCC4.3.4

增编1 这在C++11中可能吗?有人暗示是这样的。我知道C++11中的模板支持不同数量的参数,所以我将修改我的需求以适应这一点。我会花点精力写一些东西,但与此同时,我正在寻找以下要求:

  • 应该有如下签名:

    C2<V, ...> map(const C1<U, ...>&, V (U::*)(...), ...)
    
    C2映射(常数C1&,V(U::*)(…),…)
    
    这是通过引用获取一些集合C1,其中包含U类型的元素,使用一些默认参数构造,还获取U的一些成员函数(返回V并获取一些未知类型的参数),然后依次获取要传递给成员函数的参数。该函数最终将返回一个C2类型的集合,其中包含V类型的元素,并使用未知数量的默认参数进行初始化

  • 应可链接:

    vector<int> herp = map(
                       map(
                            set<Class1, myComparator>(),
                       &Class1::getClass2, 2, 3),
                       &Class2::getFoo);
    
    vector herp=map(
    地图(
    set(),
    &Class1::getClass2,2,3),
    &类别2:getFoo);
    
  • 如果我在使用它时不必有模板参数或任何其他额外的详细信息,则会获得额外的积分


std::transform
很棒,但不可链接。

模板参数永远无法从嵌套类型中推导出来。即使可以从成员函数指针推断出
U
V
,也无法推断模板类型
t

在链接中显式指定模板参数(在编写上述语句之前,我没有使用链接)也不起作用,主要是因为
std::vector
的模板参数不仅仅是一种类型
t
std::vector
采用值类型和分配器类型。解决问题变得相当丑陋:

#include <vector>
#include <iostream>

using namespace std;

class Container
{
public:
    Container() {}
    Container(int _i) : i(_i) {}

    int get_i() const {return i;}

    int i;
};

    template <
        class U, 
        class V, 
        template <typename...> class T 
    >

    T<V> WugMap(
        typename T<U>::const_iterator first, 
        typename T<U>::const_iterator second, 
        V (U::*method)() const)
    {
        T<V> collection;
        while (first != second)
        {
            collection.insert(collection.end(), ((*(first++)).*method)());
        }
        return collection;
    }

int main()
{
    vector<Container> containers;
    for (int i = 0; i < 10; ++i) containers.push_back((Container(i)));

    WugMap<Container, int, std::vector>(
        containers.begin(), containers.end(), &Container::get_i);
}
#包括
#包括
使用名称空间std;
类容器
{
公众:
容器(){}
容器(int_i):i(_i){
int get_i()常量{return i;}
int i;
};
模板<
U类,
第五类,
模板类T
>
T WugMap(
typename T::const_迭代器优先,
类型名T::常量迭代器秒,
V(U::*方法)(常数)
{
T收集;
while(第一个!=第二个)
{
插入(collection.end(),((*(first++).*方法)();
}
回收;
}
int main()
{
载体容器;
对于(inti=0;i<10;++i)容器,向后推(容器(i));
WugMap(
containers.begin()、containers.end()、&Container::get_i);
}

不确定这是否应该是一个答案,但请注意:

std::vector<std::string> src = f();
std::vector<std::string::size_type> sizes; 
sizes.reserve(src.size());
// Actual transformation:
std::transform( src.begin(), src.end(), std::back_inserter(sizes), 
                [](std::string const& s) { return s.size(); } );
std::vector src=f();
std::向量大小;
size.reserve(src.size());
//实际转变:
std::transform(src.begin()、src.end()、std::back_插入器(大小),
[](std::string const&s){返回s.size();});
类似的事情也可以手动完成,但彻底改造车轮是毫无意义的


至于在
std::transform
的情况下有什么不同,它并没有尝试将类型绑定得如此紧密,前两个参数需要
Iter1
,第三个参数需要
Iter2
,第三个参数需要
Functor
。界面上没有检查以保证
Iter1
Iter2
是同一类型容器中的迭代器,或者
Functor
将从第一个容器中的值类型转换为第二个容器中的值类型。

它是否需要是容器?在
中,已使用迭代器执行此操作。@JonPurdy:让我们假设std::transform不存在。另外,这个实现的目的是提供std::transform没有的一个特性是它接受一个指向成员函数的指针(我认为std::transform在没有包装器对象的情况下不能以这种方式工作)。另外,std::transform对一个集合进行了变异,这是为了产生一个可能不同类型的新集合。@DyP:你知道,这可能与它有关,尽管确切的原因我无法理解。关于是否使用
class
typename
有一些微妙的规则,除非涉及到模板,否则这些规则不会起作用。这也许可以解释为什么它是编译的,但不是一个函数。@Wug:谁说
std::transform
不能将指针应用于成员?在C++11中,可以使用通用绑定器或lambda表达式。在C++03 IIRC中,还可以将指针绑定到member和altern