C++程序将列表转换为集合

c++程序将列表转换为集合

C++中的列表与向量一样是容器,但列表的实现是基于双重的链表与向量的数组实现相比。列表一般不包含列表中的元素分布在连续的位置记忆。列表在其中的任何地方都提供相同的恒定时间操作,这是主要的使用列表的特点。另一方面,集合是包含唯一值的容器某种类型并且所有元素都按升序排序。这两个容器是不同,但是有多种方法可以将列表转换为集合。我们讨论该方法详情如下。

朴素方法

最简单、最幼稚的方法是定义两个不同的容器;列表类型之一另一个是set类型,将列表的每个元素复制到集合中。

语法

list myList;set mySet;for ( int const &val: myList ) {   mySet.insert(val);}

算法

在列表中获取输入。迭代列表中的每个元素并将它们插入到集合中。显示集合的内容。

示例

#include #include #include using namespace std;int main(){      //initializing the list   list myList = { 10, 30, 65, 98, 76, 44, 32, 73, 81, 29 };   set mySet;   cout<< "The list contents are:" << endl;      //displaying the list contents   for ( int const &val: myList ) {      cout << val << ' ';   }      //copying the elements of the list   for ( int const &val: myList ) {      mySet.insert(val);   }   cout << "nThe set contents are:" << endl;   for ( int const &val: mySet ) {      cout << val << ' ';   }   return 0;}

输出

The list contents are:10 30 65 98 76 44 32 73 81 29 The set contents are:10 29 30 32 44 65 73 76 81 98 

使用范围构造函数

列表的开始和结束指针必须作为构造函数的参数提供构建集合时使用范围构造函数。

语法

list myList;set mySet(begin(myList), end(myList));

算法

在列表中获取输入。

立即学习“C++免费学习笔记(深入)”;

创建集合时,将列表的开始和结束指针传递给集合的范围构造函数。

显示集合的内容。

示例

#include #include #include using namespace std;int main(){      //initializing the list   list myList = { 30, 70, 56, 89, 67, 44, 23, 37, 18, 92 };      //using the range constructor   set mySet(begin(myList), end(myList));   cout<< "The list contents are:" << endl;      //displaying the list contents   for ( int const &val: myList ) {      cout << val << ' ';   }   cout << "nThe set contents are:" << endl;   for ( int const &val: mySet ) {      cout << val << ' ';   }   return 0;}

输出

The list contents are:30 70 56 89 67 44 23 37 18 92 The set contents are:18 23 30 37 44 56 67 70 89 92 

使用复制功能

C++ 中的复制函数允许将数据从一个容器复制到另一个容器。要使用复制函数,列表的开始和结束指针必须作为参数传递到函数以及指向集合的指针和集合内的集合的开头插入器功能。

语法

list myList;set mySet;copy(begin(myList), end(myList), inserter(mySet, begin(mySet)));

算法

在列表中获取输入。

立即学习“C++免费学习笔记(深入)”;

定义一个新集合。

将列表的开始和结束指针以及插入器函数中的集合和集合开头的指针作为参数传递给复制函数。

显示集合的内容。

示例

#include #include #include using namespace std;int main(){      //initializing the list   list myList = { 33, 74, 52, 84, 65, 47, 28, 39, 13, 96 };   set mySet;      //using the copy function   copy(begin(myList), end(myList), inserter(mySet, begin(mySet)));   cout<< "The list contents are:" << endl;      //displaying the list contents   for ( int const &val: myList ) {      cout << val << ' ';   }   cout << "nThe set contents are:" << endl;   for ( int const &val: mySet ) {      cout << val << ' ';   }   return 0;}

输出

The list contents are:33 74 52 84 65 47 28 39 13 96 The set contents are:13 28 33 39 47 52 65 74 84 96 

结论

当我们使用集合时,我们不能向集合中添加或存储重复的元素,但是允许重复的元素存储在列表或类似数组的数据结构中。有在某些情况下,首选使用集合而不是列表。这些转换我们之前见过的技术对此确实很有帮助。

以上就是C++程序将列表转换为集合的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444631.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 21:44:37
下一篇 2025年12月17日 21:44:48

发表回复

登录后才能评论
关注微信