繁体   English   中英

推回静态向量&lt;对 <string, double[9]> &gt; myVector;

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

我想存储一些信息,其中将包含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}}));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM