数组作为映射键
Array as map key
我正在尝试使用数组作为映射键。我让它工作了,但是当我尝试将它集成到 class 中时,我收到以下编译器错误:
In instantiation of ‘T& Matrix::operator[](std::array) [with T = double]’:
35:12: required from here
20:24: error: no match for call to ‘(std::map, double, std::less >, std::allocator, double> > >) (const std::array&)’
return matrix(index);
我的代码是这样的:
#include <map>
template <typename T>
struct Matrix{
std::map<std::array<int,2>,T> matrix;
int rows;
int columns;
Matrix()
: rows(0),
columns(0)
{ }
Matrix(int rows,int columns)
: rows(rows),
columns(columns)
{ }
T& operator[](const std::array<int,2> index){
return matrix(index);
}
T& operator[](const std::array<int,2>& index) const{
return matrix(index);
}
};
int main(int argc, char *argv[])
{
Matrix<double> M(10,10);
double a = 10;
M[{10,11}] = a;
return 0;
}
你的代码有一些问题:
- 缺少
array
的包含(适用于 gcc 但不适用于 clang)
- 缺少调用运算符的用法
- 使用副本而不是 const 引用
#include <array> // Added include
#include <map>
template <typename T>
struct Matrix{
std::map<std::array<int,2>,T> matrix;
int rows;
int columns;
Matrix()
: rows(0),
columns(0)
{ }
Matrix(int rows,int columns)
: rows(rows),
columns(columns)
{ }
T& operator[](const std::array<int,2> &index){
return matrix[index]; // replace call operator
}
const T& operator[](const std::array<int,2> &index) const{ //return const reference
return matrix.at(index); // replace call operator
}
};
int main(int argc, char *argv[])
{
Matrix<double> M(10,10);
double a = 10;
M[{10,11}] = a;
return 0;
}
如错误信息所说,问题出在这里:
return matrix(index);
应该是:
return matrix[index];
注意 []
运算符。
此外,matrix[index]
不能在 const
上下文中调用,因为在 C++ 中 map::operator[]
会创建丢失的元素,潜在的变异操作也是如此。我会重新考虑您的设计,否则您的非常量 []
和 const []
在丢失键方面的行为会有所不同。
注意:您还应该 #include <array>
。
P.S。 std::array
is provided.
的比较运算符
在这些运算符中
T& operator[](const std::array<int,2> index){
return matrix(index);
}
T& operator[](const std::array<int,2>& index) const{
return matrix(index);
}
您正在尝试调用 class 模板的不存在的运算符函数 std::map
matrix(index)
很明显你的意思是第一个下标运算符
matrix[index]
T& operator[](const std::array<int,2> &index){
return matrix[index];
}
并且在第二个下标运算符中的成员函数在.
const T& operator[](const std::array<int,2>& index) const{
return matrix.at(index);
}
另外,第二个运算符应该用 returned 类型声明,用限定符 const 限定,它应该 return 一个常量引用,因为成员函数又是一个常量函数。
我正在尝试使用数组作为映射键。我让它工作了,但是当我尝试将它集成到 class 中时,我收到以下编译器错误:
In instantiation of ‘T& Matrix::operator[](std::array) [with T = double]’: 35:12: required from here 20:24: error: no match for call to ‘(std::map, double, std::less >, std::allocator, double> > >) (const std::array&)’ return matrix(index);
我的代码是这样的:
#include <map>
template <typename T>
struct Matrix{
std::map<std::array<int,2>,T> matrix;
int rows;
int columns;
Matrix()
: rows(0),
columns(0)
{ }
Matrix(int rows,int columns)
: rows(rows),
columns(columns)
{ }
T& operator[](const std::array<int,2> index){
return matrix(index);
}
T& operator[](const std::array<int,2>& index) const{
return matrix(index);
}
};
int main(int argc, char *argv[])
{
Matrix<double> M(10,10);
double a = 10;
M[{10,11}] = a;
return 0;
}
你的代码有一些问题:
- 缺少
array
的包含(适用于 gcc 但不适用于 clang) - 缺少调用运算符的用法
- 使用副本而不是 const 引用
#include <array> // Added include
#include <map>
template <typename T>
struct Matrix{
std::map<std::array<int,2>,T> matrix;
int rows;
int columns;
Matrix()
: rows(0),
columns(0)
{ }
Matrix(int rows,int columns)
: rows(rows),
columns(columns)
{ }
T& operator[](const std::array<int,2> &index){
return matrix[index]; // replace call operator
}
const T& operator[](const std::array<int,2> &index) const{ //return const reference
return matrix.at(index); // replace call operator
}
};
int main(int argc, char *argv[])
{
Matrix<double> M(10,10);
double a = 10;
M[{10,11}] = a;
return 0;
}
如错误信息所说,问题出在这里:
return matrix(index);
应该是:
return matrix[index];
注意 []
运算符。
此外,matrix[index]
不能在 const
上下文中调用,因为在 C++ 中 map::operator[]
会创建丢失的元素,潜在的变异操作也是如此。我会重新考虑您的设计,否则您的非常量 []
和 const []
在丢失键方面的行为会有所不同。
注意:您还应该 #include <array>
。
P.S。 std::array
is provided.
在这些运算符中
T& operator[](const std::array<int,2> index){
return matrix(index);
}
T& operator[](const std::array<int,2>& index) const{
return matrix(index);
}
您正在尝试调用 class 模板的不存在的运算符函数 std::map
matrix(index)
很明显你的意思是第一个下标运算符
matrix[index]
T& operator[](const std::array<int,2> &index){
return matrix[index];
}
并且在第二个下标运算符中的成员函数在.
const T& operator[](const std::array<int,2>& index) const{
return matrix.at(index);
}
另外,第二个运算符应该用 returned 类型声明,用限定符 const 限定,它应该 return 一个常量引用,因为成员函数又是一个常量函数。