Problem mit iteratoren
-
Ich versuche gerade einen Index in einem vector zu finden. Bekomme aber nur ellenlange Fehlermeldungen
vector<double>::iterator it; // find first element in xaxis with xaxis[i] < x (lower than x) it = lower_bound (xaxis.begin()+ int(xIndexStart), xaxis.end(), x); size_t l = std::distance(xaxis.begin(), it); // index left from x size_t r = std::distance(xaxis.begin(), it+1); // index right from xdas liefert mir den Fehler
include\math/Interpolate.h: In function 'double Interpolate(double, const std::vector<double, std::allocator<double> >&, const std::vector<double, std::allocator<double> >&, int&)':
include\math/Interpolate.h:29: warning: comparison between signed and unsigned integer expressions
include\math/Interpolate.h:35: error: no match for 'operator=' in 'it = std::lower_bound [with _FIter = __gnu_cxx::__normal_iterator<const double*, std::vector<double, std::allocator<double> > >, _Tp = double](((const std::vector<double, std::allocator<double> >)xaxis)->std::vector<_Tp, _Alloc>::begin [with _Tp = double, _Alloc = std::allocator<double>]().__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+ [with _Iterator = const double, _Container = std::vector<double, std::allocator<double> >](((const ptrdiff_t&)((const ptrdiff_t*)(&((int)xIndexStart))))), ((const std::vector<double, std::allocator<double> >)xaxis)->std::vector<_Tp, _Alloc>::end [with _Tp = double, _Alloc = std::allocator<double>](), ((const double&)((const double)(& x))))'
c:\qt\sdk\2010.01\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_iterator.h:669: note: candidates are: __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >& __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >::operator=(const __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >&)
include\math/Interpolate.h:38: error: no matching function for call to 'distance(__gnu_cxx::__normal_iterator<const double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >&)'
include\math/Interpolate.h:39: error: no matching function for call to 'distance(__gnu_cxx::__normal_iterator<const double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >)'Sieht jemand mehr wie ich und kann mir sagen was ich falsch mache?
-
Zeile 25 nicht im Zitat?
Beispiel von lower/upper bound aus http://www.cplusplus.com/reference/algorithm/lower_bound/ auf double convertiert ... und das tut
-
hier der komplette Code:
#ifndef INTERPOLATE_H #define INTERPOLATE_H #include <cassert> #include <vector> using std::vector; #include <algorithm> double Interpolate(double x, const vector<double>& xaxis, const vector<double> & ydata, int & previousXIndex = -1) { size_t size = xaxis.size(); // check if x is in valid range, otherwise return limits if ( x >= xaxis.back() ) { previousXIndex = size; return ydata.back(); } if ( x <= xaxis.front() ) { previousXIndex = 0; return ydata.front(); } // setup xIndexStart, according to previousXIndex, // if previousXIndex can be used size_t xIndexStart = 0; if (previousXIndex > 0) { if ( previousXIndex > 0 && previousXIndex <= size ) { xIndexStart = previousXIndex; } } vector<double>::iterator it; // find first element in xaxis with xaxis[i] < x (lower than x) it = lower_bound (xaxis.begin()+ int(xIndexStart), xaxis.end(), x); size_t l = std::distance(xaxis.begin(), it); // index left from x size_t r = std::distance(xaxis.begin(), it+1); // index right from x previousXIndex = r; // start next search here if (x == xaxis[l]){ return ydata[l]; } // check ranges of l and r assert( !(r < 0 || r >= size || l < 0 || l >= size) ); return ydata[l] + (ydata[r]-ydata[l]) * (x-xaxis[l]) / (xaxis[r]-xaxis[l]); } #endif // INTERPOLATE_HZeile 25 enthält nur ein Komentar.
-
Reduziere Dein Problem auf ein vollständiges aber möglichst kurzes Programm.
Beispielsweise macht folgendes Programm bei mir keine Probleme:
#include <iostream> #include <ostream> #include <vector> #include <algorithm> using namespace std; int main() { static const int foo[] = {1,3,5,7,9}; vector<int> vec(foo,foo+5); vector<int>::iterator it = lower_bound(vec.begin(),vec.end(),6); if (it!=vec.end()) { cout << "*it --> " << *it << endl; cout << "distance --> " << distance(vec.begin(),it) << endl; } }
-
TUT:
using namespace std; int main () { double mydoub[] = {10.0,20.0,30.0,30.0,20.0,10.0,10.0,50.0,20.0}; vector<double> v(mydoub,mydoub+9); // 10.0,20.0,30.0,30.0,20.0,10.0,10.0,50.0,20.0 vector<double>::iterator low,up; double x = 45.0; low=lower_bound (v.begin() + int(2), v.end(), x); up= upper_bound (v.begin(), v.end(), 19.0); cout << "lower_bound at position " << int(low- v.begin()) << endl; cout << "upper_bound at position " << int(up - v.begin()) << endl; return 0; }TUT NICHT:
using namespace std; int main () { double mydoub[] = {10.0,20.0,30.0,30.0,20.0,10.0,10.0,50.0,20.0}; const vector<double> v(mydoub,mydoub+9); // 10.0,20.0,30.0,30.0,20.0,10.0,10.0,50.0,20.0 vector<double>::iterator low,up; double x = 45.0; low=lower_bound (v.begin() + int(2), v.end(), x); up= upper_bound (v.begin(), v.end(), 19.0); cout << "lower_bound at position " << int(low- v.begin()) << endl; cout << "upper_bound at position " << int(up - v.begin()) << endl; return 0; }Finde den Unterschied

-
Wenn das const das eigentliche Problem ist, macht es dann mehr Sinn const bei der Übergabe wegzunehmen, oder jedesmal ein const_cast zu machen?
-
Warum Schrauben mit dem Hammer einschlagen?
Du hast nenconst vector<double>dann nimm nenvector<double>::const_iteratorzum iterieren
Edit:
FachmannFürAlles schrieb:
padreigh schrieb:
Warum Schrauben mit dem Hammer einschlagen?
Vielleicht weil es Schlagschrauben sind? http://baustoffe.baucompany24.de/index.php/cat/c22_Schlagschrauben---Nagelduebel.html
lol ... man lernt nie aus.
-
padreigh schrieb:
Warum Schrauben mit dem Hammer einschlagen?
Vielleicht weil es Schlagschrauben sind? http://baustoffe.baucompany24.de/index.php/cat/c22_Schlagschrauben---Nagelduebel.html
-
Außerdem erwartet lower_bound, upper_bound sowie binary_search eine entsprechend sortierte Sequenz.
-
aus
// check if x is in valid range, otherwise return limits if ( x >= xaxis.back() ) { previousXIndex = size; return ydata.back();hab ich mal vermutet das der Vektor sortiert ist ... das würde sonst keinen Sinn machen.