C++;-如何制作查询矩阵的静态字典 我试图编写一个C++类,它允许我通过字符串查找访问某些矩阵元素。我想创建一个“静态”类来实现这一点,例如: #include <unordered_map> namespace Mine { static double AA[3][4] = { {5.04964676394959,-0.693207030363152,0.0422140829479668,-0.000968959310672217}, {2.6044054979329,0.288475262243944,-0.0208805589126506,0.000380899394040856}, {-4.32707864788065,1.07090008760872,-0.0777874445746693,0.00165150952598117} }; static unordered_map<std::string, double[3][4]> Mine::parameter_store = { {"AA", AA}}; 是否有可能在C++中用这种方式构建查找表?p>
您可以尝试在结构/类中包装double[3][4]C++;-如何制作查询矩阵的静态字典 我试图编写一个C++类,它允许我通过字符串查找访问某些矩阵元素。我想创建一个“静态”类来实现这一点,例如: #include <unordered_map> namespace Mine { static double AA[3][4] = { {5.04964676394959,-0.693207030363152,0.0422140829479668,-0.000968959310672217}, {2.6044054979329,0.288475262243944,-0.0208805589126506,0.000380899394040856}, {-4.32707864788065,1.07090008760872,-0.0777874445746693,0.00165150952598117} }; static unordered_map<std::string, double[3][4]> Mine::parameter_store = { {"AA", AA}}; 是否有可能在C++中用这种方式构建查找表?p>,c++,c++11,matrix,C++,C++11,Matrix,您可以尝试在结构/类中包装double[3][4] structure myMatrix { double arr[3][4]; //if you want to initialize it myMatrix(double[3][4] p){ //copy matrix here } }; 此处使用的矩阵模板会使每一行存储其长度,即使这是多余的。您可以通过使用std::array(但由于这是类型信息的一部分,所以您被锁定在每个具有相同维度的矩阵中)或使用一些类似于Boost的库来提供多
structure myMatrix {
double arr[3][4];
//if you want to initialize it
myMatrix(double[3][4] p){
//copy matrix here
}
};
此处使用的
矩阵
模板会使每一行存储其长度,即使这是多余的。您可以通过使用std::array
(但由于这是类型信息的一部分,所以您被锁定在每个具有相同维度的矩阵中)或使用一些类似于Boost的库来提供多维数组来避免这种情况。这是一个非常小的低效率,除非您知道需要,否则最好不要担心这一点。我可以像以前一样初始化它吗?所有矩阵的维度都相同吗?谢谢!还有一个注意事项,这需要-std=c++11
structure myMatrix {
double arr[3][4];
//if you want to initialize it
myMatrix(double[3][4] p){
//copy matrix here
}
};
#include <unordered_map>
#include <vector>
namespace Mine{
template<class T>
using Matrix = std::vector<std::vector<T>>;
Matrix<double> AA = {
{5.04964676394959,-0.693207030363152,0.0422140829479668,-0.000968959310672217},
{2.6044054979329,0.288475262243944,-0.0208805589126506,0.000380899394040856},
{-4.32707864788065,1.07090008760872,-0.0777874445746693,0.00165150952598117}
};
static std::unordered_map<std::string, Matrix<double>* > parameter_store = { {"AA", &AA}};
}
#include <iostream>
int main()
{
std::cout << (*Mine::parameter_store["AA"])[0][0] << std::endl;
std::cout << (*Mine::parameter_store["AA"])[0][1] << std::endl;
std::cout << (*Mine::parameter_store["AA"])[1][2] << std::endl;
}
5.04965
-0.693207
-0.0208806