Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/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++ 如何填充double的字符串和向量对的映射_C++_Vector - Fatal编程技术网

C++ 如何填充double的字符串和向量对的映射

C++ 如何填充double的字符串和向量对的映射,c++,vector,C++,Vector,用这种类型的值填充的最佳方式是什么 typedef std::map<std::string, std::pair<std::vector<double>, std::vector<double>>> buf; 提前谢谢你 向上: 所以,我来到这里。但它看起来不太好,我想: double a[] = {0.1, 0.2}; double b[] = {0.0, 0.0}; foo.insert( make_pair("box", make_pai

用这种类型的值填充的最佳方式是什么

typedef std::map<std::string, std::pair<std::vector<double>, std::vector<double>>> buf;
提前谢谢你

向上: 所以,我来到这里。但它看起来不太好,我想:

double a[] = {0.1, 0.2};
double b[] = {0.0, 0.0};
foo.insert( make_pair("box", make_pair(vector<double>(a, a + sizeof(a) / sizeof(a[0])), vector<double>(b, b + sizeof(b) / sizeof(b[0]))) ) );
double a[]={0.1,0.2};
双b[]={0.0,0.0};
foo.insert(make_pair(“box”)、make_pair(vector(a,a+sizeof(a)/sizeof(a[0])、vector(b,b+sizeof(b)/sizeof(b[0]));

您可以使用
insert

typedef std::map<std::string, std::pair<std::vector<double>, std::vector<double>>> buf;

int main()
{
    buf foo;
    foo.insert({"Label", {{1,2,3}, {100,200,300}}});
}
typedef std::map buf;
int main()
{
布福;
插入({“标签”,{1,2,3},{100200300}});
}

请注意,您需要一个封闭的
{}
来表示您的
std::pair

您可以使用
insert

typedef std::map<std::string, std::pair<std::vector<double>, std::vector<double>>> buf;

int main()
{
    buf foo;
    foo.insert({"Label", {{1,2,3}, {100,200,300}}});
}
typedef std::map buf;
int main()
{
布福;
插入({“标签”,{1,2,3},{100200300}});
}

请注意,您需要一个封闭的
{}
来表示您的
std::pair

和大量的大括号:

buf my_map {         // <== whole map
    {                // <== next individual map item
        "Label",     // <== key
        {            // <== value
            {1.0, 2.0, 3.0},       // <== value.first
            {100.0, 200.0, 300.0}  // <== value.second
        } 
    }  
};

用很多很多大括号:

buf my_map {         // <== whole map
    {                // <== next individual map item
        "Label",     // <== key
        {            // <== value
            {1.0, 2.0, 3.0},       // <== value.first
            {100.0, 200.0, 300.0}  // <== value.second
        } 
    }  
};
typedef std::map buf;
我的{
{
“标签”,
{
{1,2,3}, {100,200,300}
}
},
{
“标签2”,
{
{4,5,6}, {400,500,600}
}
}
};
typedef std::map buf;
我的{
{
“标签”,
{
{1,2,3}, {100,200,300}
}
},
{
“标签2”,
{
{4,5,6}, {400,500,600}
}
}
};
如果是C++11或更高版本

bufx={{“Label”、{{1,2,3}、{100200300};

编辑

如果不使用C++11,如果您真的想用文字填充(如示例中所示),请创建帮助器函数:

template <int N, int M>
std::pair<std::vector<double>, std::vector<double>> build_pair(double(&x)[N], double(&y)[M])
{
    return std::make_pair(std::vector<double>(x, x + N), std::vector<double>(y, y + M));
}
如果是C++11或更新版本

bufx={{“Label”、{{1,2,3}、{100200300};

编辑

如果不使用C++11,如果您真的想用文字填充(如示例中所示),请创建帮助器函数:

template <int N, int M>
std::pair<std::vector<double>, std::vector<double>> build_pair(double(&x)[N], double(&y)[M])
{
    return std::make_pair(std::vector<double>(x, x + N), std::vector<double>(y, y + M));
}

如果您的意思是向量已经存在,并且在构建时没有使用文本值初始化映射,那么您通常会使用创建向量对以及进入映射的键/值对

#include <utility>

buf my_map;
my_map.insert(std::make_pair(label, std::make_pair(vector1, vector2)));
#包括
我的地图;
my_map.insert(std::make_pair(标签,std::make_pair(向量1,向量2));

如果您的意思是向量已经存在,并且在构建时没有使用文本值初始化映射,那么您通常会使用创建向量对以及进入映射的键/值对

#include <utility>

buf my_map;
my_map.insert(std::make_pair(label, std::make_pair(vector1, vector2)));
#包括
我的地图;
my_map.insert(std::make_pair(标签,std::make_pair(向量1,向量2));

为了简化(使用编译时常量)填充映射的创建,我创建了如下模板:

(“Label”, {1,2,3}, {100,200,300})
#include <map>
#include <type_traits>
template<typename... Ts>
constexpr auto make_map(Ts&&... ts)
    -> std::map<typename std::common_type_t<Ts...>::first_type,typename std::common_type_t<Ts...>::second_type>
{
    return { std::forward<Ts>(ts)... };
}//---------------------------------------------------------
using namespace std;
auto myDict = make_map(make_pair(666,string("the number of the beast"))
                      ,make_pair(667,string("the neighbor of the beast"))
                      );
using namespace std;
auto myDict = make_map(make_pair(string("label")
                                , make_pair(make_vector(1.,2.,3.)
                                           ,make_vector(100.,200.,300.)
                                           )
                                )
                      );
将myDict创建为“map

在您的情况下,也可以这样使用:

(“Label”, {1,2,3}, {100,200,300})
#include <map>
#include <type_traits>
template<typename... Ts>
constexpr auto make_map(Ts&&... ts)
    -> std::map<typename std::common_type_t<Ts...>::first_type,typename std::common_type_t<Ts...>::second_type>
{
    return { std::forward<Ts>(ts)... };
}//---------------------------------------------------------
using namespace std;
auto myDict = make_map(make_pair(666,string("the number of the beast"))
                      ,make_pair(667,string("the neighbor of the beast"))
                      );
using namespace std;
auto myDict = make_map(make_pair(string("label")
                                , make_pair(make_vector(1.,2.,3.)
                                           ,make_vector(100.,200.,300.)
                                           )
                                )
                      );
(“make_vector”的定义与“make_map”非常相似)

make_…方法很有用(或者至少“对我来说似乎是”),因为它通过从参数中获取类型来省略显式模板类型声明


也许这对其他人也有帮助(或者至少鼓舞人心:-)…

为了简化(使用编译时常数)填充贴图的创建,我创建了如下模板:

(“Label”, {1,2,3}, {100,200,300})
#include <map>
#include <type_traits>
template<typename... Ts>
constexpr auto make_map(Ts&&... ts)
    -> std::map<typename std::common_type_t<Ts...>::first_type,typename std::common_type_t<Ts...>::second_type>
{
    return { std::forward<Ts>(ts)... };
}//---------------------------------------------------------
using namespace std;
auto myDict = make_map(make_pair(666,string("the number of the beast"))
                      ,make_pair(667,string("the neighbor of the beast"))
                      );
using namespace std;
auto myDict = make_map(make_pair(string("label")
                                , make_pair(make_vector(1.,2.,3.)
                                           ,make_vector(100.,200.,300.)
                                           )
                                )
                      );
将myDict创建为“map

在您的情况下,也可以这样使用:

(“Label”, {1,2,3}, {100,200,300})
#include <map>
#include <type_traits>
template<typename... Ts>
constexpr auto make_map(Ts&&... ts)
    -> std::map<typename std::common_type_t<Ts...>::first_type,typename std::common_type_t<Ts...>::second_type>
{
    return { std::forward<Ts>(ts)... };
}//---------------------------------------------------------
using namespace std;
auto myDict = make_map(make_pair(666,string("the number of the beast"))
                      ,make_pair(667,string("the neighbor of the beast"))
                      );
using namespace std;
auto myDict = make_map(make_pair(string("label")
                                , make_pair(make_vector(1.,2.,3.)
                                           ,make_vector(100.,200.,300.)
                                           )
                                )
                      );
(“make_vector”的定义与“make_map”非常相似)

make_…方法很有用(或者至少“对我来说似乎是”),因为它通过从参数中获取类型来省略显式模板类型声明



也许这对其他人也有帮助(或者至少是鼓舞人心的:-)…

你能使用boost库吗?(参见:它模拟了一些C++11功能)并且没有boost。这对我来说很痛苦。所以你只能使用标准的
推回
插入
生成对
:(使用数组到向量构造器eah,您可能可以简化一点。我认为这是唯一的方法。那么构造应该是什么呢?我添加了帮助函数,看看这是否足以让它更干净:)您可以使用boost库吗?(请参阅:它模拟了一些C++11特性)也没有提升。这对我来说很痛苦。所以你只剩下标准的
推回
插入
配对
:(使用数组到向量构造器eah,您可能可以简化一点。我认为这是唯一的方法。那么构造应该是什么呢?我添加了helper函数,看看这是否足以让它更简洁:)我在“{我在VS 2010中的语法错误:missing')”之前的“{我在VS 2010中的语法错误:missing')”{对不起,VS2010在这个结构上出现语法错误。对不起,VS2010在这个结构上出现语法错误。不,开始时没有向量。一切都是从头开始的。不,一开始就没有向量。一切都是从头开始的。我一点也不奇怪。找一个真正的编译器。我在这方面有限制。当然,我宁愿用智能的方式来做注释上面的n是c++11。对于较旧的编译器,您需要根据其他答案单独添加项:(我并不感到惊讶。找一个真正的编译器kiddo。我对此有限制。当然,我更愿意以智能方式进行。上面的符号是c++11。对于较旧的编译器,您需要根据其他答案单独添加项:(