卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章18200本站已运行340

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

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

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

朴素方法

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

语法

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

算法

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

示例

'
#include <iostream>
#include <set>
#include <list>
using namespace std;
int main(){
   
   //initializing the list
   list<int> myList = { 10, 30, 65, 98, 76, 44, 32, 73, 81, 29 };
   set<int> 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<int> myList;
set<int> mySet(begin(myList), end(myList));

算法

  • 在列表中获取输入。

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

  • 显示集合的内容。

示例

'
#include <iostream>
#include <set>
#include <list>
using namespace std;
int main(){
   
   //initializing the list
   list<int> myList = { 30, 70, 56, 89, 67, 44, 23, 37, 18, 92 };
   
   //using the range constructor
   set<int> 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<int> myList;
set<int> mySet;
copy(begin(myList), end(myList), inserter(mySet, begin(mySet)));

算法

  • 在列表中获取输入。

  • 定义一个新集合。

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

  • 显示集合的内容。

示例

'
#include <iostream>
#include <set>
#include <list>
using namespace std;
int main(){
   
   //initializing the list
   list<int> myList = { 33, 74, 52, 84, 65, 47, 28, 39, 13, 96 };
   set<int> 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 

结论

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

卓越飞翔博客
上一篇: 如何使用PHP数据库连接处理大数据量的查询
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏