Re: Why is the iterator pass-by-value in the STL?
On Nov 16, 10:59 am, Ruki <war...@gmail.com> wrote:
Why is the iterator pass-by-value in the STL?
I think it is more efficient and safe by passing a const
reference,isn't it?
void func(const_iterator first, const_iterator last);
void func(const_iterator const& first, const_iterator const& last);
Maybe because the operator++ cannot be used in the 2nd form of func
above. Here is a small example to demonstrate the problem:
$ cat value.cc
#include <iostream>
#include <vector>
typedef std::vector<int>::const_iterator I;
void good(I from, I to) {
while ( from++ != to ) {
std::cout << *from << std::endl;
}
}
void isThisBetter(I const& from, I const &to) {
// Cannot do from++, so create temporary explicitly.
I cur(from);
while ( cur++ != to ) {
std::cout << *cur << std::endl;
}
}
int main() {
std::vector<int> dlg;
dlg.push_back(1);
dlg.push_back(2);
dlg.push_back(7);
dlg.push_back(8);
good(dlg.begin(), dlg.end());
isThisBetter(dlg.begin(), dlg.end());
}
$ g++ -Wall -Wall value.cc
$ ./a
2
7
8
0
2
7
8
0
$
Rgds,
anna
--
Civil Disobedience
http://missingrainbow.blogspot.com/2008/07/civil-disobedience.html
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]