2012-09-28 3 views
4
int main() 
{ 
    vector<int> myvector (3,100); 

    int myarray [] = { 501,502,503 }; 
    myvector.insert (myvector.begin(), myarray, myarray+3); 

    return 0; 
} 

그건 그렇습니다.벡터 앞면에 구조를 추가하는 방법은 무엇입니까?

이되지 않습니다

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; 
} 

오류 :

[email protected]:~> 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] 
[email protected]:~> 

오류가 무슨 말? 두 가지 예가 다른 점은 무엇입니까?

+1

이 오류에 대해 어리 석을 생각하지 마십시오. C++ 컴파일러 오류 메시지는 기본적으로 쓸모가 없습니다. "여기에 오류가 있지만 의미는 무엇인지 알려주지 않습니다 ... neener neener"입니다. 그들은 너무 나빠서 오류 메시지를 암호 해독하는 것만으로 생계를 유지하는 회사조차 있습니다. – 6502

+1

@ 6502 첫 번째 줄은 확실히 말하고 있습니다 : 코드가 사용하려고 시도한 생성자가 존재하지 않는다고합니다. – juanchopanza

+1

@juanchopanza : 진짜 재미있는 것은 다른 파일에서 const를 잊어 버리고 오류 메시지가 일반 문자보다 밑줄이 많은 이름이있는 표준 include 파일의 헛소리에서 직접 말도 안되는 것입니다. 이 특정 경우에는 오류가 ok (C++ 설정의 경우)였습니다. 하지만 좋은 오류는 "두 번째 매개 변수 앞에 누락되었습니다."... – 6502

답변

7

두 번째 인수로 값을 취하는 세 매개 변수 std::vector::insert 메서드가 없습니다. 전면에 삽입 할 경우, 당신은 당신이 큰 벡터에 종종이 작업을 수행 할려고하는 경우에, 당신은 대신 std::deque을 사용하고 사용하는 것을 고려하고 싶을 수도있다, 그런데

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

을 시도 할 수 복잡한 시간 복잡성을 가진 방법은 push_front입니다.

+0

거기에,이 참조하십시오 : http://www.cplusplus.com/reference/stl/vector/insert/ –

+0

btw, 나는 당신의 방법을 시도, 너무 작동합니다. 감사. –

+0

@AnishaKaul nope. 세 개의 매개 변수 변형에 대한 두 번째 매개 변수로'site_type' 또는'InputIterator'가 있습니다. – juanchopanza

관련 문제