调用库的问题

发布于 2024-11-04 10:06:30 字数 761 浏览 1 评论 0原文

你好 我正在观看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

め可乐爱微笑 2024-11-11 10:06:30

很明显,您的教程不使用标准模板库,而是使用它自己的自定义库。 #include "set.h" 在用户搜索路径(通常是当前目录)中查找 set.h 文件。由于您没有该文件,编译器会发出错误。

当您#include时,您将获得STL set 类。您的教程的 "set.h" 文件可能会给您另一个类(

"map.h" 也是如此) )。

无论如何,如果您正在学习 C++ 教程,您不妨尝试找到一个有关 STL 的教程,因为它比大多数其他 C++ 库得到了更广泛的支持和采用。您可以关注此处

您的代码片段的相应代码可能如下所示:

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

您使用的是 ss.begin(),而不是 ss.iterator(),它返回一个位于“开始”你的集合。您必须将 itrss.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 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.

余罪 2024-11-11 10:06:30

最有可能的是一个错字:

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

Most probably it's a typo:

set<int>::iterator itr = ss.iterator();
草莓味的萝莉 2024-11-11 10:06:30

您似乎正在使用 Java 教程来学习如何使用 C++ 迭代器。这样的做法是正确的:

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

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

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++;
}
吃素的狼 2024-11-11 10:06:30
#include "set.h"

这会在包含它的文件所在的同一目录中查找名为 set.h 的文件。它告诉你那不存在。您需要创建它/将其移动到那里。

#include <set> 

这是搜索您的包含路径并找到 STL set ,它当然没有您在自己的 set.h 中为类定义的方法

#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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文