2011-04-20 9 views
0

があります。私はここで私の質問を検索したが、何か関連するものを見つけることに失敗した。 これが問題です。
私はプログラムの中でこのコードを使っています。
私はそれをMSVS 2008で開発しましたが、うまくいきましたが、g ++でコンパイルしようとしたときに、以下の関数list::insertが原因で失敗しました。stl vector :: WindowsとLinuxの違いを挿入しますか?

//... 
pair<uint, double> NewElem(i, Prob.at(i)); 
bool inserted(false); 
vector<pair<uint, double> >::const_iterator li = NewList.begin(); 
for (vector<double>::const_iterator ji = BlocksMemory.begin() ; ji != BlocksMemory.end() ; ++ji) 
{ 
    if (NewElem.second <= *ji) 
     li += _SORT_BLOCK; 
    else 
     break; 
} 
for(;li != NewList.end() ; ++li) 
{ 
    if (NewElem.second > li->second) 
    { 
     NewList.insert(li, NewElem); 
     inserted = true; 
     break; 
    } 
} 

一方が見ることができるように、liNewListconst_iteratorあります。 NewElemのタイプはNewListと同じコンテンツタイプのpairです。

main.cpp:447: error: no matching function for call to " std::vector<std::pair<unsigned int, double>, std::allocator<std::pair<unsigned int, double> > >::insert(__gnu_cxx::__normal_iterator<const std::pair<unsigned int, double>*, std::vector<std::pair<unsigned int, double>, std::allocator<std::pair<unsigned int, double> > > >&, std::pair<unsigned int, double>&) "

/usr/include/c++/4.4/bits/vector.tcc:106: note: candidates are: __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = std::pair<unsigned int, double>, _Alloc = std::allocator<std::pair<unsigned int, double> >]

/usr/include/c++/4.4/bits/stl_vector.h:850: note: void std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp = std::pair<unsigned int, double>, _Alloc = std::allocator<std::pair<unsigned int, double> >]

何の理由になりますがあり

ます(読めない)応答を見ることができますか?そして、可能な解決策は何ですか?

iterator insert(iterator, const value_type&); 

しかし、あなたが関数に渡している最初の引数は、暗黙的に変換できないconst_iterator、次のとおりです。

答えて

2

使用しようとしているinsertメンバ関数のシグネチャは、おそらくです非const iterator。そのコードはVSで作業してはいけません。

簡単な解決策は、あなたがコンテナを変更しようとする場合は、非const iteratorを使用することである。また、

+0

はい、ありがとう...私は気になるでしょう、私はこのコードの部分を書いている間、私は非常に疲れていたように見えます:)それはちょっとしたオートメーションでした、私は通常 'const&'に引数を渡しています。単純な 'vector :: iterator'で反復することはできませんでした。私の場合は、ローカルスコープにvectorを定義していましたが、 'const_iterator'を使う必要はありませんでした。ありがとうございました。 –

1

std::vector< std::pair<uint,double> >::iterator li = NewList.begin();としてliを定義し、あなたがのstd ::ベクトルに挿入したいことを確信していますか?パフォーマンス上の理由から、std :: listはより良い選択です。また、リスト上の挿入は、既存のイテレータをベクトルに対しても無効にしません。

+0

+1教育的バックグラウンドの場合 – sehe

+0

大丈夫です。また、大量のデータがある場合にシーケンシャルメモリブロックを使用する必要はありません。コードを改訂します。 –

関連する問題