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++;重载[]以使用模板访问子向量_C++_Templates_Vector_Operator Overloading - Fatal编程技术网

C++ C++;重载[]以使用模板访问子向量

C++ C++;重载[]以使用模板访问子向量,c++,templates,vector,operator-overloading,C++,Templates,Vector,Operator Overloading,我想重载[]向量操作符,以便轻松创建临时子向量 我想它会像下面显示的代码一样。但是,当我尝试编译时,我得到了错误: 错误:“运算符[]”必须是成员函数 其中[]是向量的成员 #include <vector> #include <algorithm> using namespace std template <class T, int a, int b> T& operator[](int a, int b) { vector<cla

我想重载[]向量操作符,以便轻松创建临时子向量

我想它会像下面显示的代码一样。但是,当我尝试编译时,我得到了错误:

错误:“运算符[]”必须是成员函数

其中[]是向量的成员

#include <vector>
#include <algorithm>

using namespace std

template <class T, int a, int b>
T& operator[](int a, int b)
{
    vector<class T> subvector;
    copy ( T.begin() + min(a, b), v1.begin() + max(a, b) + 1, back_inserter(subvector) );

    if (a > b) {reverse(subvector.begin(), subvector.end());};
}
#包括
#包括
使用名称空间std
模板
T&operator[](内部a、内部b)
{
向量子向量;
复制(T.begin()+min(a,b),v1.begin()+max(a,b)+1,back_插入器(子向量));
如果(a>b){reverse(subvector.begin(),subvector.end());};
}

根据C++标准

< <代码> < p> 13.5.5认购

1运算符[]应为具有一个参数的非静态成员函数。它实现了订阅

语法 后缀表达式[表达式]

后缀表达式[大括号初始化列表]

因此,对于类型为的类对象x,下标表达式x[y]被解释为
x.operator[](y)

如果<>(T1)存在,如果操作符被过载解决机制(133.3)选择为最佳匹配函数,则

< P>根据C++标准

13.5.5认购

1运算符[]应为具有一个参数的非静态成员函数。它实现了订阅

语法 后缀表达式[表达式]

后缀表达式[大括号初始化列表]

因此,对于类型为的类对象x,下标表达式x[y]被解释为
x.operator[](y)

如果<>(T1)存在,如果操作符被过载解决机制(133.3)选择为最佳匹配函数,则

< P>根据C++标准

13.5.5认购

1运算符[]应为具有一个参数的非静态成员函数。它实现了订阅

语法 后缀表达式[表达式]

后缀表达式[大括号初始化列表]

因此,对于类型为的类对象x,下标表达式x[y]被解释为
x.operator[](y)

如果<>(T1)存在,如果操作符被过载解决机制(133.3)选择为最佳匹配函数,则

< P>根据C++标准

13.5.5认购

1运算符[]应为具有一个参数的非静态成员函数。它实现了订阅

语法 后缀表达式[表达式]

后缀表达式[大括号初始化列表]

因此,对于类型为的类对象x,下标表达式x[y]被解释为
x.operator[](y)

T如果
T::operator[](T1)
存在,并且如果重载解析机制(13.3.3)选择了该运算符作为最佳匹配函数。

而像
operator+
operator>
这样的一些运算符可以作为独立函数存在,具有特定数量的参数,则
operator[]
不能。它必须是类的成员函数

您试图实现的目标也无法实现,因为
operator[]
只能接受一个参数

只需使用一个函数:

template<typename T>
std::vector<T> make_sub_vector(std::vector<T> const& v, int begin, int end) {
    assert(begin < end); // Either this, or you swap them 
                         // if this condition is not met.
                         // I'm asserting because I don't know 
                         // what to do when they're equal.
    return std::vector<T>(v.begin() + begin, v.begin() + end);
}
模板
标准::向量生成子向量(标准::向量常量&v,整数开始,整数结束){
assert(begin
虽然像
operator+
operator>
这样的一些操作符可以作为独立函数存在,并带有特定数量的参数,但
operator[]
不能。它必须是类的成员函数

您试图实现的目标也无法实现,因为
operator[]
只能接受一个参数

只需使用一个函数:

template<typename T>
std::vector<T> make_sub_vector(std::vector<T> const& v, int begin, int end) {
    assert(begin < end); // Either this, or you swap them 
                         // if this condition is not met.
                         // I'm asserting because I don't know 
                         // what to do when they're equal.
    return std::vector<T>(v.begin() + begin, v.begin() + end);
}
模板
标准::向量生成子向量(标准::向量常量&v,整数开始,整数结束){
assert(begin
虽然像
operator+
operator>
这样的一些操作符可以作为独立函数存在,并带有特定数量的参数,但
operator[]
不能。它必须是类的成员函数

您试图实现的目标也无法实现,因为
operator[]
只能接受一个参数

只需使用一个函数:

template<typename T>
std::vector<T> make_sub_vector(std::vector<T> const& v, int begin, int end) {
    assert(begin < end); // Either this, or you swap them 
                         // if this condition is not met.
                         // I'm asserting because I don't know 
                         // what to do when they're equal.
    return std::vector<T>(v.begin() + begin, v.begin() + end);
}
模板
标准::向量生成子向量(标准::向量常量&v,整数开始,整数结束){
assert(begin
虽然像
operator+
operator>
这样的一些操作符可以作为独立函数存在,并带有特定数量的参数,但
operator[]
不能。它必须是类的成员函数

您试图实现的目标也无法实现,因为
operator[]
只能接受一个参数

只需使用一个函数:

template<typename T>
std::vector<T> make_sub_vector(std::vector<T> const& v, int begin, int end) {
    assert(begin < end); // Either this, or you swap them 
                         // if this condition is not met.
                         // I'm asserting because I don't know 
                         // what to do when they're equal.
    return std::vector<T>(v.begin() + begin, v.begin() + end);
}
模板
标准::向量生成子向量(标准::向量常量&v,整数开始,整数结束){
assert(begin