在一行中初始化向量对 [英] Initialize a vector of pairs in one line

查看:64
本文介绍了在一行中初始化向量对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 k 个对象初始化std :: vector(std :: pair的对象),并带有如下所示的一对值。

I want to initialize a std::vector (of std::pair), with k objects, with the pair of values shown below.

这是我的尝试:

// int k
std::vector <std::pair<Point::FT, int> >
    v(k, (std::numeric_limits<FT>::max(), -1));

错误:

usr/include/c++/4.6/bits/stl_vector.h: In member function ‘void std::vector<T, Allocator>::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int, _Tp = std::pair<float, int>, _Alloc = std::allocator<std::pair<float, int> >]’:
/usr/include/c++/4.6/bits/stl_vector.h:340:4:   instantiated from ‘std::vector<T, Allocator>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = int, _Tp = std::pair<float, int>, _Alloc = std::allocator<std::pair<float, int> >, std::vector<T, Allocator>::allocator_type = std::allocator<std::pair<float, int> >]’
../Random_kd_tree.h:139:50:   instantiated from ‘void Random_kd_tree<DivisionSpace, Tree>::search_nn(std::vector<float>&, int, std::vector<std::pair<float, int> >&) [with DivisionSpace = Division_Euclidean_space, Tree = RKD<Division_Euclidean_space>]’
../main.cpp:51:30:   instantiated from here
/usr/include/c++/4.6/bits/stl_vector.h:1080:4: error: no matching function for call to ‘std::vector<std::pair<float, int> >::_M_fill_initialize(std::vector<std::pair<float, int> >::size_type, int&)’
/usr/include/c++/4.6/bits/stl_vector.h:1080:4: note: candidate is:
/usr/include/c++/4.6/bits/stl_vector.h:1122:7: note: void std::vector<T, Allocator>::_M_fill_initialize(std::vector<T, Allocator>::size_type, const value_type&) [with _Tp = std::pair<float, int>, _Alloc = std::allocator<std::pair<float, int> >, std::vector<T, Allocator>::size_type = unsigned int, std::vector<T, Allocator>::value_type = std::pair<float, int>]
/usr/include/c++/4.6/bits/stl_vector.h:1122:7: note:   no known conversion for argument 2 from ‘int’ to ‘const value_type& {aka const std::pair<float, int>&}’


推荐答案

您只需在构造函数参数周围切换到 { } ...

You can just switch to { } around the constructor arguments...

std::vector<std::pair<Point::FT, int>> v{k, {std::numeric_limits<FT>::max(), -1}};

查看它正在运行此处

这篇关于在一行中初始化向量对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆