How to add structure to the vector front?

int main () { vector<int> myvector (3,100); int myarray [] = { 501,502,503 }; myvector.insert (myvector.begin(), myarray, myarray+3); return 0; } 

It works.

It does not mean:

 typedef struct { float latitude; float longitude; } coordinate; int main () { std :: vector <coordinate> previousPoints; coordinate start; start.latitude = 22.3; start.longitude = 33.4; previousPoints.insert (previousPoints.begin (), start, 1); return 0; } 

Error:

 anisha@linux-trra :~> g++ y.cpp y.cpp: In function 'int main()': y.cpp:18:58: error: no matching function for call to 'std::vector<coordinate>::insert(std::vector<coordinate>::iterator, coordinate&, int)' /usr/include/c++/4.5/bits/vector.tcc:106:5: note: candidates are: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, const value_type&) [with _Tp = coordinate, _Alloc = std::allocator<coordinate>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<coordinate*, std::vector<coordinate> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = coordinate*, value_type = coordinate] /usr/include/c++/4.5/bits/stl_vector.h:858:7: note: void std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, std::vector::size_type, const value_type&) [with _Tp = coordinate, _Alloc = std::allocator<coordinate>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<coordinate*, std::vector<coordinate> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = coordinate*, std::vector::size_type = long unsigned int, value_type = coordinate] anisha@linux-trra :~> 

What does the error mean? How are both examples different?

+4
source share
1 answer

There are no three std :: vector :: insert parameters that take a value as the second argument. If you want to paste in front, you can try

 previousPoints.insert(previousPoints.begin(), start); 

By the way, if you are going to perform this operation often, on large vectors, you might want to use std::deque instead and use its push_front , which has a complex time complexity.

+7
source

Source: https://habr.com/ru/post/1436742/


All Articles