int main ()
{
vector<int> myvector (3,100);
int myarray [] = { 501,502,503 };
myvector.insert (myvector.begin(), myarray, myarray+3);
return 0;
}
That works.
This doesn’t:
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’s the error talking about? How are the both examples different?
There is no three parameter std::vector::insert method taking a value as second argument. If you want to insert at the front, you could try
By the way, if you are going to be performing this operation often, on large vectors, you may want to consider using an
std::dequeinstead, and using it’s push_front method, which has complex time complexity.