Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

c++ - Initialize a vector of pairs in one line

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

Here is my attempt:

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

The error:

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>&}’
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming Point::FT is something for which numeric_limits::max() is valid,

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...