调用库的问题 [英] problem with calling libraries

查看:189
本文介绍了调用库的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我在看一个c ++教程,
教师包括像这样的库

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

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

使用此代码我得到错误


致命错误C1083:无法打开包含
文件:'set.h':没有这样的文件或
目录

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

所以我必须这样写

# include  <map>
#include <set>

但是现在我有另一个问题
当我创建一个集合或映射ob,我可以使用不同于教程中显示的
例如在教程指导中创建和导航一个集合或地图像这样

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();
}

但我的ss和对象没有这个方法

but my ss and object doesn't have this methods

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

我必须这样写这个代码

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

cout<<*itr;
}

有什么问题?

推荐答案

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

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.

当您 #include< set> ,你得到STL set 类。你的教程的set.h文件可能会给你另一个类(同样适用于< map> map.h)。

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").

无论如何,如果你遵循C ++教程,尝试找到一个关于STL,因为它得到更广泛的支持和采用大多数其他C ++库。您可以按此处

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;
}

而不是 ss.iterator(),你有 ss.begin(),它返回一个迭代器定位到你的集合的开始。而不是 itr.hasnext(),你必须比较 itr ss.end ; ss.end()返回一个定位到集合end的迭代器,所以你知道你没有完成迭代,而你的迭代器和迭代器不一样定位到集合的结尾。 itr ++ * itr 发生 itr.next()

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().

在上下文中, itr.next()都返回当前元素, 。这是坏的设计,因为对象不提供只访问当前元素的方法,导致重复的代码,如果你只是想访问当前元素多次。 STL迭代器具有用于推进和获得被引用元素的不同操作: itr ++ 推进迭代器,并且 * itr 当前元素。

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.

这篇关于调用库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆