stl swap函数

C ++ STL vector :: swap()函数 (C++ STL vector::swap() function)

vector::swap() function is used to swap/exchange the content of two vectors with the same type, their sizes may differ.

vector :: swap()函数用于交换/交换相同类型的两个向量的内容,它们的大小可能不同。

The contents of vector1 are exchanged with the content of vector2. Vectors must be of the same type i.e. either both of them are of type int, string, long, etc.

vector1的内容与vector2的内容交换。 向量必须具有相同的类型,即两个向量均为int,string,long等类型。

This is not necessary that both of the vectors must have the same size i.e. size of both the vectors may differ from each other.

两个矢量必须具有相同的大小是不必要的,即,两个矢量的大小可以彼此不同。

Note: Both the containers/vectors are modified after the call of this function.

注意:调用此函数后,两个容器/向量均被修改。

Syntax of vector::swap() function

vector :: swap()函数的语法

    vector1.swap(vector2);

Parameter(s): vector2 – another vector with that we have to swap the content of first vector vector1.

参数: vector2 –与另一个向量我们必须交换第一个向量vector1的内容。

Return value: void – it returns nothing.

返回值: void –不返回任何内容。

Time Complexity: O(1) i.e constant order

时间复杂度:O(1),即恒定阶数

Example:

例:

    Input:
vector<int> vector1{ 1, 2, 3, 4, 5 };
vector<int> vector2{ 6, 7, 8, 9, 10 };
Function call:
vector1.swap(vector2);
Output:
Vector1: 1 2 3 4 5
Vector2: 6 7 8 9 10
After swapping...
Vector1: 6 7 8 9 10
Vector2: 1 2 3 4 5

C ++程序演示vector :: swap()函数的示例 (C++ program to demonstrate example of vector::swap() function)

#include <iostream>
#include <vector>
using namespace std;
int main()
{vector<int> vector1{ 1, 2, 3, 4, 5 };
vector<int> vector2{ 6, 7, 8, 9, 10 };
// Before Swapping vector-1 and vector-2
cout << "Vector 1" << endl;
for (auto i : vector1)
cout << i << " ";
cout << "\n";
cout << "Vector 2" << endl;
for (auto i : vector2)
cout << i << " ";
cout << "\n";
// Swapping vector-1 and vector-2
vector1.swap(vector2);
// After swapping vector-1 and vector-2
cout << "Vector 1" << endl;
for (auto i : vector1)
cout << i << " ";
cout << "\n";
cout << "Vector 2" << endl;
for (auto i : vector2)
cout << i << " ";
return 0;
}

Output

输出量

Vector 1
1 2 3 4 5
Vector 2
6 7 8 9 10
Vector 1
6 7 8 9 10
Vector 2
1 2 3 4 5

翻译自: https://www.includehelp.com/stl/vector-swap-method-with-example.aspx

stl swap函数

stl swap函数_C ++ STL | vector :: swap()函数与示例相关推荐

  1. mysql 自定义哈希函数_C++ STL无序容器自定义哈希函数和比较规则(超级详细)...

    前面在讲解 unordered_map.unordered_multimap.unordered_set 以及 unordered_multiset 这 4 种无序关联式容器(哈希容器)时,遗留过一个 ...

  2. stl max函数_C ++ STL中带有示例的array :: max_size()函数

    stl max函数 C ++ STL array :: max_size()函数 (C++ STL array::max_size() function) max_size() function is ...

  3. c语言中swap函数_C ++中的swap()函数

    c语言中swap函数 介绍 (Introduction) In this tutorial, we are going to learn the swap() function in C++ prog ...

  4. stl向量最大值_C ++ STL中向量的最小和最大元素

    stl向量最大值 Given a vector and we have to find the smallest (minimum) and largest (maximum) elements. 给 ...

  5. c++ lambda函数_C++11 之 lambda函数的详细使用

    1. lambda 函数概述 lambda 表达式是一种匿名函数,即没有函数名的函数:该匿名函数是由数学中的λ演算而来的.通常情况下,lambda函数的语法定义为: [capture] (parame ...

  6. c调用python函数_C调用Python的函数

    1 /*2 module: Python脚步的模块名3 function: 要调用的函数名4 format: 传递给Py_VaBuildValue函数的可变参数模板5 */6 intPyCall(co ...

  7. string函数_C++[06] string成员函数之删除函数erase

    介绍一下删除函数erase,老方法,新建cpp文件,并找到string的erase成员函数,可以看到,erase有3个重载函数 右键转到定义,可以查看erase的源码 _Myt& erase( ...

  8. c++isdigit函数_c++isdigit函数_C语言中isdigit()函数和isxdigit()函数的用法

    C语言isdigit()函数:判断字符是否为阿拉伯数字头文件: #include 定义函数: int isdigit(int c); 函数说明:检查参数 c 是否为阿拉伯数字0 到9. 返回值:若参数 ...

  9. c++isdigit函数_C语言中isdigit()函数和isxdigit()函数的用法

    C语言isdigit()函数:判断字符是否为阿拉伯数字头文件: #include 定义函数: int isdigit(int c); 函数说明:检查参数 c 是否为阿拉伯数字0 到9. 返回值:若参数 ...

最新文章

  1. pytest测试实战 电子书_pytest实战APL测试框架
  2. js1:对象的学习,构造函数,继承构造函数【使用教材:JavaScript深度剖析第2版】...
  3. 华南理工大学网络教育计算机答案,计算机应用基础--随堂练习2019春华南理工大学网络教育答案...
  4. 目标描述(基于边界的描述)
  5. python3.7安装, 解决pip is configured with locations that require TLS/SSL问题
  6. 个人专著推荐2:Linux安全技术内幕
  7. Modern Radar for Automotive Applications(用于汽车应用的现代雷达)
  8. 看了B站上的这些Java视频,我飘了!
  9. 苹果10月23日或发布iPad mini
  10. TOP公链主网技术:多层扩容和多层分片
  11. 为什么计算机是32位64位,64位是x86还是x64_为什么64位是X64,32位是X86?
  12. Excel表格中的三维气泡图,你会做吗?
  13. cobbler部署与示例
  14. python查找联系人_python ---简易联系人
  15. 转发:【论文精读】Human Semantic Parsing for Person Re-identification
  16. 热门在线项目管理工具
  17. php sleep usleep,php sleep()函数, usleep()函数的用法
  18. linux全角半角切换,教您输入法全角和半角怎么切换
  19. iOS自定义相机实现拍照和连拍
  20. 海康监控主机无法登录后台解决办法

热门文章

  1. html计算器_学习HTML、CSS和JavaScript的最佳方法是什么?
  2. 角速度求积分能得到欧拉角吗_一个有趣的反常积分问题
  3. ps怎么清屏_黑洞PS大赛刷屏!最后一张扎心了……
  4. python plt.plot bar 设置绘图尺寸大小
  5. 代码实现——MapReduce实现Hadoop序列化
  6. 移动Web加速技术月报第2期
  7. 【数学】Floating-Point Hazard
  8. java sleep和wait区别
  9. CCF201509-2 日期计算(100分)
  10. 开源Registry项目Harbor源代码结构解析