调用库的问题
你好 我正在观看 C++ 教程 讲师包含这样的库
# include "map.h"
#include "set.h"
,但是当我使用此代码时,我收到错误
致命错误 C1083:无法打开包含 文件:'set.h':没有这样的文件或 目录
所以我必须这样写
# include <map>
#include <set>
但现在我有另一个问题 当我创建集合或地图ob时,我可以使用的方法与教程中显示的方法不同 例如,在教程中,讲师创建并导航这样的集合或地图,
set<int> ss;
set<int>::itrator itr = ss.itrator();
while(itr.hasnext()){
cout<<itr.next();
}
但我的 ss 和对象没有此方法
ss.itrator();
itr.hasnext()
itr.next();
,我必须像这样编写此代码,这
set<int> ss;
set<int>::itrator itr = ss.begin();
while(!itr.end()){
cout<<*itr;
}
是什么问题?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很明显,您的教程不使用标准模板库,而是使用它自己的自定义库。
#include "set.h"
在用户搜索路径(通常是当前目录)中查找set.h
文件。由于您没有该文件,编译器会发出错误。当您
#include
时,您将获得STLset
类。您的教程的"set.h"
文件可能会给您另一个类(和
"map.h"
也是如此) )。无论如何,如果您正在学习 C++ 教程,您不妨尝试找到一个有关 STL 的教程,因为它比大多数其他 C++ 库得到了更广泛的支持和采用。您可以关注此处。
您的代码片段的相应代码可能如下所示:
您使用的是
ss.begin()
,而不是ss.iterator()
,它返回一个位于“开始”你的集合。您必须将itr
与ss.end()
进行比较,而不是itr.hasnext()
;ss.end()
返回一个位于集合“末尾”的迭代器,因此您知道迭代尚未完成,而您的迭代器与位于集合末尾的迭代器不同。收藏。itr++
和*itr
代替itr.next()
。在您的上下文中,itr.next() 既返回当前元素,又使迭代器前进 1。这是糟糕的设计,因为该对象不提供仅访问当前元素的方法,如果您只想多次访问当前元素,则会导致重复代码。 STL 迭代器具有用于前进和获取引用元素的不同操作:
itr++
前进迭代器,*itr
获取当前元素。Pretty obviously, your tutorial doesn't use the Standard Template Library, and uses a custom library of its own.
#include "set.h"
looks for aset.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 STLset
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:
Instead of
ss.iterator()
, you havess.begin()
, which returns an iterator positioned to the "beginning" of your set. Instead ofitr.hasnext()
, you must compareitr
toss.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 ofitr.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.最有可能的是一个错字:
Most probably it's a typo:
您似乎正在使用 Java 教程来学习如何使用 C++ 迭代器。这样的做法是正确的:
It looks like you're using a Java tutorial to learn how to use C++ iterators. The approach like this is correct:
这会在包含它的文件所在的同一目录中查找名为
set.h
的文件。它告诉你那不存在。您需要创建它/将其移动到那里。这是搜索您的包含路径并找到 STL
set
,它当然没有您在自己的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.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 ownset.h