c++ - problem with calling libraries -
hi i'm watching c++ tutorial , instructor includes libraries
# include "map.h" #include "set.h"
but when use code error
fatal error c1083: cannot open include file: 'set.h': no such file or directory
so have write
# include <map> #include <set>
but have problem when create set or map ob , methods can use different shown in tutorial example in tutorial instructor create , navigate set or map
set<int> ss; set<int>::itrator itr = ss.itrator(); while(itr.hasnext()){ cout<<itr.next(); }
but ss , object doesn't have methods
ss.itrator(); itr.hasnext() itr.next();
an have write code
set<int> ss; set<int>::itrator itr = ss.begin(); while(!itr.end()){ cout<<*itr; }
what problem ?
pretty obviously, tutorial doesn't use standard template library, , uses custom library of own. #include "set.h"
looks set.h
file in user search path, current directory. since don't have file, compiler emits error.
when #include <set>
, stl set
class. tutorial's "set.h"
file probably give class (same goes <map>
, "map.h"
).
anyhow, if you're following c++ tutorial, might try find 1 stl, since it's got wider support , adoption other c++ libraries. can follow 1 here.
the corresponding code snippet following:
set<int> ss; (set<int>::iterator itr = ss.begin(); itr != ss.end(); itr++) { cout << *itr; }
instead of ss.iterator()
, have ss.begin()
, returns iterator positioned "beginning" of set. instead of itr.hasnext()
, must compare itr
ss.end()
; ss.end()
returns iterator positioned "end" of set, know you're not done iterating while iterator not same iterator positioned end of collection. itr++
, *itr
take place of itr.next()
.
in context, itr.next()
both returns current element , advances iterator one. bad design because object provides no means access current element, leading duplicate code if want access current element multiple times. stl iterators have distinct operations advancing , obtaining referenced element: itr++
advances iterator, , *itr
obtains current element.
Comments
Post a Comment