c++ 中的类似集合的数据结构是什么?

发布于 2024-10-04 00:58:05 字数 215 浏览 1 评论 0原文

我需要利用delphi集合的优点,例如c++中的“in”,但我不知道c++中是否有像集合这样的数据结构

我知道我可以使用数组代替,但正如我所说的我想使用像“in”这样的集合优势,那么c++中是否有像集合这样的内置数据结构?

如果是,请解释如何使用它,我仍然是 c++ 的初学者。

如果不是,有什么方法可以表示它(除了数组,因为我知道)。

提前致谢 :)

I need to use the advantages of delphi sets like "in" in c++, but I don't know if there is a data structure like sets in c++

I know that I may use an array instead, but as I have said I want to use sets advantages like "in", so is there any built in data structure like sets in c++?

If yes, please explain how to use it, I'm still a starter in c++

If no, is there any way to represent it (exept array since I know it).

thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

赢得她心 2024-10-11 00:58:05

有一个名为 std::set 的标准库容器...我不知道 delphi,但是可以使用 实现一个简单的 element in set 操作>find 方法并将结果与​​ end 进行比较:

std::set<int> s;
s.insert( 5 );
if ( s.find( 5 ) != s.end() ) {
   // 5 is in the set
}

其他操作可能会实现为标准库中的算法(std::unionstd: :差异...)

There is a standard library container called std::set... I don't know delphi, but a simple element in set operation would be implemented by using the find method and comparing the result with end:

std::set<int> s;
s.insert( 5 );
if ( s.find( 5 ) != s.end() ) {
   // 5 is in the set
}

Other operations might be implemented as algorithms in the standard library (std::union, std::difference... )

℉絮湮 2024-10-11 00:58:05

使用std::set。请参阅 http://www.cplusplus.com 以供参考。

Use std::set. See http://www.cplusplus.com for reference.

浪荡不羁 2024-10-11 00:58:05

In C++ there is nothing similarly integrated. Depending on your needs you might want to use bit flags and bitwise operations or the std::bitset standard container (besides std::set, of course). If you are using C++Builder there is also a class that simulates Delphi sets - search System.hpp for something like BaseSet or SetBase or similar - I don't recall the exact name.

冬天的雪花 2024-10-11 00:58:05

是的,第 12 页描述了一个 C++ STL set 容器类。 Stroustrup 的 TC++PL(特别版)第 491 节。

Yes, there is a C++ STL set container class described on p. 491 of Stroustrup's TC++PL (Special Ed.).

咿呀咿呀哟 2024-10-11 00:58:05

STL算法有以下特点
来自 MSDN

set_difference
将属于一个已排序源范围但不属于第二已排序源范围的所有元素合并为单个已排序目标范围,其中排序标准可由二元谓词指定。

设置交集
将属于两个排序源范围的所有元素联合到单个排序目标范围中,其中排序标准可以由二元谓词指定。

设置对称差异
将属于一个(但不是两个)已排序源范围的所有元素合并到单个已排序目标范围中,其中排序标准可以由二元谓词指定。

set_union
将属于两个排序的源范围中的至少一个的所有元素联合到单个排序的目标范围中,其中排序标准可以由二元谓词指定。

STL algorithm has the following
From MSDN

set_difference
Unites all of the elements that belong to one sorted source range, but not to a second sorted source range, into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate.

set_intersection
Unites all of the elements that belong to both sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate.

set_symmetric_difference
Unites all of the elements that belong to one, but not both, of the sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate.

set_union
Unites all of the elements that belong to at least one of two sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate.

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