problem with calling libraries

2019-07-19 07:26发布

问题:

hi i'm watching a c++ tutorial and instructor includes the libraries like this

# include  "map.h"
#include "set.h"

but when i use this code i get error

fatal error C1083: Cannot open include file: 'set.h': No such file or directory

so i have to write it like this

# include  <map>
#include <set>

but now i have another problem when i create an set or map ob , the methods that i can use are different from what is shown in the tutorial for example in the tutorial instructor create and navigate a set or map like this

set<int> ss;
set<int>::itrator itr = ss.itrator();
while(itr.hasnext()){

cout<<itr.next();
}

but my ss and object doesn't have this methods

ss.itrator();
itr.hasnext()
itr.next();

an i have to write this code like this

set<int> ss;
set<int>::itrator itr = ss.begin();
while(!itr.end()){

cout<<*itr;
}

what is the problem ?

回答1:

Pretty obviously, your tutorial doesn't use the Standard Template Library, and uses a custom library of its own. #include "set.h" looks for a set.h file in the user search path, which is usually the current directory. Since you don't have the file, the compiler emits an error.

When you #include <set>, you get the STL set class. Your tutorial's "set.h" file probably would probably give you another class (same goes for <map> and "map.h").

Anyhow, if you're following a C++ tutorial, you might as well try to find one about the STL, since it's got wider support and adoption that most other C++ libraries. You can follow one here.

The corresponding code for your snippet would probably be the following:

set<int> ss;
for (set<int>::iterator itr = ss.begin(); itr != ss.end(); itr++)
{
    cout << *itr;
}

Instead of ss.iterator(), you have ss.begin(), which returns an iterator positioned to the "beginning" of your set. Instead of itr.hasnext(), you must compare itr to ss.end(); ss.end() returns an iterator positioned to the "end" of the set, so you know you're not done iterating while your iterator is not the same as an iterator positioned to the end of the collection. itr++ and *itr take place of itr.next().

In your context, itr.next() both returns the current element and advances the iterator by one. This is bad design because the object provides no means to just access the current element, leading to duplicate code if you just want to access the current element multiple times. The STL iterators have distinct operations for advancing and obtaining the referenced element: itr++ advances the iterator, and *itr obtains the current element.



回答2:

Most probably it's a typo:

set<int>::iterator itr = ss.iterator();


回答3:

It looks like you're using a Java tutorial to learn how to use C++ iterators. The approach like this is correct:

set<int> ss;
set<int>::iterator iter = ss.begin();

while (iter != ss.end()){
  cout << *iter <<endl;
  iter++;
}


回答4:

#include "set.h"

This looks for a file named set.h in the same directory as the file including it. It's telling you that doesn't exist. You need to create it / move it there.

#include <set> 

That's searching your include path and finding the STL set which of course does not have the methods you've defined for your class in your own set.h



标签: c++ libraries