paul10
Mitglied
Hallo,
also mal zu meiner Frage:
Ich habe das Buch C++ von A bis Z und lese mich gerade bei Templates und STL ein, da kommt dann bei STL ein Source Code vor, der mir nicht ganz geläufig ist... also seht mal :
ich würde gerne wissen ob er sich da nicht verdann hat und statt :
nicht das gehören würde :
da der index des array ja mit 0 und nicht mit 1 beginnt...
Danke im Vorraus
Gruß
also mal zu meiner Frage:
Ich habe das Buch C++ von A bis Z und lese mich gerade bei Templates und STL ein, da kommt dann bei STL ein Source Code vor, der mir nicht ganz geläufig ist... also seht mal :
Code:
// stl1.cpp
#include<iostream>
using namespace std;
typedef int* myIterator;
myIterator find( myIterator begin, myIterator end,
const int& ival );
void init( int* Container, int count );
int main() {
const int cnt = 50;
int Container[cnt];
myIterator begin = Container;
myIterator end = Container + cnt ; // <---- AUF DAS BEZIEHT SICH MEINE FRAGE
init( &Container[0], cnt );
int num = -1;
while( num != 0 ) {
cout << "Nummer suchen (0=Ende) : ";
if( !(cin >> num) ) {
break; // Fehlerhafte Eingabe
}
if( num != 0 ) {
myIterator pos = find( begin, end, num );
if( pos != end ) {
cout << num << " an Pos. "
<< (pos-begin) << "\n";
}
else {
cout << num << " ist nicht vorhanden!!\n";
}
}
}
return 0;
}
myIterator find( myIterator begin, myIterator end,
const int& ival) {
while(begin != end && *begin != ival) {
++begin; // nächste Position
}
return begin;
}
void init( int* Container, int count ) {
for(int i = 0; i < count; i++) {
Container[i] = i*i;
}
}
ich würde gerne wissen ob er sich da nicht verdann hat und statt :
Code:
myIterator end = Container + cnt
nicht das gehören würde :
Code:
myIterator end = Container + ( cnt - 1 );
da der index des array ja mit 0 und nicht mit 1 beginnt...
Danke im Vorraus
Gruß