首页 > 后端开发 > C++ > 正文

C++程序将向量转换为列表

WBOY
发布: 2023-09-10 08:49:03
转载
1300 人浏览过

C++程序将向量转换为列表

C++ 中的向量是动态数组,可以包含任何类型的数据,可以是用户定义的或原始的。动态是指向量的大小可以根据操作增加或减少。向量支持各种函数,数据操作非常容易。另一方面,列表是与向量相同的容器,但与向量的数组实现相比,列表实现是基于双向链表的。列表在其中的任何位置都提供相同的恒定时间操作,这是使用列表的主要功能。我们来看看将向量转换为列表的主要方法。

使用范围构造函数

要使用范围构造函数,在创建列表时必须将向量的起始指针和结束指针作为参数传递给构造函数。

语法

vector <int> ip;
list <int> op( ip.begin(), ip.end() );
登录后复制

算法

  • 将输入存储在向量中。
  • 将向量的起始指针和结束指针传递给列表的范围构造函数。
  • 显示列表的内容。

示例

#include <iostream>
#include <vector>
#include <list>

using namespace std;
list <int> solve( vector <int> ip) {
   //initialise the list
   list <int> op( ip.begin(), ip.end() );
   return op;
}

int main() {
   vector <int> ip( { 15, 20, 65, 30, 24, 33, 12, 29, 36, 58, 96, 88, 30, 71 } );
   list <int> op = solve( ip );

   //display the input
   cout<< "The input vector is: ";
   for( int i : ip ) {
      cout<< i << " ";
   }

   //display the output
   cout << "\nThe output list is: ";
   for( int j : op ) {
      cout << j << " ";
   }
   return 0;
}
登录后复制

输出

The input vector is: 15 20 65 30 24 33 12 29 36 58 96 88 30 71 
The output list is: 15 20 65 30 24 33 12 29 36 58 96 88 30 71
登录后复制

使用std::list的assign函数

std::list的用法与范围构造函数的用法类似。我们以与范围构造函数相同的方式传递向量的起始和结束指针。

语法

vector <int> ip;
list <int> op();
op.assign(ip.begin(), ip.end());
登录后复制

算法

  • 将输入存储在向量中。
  • 定义一个新的列表。
  • 将向量的起始指针和结束指针传递给列表的assign函数
  • 显示列表的内容。

示例

#include <iostream>
#include <vector>
#include <list>

using namespace std;
list <int> solve( vector <int> ip) {

   //initialise the list
   list <int> op;
   op.assign( ip.begin(), ip.end() );
   return op;
}

int main() {
   vector <int> ip( { 40, 77, 8, 65, 92 ,13, 72, 30, 67, 12, 88, 37, 18, 23, 41} );
   list <int> op = solve( ip );

   //display the input
   cout<< "The input vector is: ";
   for( int i : ip ) {
      cout<< i << " ";
   }

   //display the output
   cout << "\nThe output list is: ";
   for( int j : op ) {
      cout << j << " ";
   }
   return 0;
}
登录后复制

输出

The input vector is: 40 77 8 65 92 13 72 30 67 12 88 37 18 23 41 
The output list is: 40 77 8 65 92 13 72 30 67 12 88 37 18 23 41 
登录后复制

使用列表插入函数

我们可以使用列表的插入函数将数据从向量插入到列表中。列表需要列表开头的指针以及向量开头和结尾的指针。

语法

vector <int> ip;
list <int> op();
op.insert(op.begin(), ip.begin(), ip.end());
登录后复制

算法

  • 将输入存储在向量中。
  • 定义一个新的列表。
  • 传递列表的起始指针以及起始和结束指针
  • 列表插入函数的向量。
  • 显示列表的内容。

示例

#include <iostream>
#include <vector>
#include <list>

using namespace std;
list <int> solve( vector <int> ip) {

   //initialise the list
   list <int> op;
   op.insert( op.begin(), ip.begin(), ip.end() );
   return op;
}

int main() {
   vector <int> ip( { 30, 82, 7, 13, 69, 53, 70, 19, 73, 46, 26, 11, 37, 83} );
   list <int> op = solve( ip );

   //display the input
   cout<< "The input vector is: ";

   for( int i : ip ) {
      cout<< i << " ";
   }

   //display the output
   cout << "\nThe output list is: ";
   for( int j : op ) {
      cout << j << " ";
   }
   return 0;
}
登录后复制

输出

The input vector is: 30 82 7 13 69 53 70 19 73 46 26 11 37 83 
The output list is: 30 82 7 13 69 53 70 19 73 46 26 11 37 83 
登录后复制

结论

在C++中,将向量转换为列表具有在列表的任何位置上统一的操作复杂度的好处。有几种方法可以将向量转换为列表。然而,我们只在这里提到了最简单和最快的方法。

以上是C++程序将向量转换为列表的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!