C++ 如何基于变量类型创建向量

C++ 如何基于变量类型创建向量,c++,C++,我使用一个模板函数,目标是接收一个向量和一个函数,并返回函数类型 模板 自动应用(常量标准::向量&V、常量函数&F){ 向量x;#这里有错误 返回x; } 但是IDE给了我错误(): /usr/local/include/c++/10.2.0/ext/new\u allocator.h:在“class\uu gnu\u cxx::new\u allocator”的实例化中: /usr/local/include/c++/10.2.0/bits/allocator.h:116:11:从'cla

我使用一个模板函数,目标是接收一个向量和一个函数,并返回函数类型

模板
自动应用(常量标准::向量&V、常量函数&F){
向量x;#这里有错误
返回x;
}
但是IDE给了我错误():

/usr/local/include/c++/10.2.0/ext/new\u allocator.h:在“class\uu gnu\u cxx::new\u allocator”的实例化中:
/usr/local/include/c++/10.2.0/bits/allocator.h:116:11:从'class std::allocator'中需要
/usr/local/include/c++/10.2.0/bits/stl_vector.h:87:21:必须来自“struct std::_vector_base”
/usr/local/include/c++/10.2.0/bits/stl_vector.h:389:11:必须来自“class std::vector”
main.cpp:10:22:auto apply(const std::vector&,const Function&)[with T=int;Function=double(double)]中的必填项
main.cpp:19:39:从这里开始需要
/usr/local/include/c++/10.2.0/ext/new_allocator.h:96:7:error:'const\u Tp*\uu gnu cxx::new_allocator::address(u gnu cxx::new_allocator::const_reference)const[with Tp=double(double);u gnu cxx::new_分配器::const_指针=double(*)(double);u gnu cxx::new_u分配器::const_u引用=double()'不能用“\u-Tp*\u-gnu-cxx::new\u分配器::address(\u-gnu-cxx::new\u分配器::reference)const[with _-Tp=double(double);\u-gnu-cxx::new\u分配器::pointer=double(*)(double);\u-gnu-cxx::new\u分配器::reference=double(&)(double)]重载'
96 |地址(常数参考)常数GLIBCXX |无例外
|       ^~~~~~~
/usr/local/include/c++/10.2.0/ext/new_allocator.h:92:7:注意:以前的声明“_-Tp*_-gnu-cxx::new_-allocator::address(u-gnu-cxx::new_-allocator::reference)const[with_-Tp=double(double);u-gnu-cxx::new_-allocator::pointer::pointer=double(*)(double);u-gnu-cxx::new_-allocator::reference=double(&)(double)]”
92 |地址(参考)常数| GLIBCXX |无例外
|       ^~~~~~~
main.cpp:在函数“int main(int,char**)”中:
main.cpp:19:31:错误:请求从“vector”转换为非标量类型“vector”
19 |向量r=::应用(v,seno);
|                        ~~~~~~~^~~~~~~~~
这是主函数的调用

double seno(double n){返回sin(n);}
int main(int argc,char*argv[]){
向量v{1,2,3,4,5};
向量r=::应用(v,seno);
cout
vector x;//此处的错误定义了函数指针的向量。但这不是您想要的-您想要的是函数返回类型的向量。这就是
decltype()
的用途

在apply函数中,
F
是要调用的函数,
T
是传入向量中的值的类型。这意味着
T()
是向量中项的默认值(在本例中,int的默认值为0)。然后,
F(T())
实际上会使用0调用函数并返回某个内容,因此
decltype(F(T())
会告诉您返回的内容的类型

这意味着您需要编写向量x;

T()
之所以有效,是因为类型是
int
并且它是默认可构造的。正如@alterigel在注释
std::declval()
中所说,当类型不是默认可构造的时更好

因此,在某些情况下可能需要
向量x;

整个计划将如下所示:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

template <typename T, typename Function>
auto apply(const std::vector<T>& V, const Function &F) {
    vector<decltype(F(T()))> x;
    for(auto a : V)
        x.push_back(F(a));
    return x;
}

double seno( double n ) { return sin(n); }

int main( int argc, char* argv[]) {
    vector<int> v{ 1, 2, 3, 4, 5 };
    vector<double> r = ::apply(v, seno);
    for (auto a : r)
        cout << a << " ";
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
样板
自动应用(常量标准::向量&V、常量函数&F){
向量x;
用于(自动a:V)
x、 推回(F(a));
返回x;
}
双seno(双n){返回sin(n);}
int main(int argc,char*argv[]){
向量v{1,2,3,4,5};
向量r=::应用(v,seno);
用于(自动a:r)

你能不能组合一个来提供更多的上下文?提示:
vector
vs.
vector
如果你想硬编码一个类型而不是使用模板,你会写什么?为什么你想要一个名为
apply
的函数来创建函数向量?你不想将函数应用到vec中的值吗tor并生成一个双精度向量?是否希望
:应用
返回
向量
向量
?因为现在您正试图同时执行这两个操作,这就是错误说明的原因。
decltype(F(std::declval())如果
T
不是默认可构造的,那么
将是一个更好的选择;如果C++17是可构造的,则更好的选择是
std::invoke\u result\u T
available@alterigel谢谢你的建议,我会修改答案的。
/usr/local/include/c++/10.2.0/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<double(double)>':
/usr/local/include/c++/10.2.0/bits/allocator.h:116:11:   required from 'class std::allocator<double(double)>'
/usr/local/include/c++/10.2.0/bits/stl_vector.h:87:21:   required from 'struct std::_Vector_base<double(double), std::allocator<double(double)> >'
/usr/local/include/c++/10.2.0/bits/stl_vector.h:389:11:   required from 'class std::vector<double(double), std::allocator<double(double)> >'
main.cpp:10:22:   required from 'auto apply(const std::vector<T>&, const Function&) [with T = int; Function = double(double)]'
main.cpp:19:39:   required from here
/usr/local/include/c++/10.2.0/ext/new_allocator.h:96:7: error: 'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = double(double); __gnu_cxx::new_allocator<_Tp>::const_pointer = double (*)(double); __gnu_cxx::new_allocator<_Tp>::const_reference = double (&)(double)]' cannot be overloaded with '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = double(double); __gnu_cxx::new_allocator<_Tp>::pointer = double (*)(double); __gnu_cxx::new_allocator<_Tp>::reference = double (&)(double)]'
   96 |       address(const_reference __x) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~
/usr/local/include/c++/10.2.0/ext/new_allocator.h:92:7: note: previous declaration '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = double(double); __gnu_cxx::new_allocator<_Tp>::pointer = double (*)(double); __gnu_cxx::new_allocator<_Tp>::reference = double (&)(double)]'
   92 |       address(reference __x) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~
main.cpp: In function 'int main(int, char**)':
main.cpp:19:31: error: conversion from 'vector<double(double),allocator<double(double)>>' to non-scalar type 'vector<double,allocator<double>>' requested
   19 |     vector<double> r = ::apply(v, seno);
      |                        ~~~~~~~^~~~~~~~~
double seno( double n ) { return sin(n); }

int main( int argc, char* argv[]) {

    vector<int> v{ 1, 2, 3, 4, 5 };
    vector<double> r = ::apply(v, seno);
    cout << r;

    return 0;
}
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

template <typename T, typename Function>
auto apply(const std::vector<T>& V, const Function &F) {
    vector<decltype(F(T()))> x;
    for(auto a : V)
        x.push_back(F(a));
    return x;
}

double seno( double n ) { return sin(n); }

int main( int argc, char* argv[]) {
    vector<int> v{ 1, 2, 3, 4, 5 };
    vector<double> r = ::apply(v, seno);
    for (auto a : r)
        cout << a << " ";
    return 0;
}