推回静态向量<对<字符串,双精度[9]> > myVector;

Push back to a static vector< pair<string, double[9]> > myVector;

本文关键字:gt lt myVector 双精度 静态 向量 字符串      更新时间:2023-10-16

我想存储大量信息,其中包含1个字符串和9个双精度。这些信息将属于一个"项目",所以我想按名称排序,因此我决定将其放入一个成对的向量中,成对的第一部分是名称,第二部分是双数组。因此,我可以很容易地对其进行排序,也可以很轻松地访问它们。

我有一个带有静态私有数据成员"myVector"的C++类

代码如下:

class MyClass : public OtherClass{
private:
    static vector< pair<string, double[9]> > myVector;
public:
    MyClass(void);
    ~MyClass(void);
};
vector< pair<string, double[9]> > MyClass::myVector;

问题是,在这个类的.cpp中,当我尝试执行以下操作时:

myVector.push_back(make_pair(sName, dNumericData));

其中sName是字符串类型的变量,dNumericData是双数组大小为9的变量,我得到一个错误:

2   IntelliSense: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::pair<std::string, double [9]>, _Alloc=std::allocator<std::pair<std::string, double [9]>>]" matches the argument list
        argument types are: (std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char>>, double *>)
        object type is: std::vector<std::pair<std::string, double [9]>, std::allocator<std::pair<std::string, double [9]>>>

你知道我该怎么做吗?

dNumericData衰减为指针,因此参数类型不匹配。您可以将std::array<>同时用于对类型和dNumericData

我会制作一个结构或类,而不是使用std::pair:

struct MyStuff {
  string name;
  array<double, 9> values; // use float unless you need so much precision
  MyStuff(string name_, array<double, 9> values_) : name(name_), values(values_) {}
};
vector<MyStuff> v;
v.emplace_back(MyStuff("Jenny", {{8,6,7,5,3,0,9}}));