• C++ move()实例讲解


    C++ 算法 move()function 用于移动元素。它接受三个参数,然后将属于范围 [first,last) 的元素移动到以 'result' 开头的范围中。

    用法

    template<class InputIterator, class OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result);

    参数

    first:它是范围​​的第一个元素的输入迭代器,其中元素本身包含在范围内。

    last: 它是范围最后一个元素的输入迭代器,其中元素本身不包含在范围内。

    result:它是移动元素初始位置的输出迭代器。

    返回值

    该函数将第一个元素的迭代器返回到移动的元素序列。

    例子1

    1. #include <iostream>
    2. #include <algorithm>
    3. #include <utility>
    4. #include <vector>
    5. #include <string>
    6. int main ()
    7. {
    8. std::vector<std::string> a = {"suraj","aman","vanshika","chhavi"};
    9. std::vector<std::string> b (4);
    10. std::cout << "Move function.\n";
    11. std::move ( a.begin(), a.begin()+4, b.begin() );
    12. std::cout << "a contains " << a.size() << " elements:";
    13. std::cout << " (The state of which is valid.)";
    14. std::cout << '\n';
    15. std::cout << "b contains " << b.size() << " elements:";
    16. for (std::string& x:b) std::cout << " [" << x << "]";
    17. std::cout << '\n';
    18. std::cout << "Moving the conatiner a...\n";
    19. a = std::move (b);
    20. std::cout << "a contains " << a.size() << " elements:";
    21. for (std::string& x:a) std::cout << " [" << x << "]";
    22. std::cout << '\n';
    23. std::cout << "b is in valid state";
    24. std::cout << '\n';
    25. return 0;
    26. }

    输出:

    Move function.
    a contains 4 elements:(The state of which is valid.)
    b contains 4 elements:[suraj] [aman] [vanshika] [chhavi]
    Moving the conatiner a...
    a contains 4 elements:[suraj] [aman] [vanshika] [chhavi]
    b is in valid state
    

    例子2

    1. #include<bits/stdc++.h>
    2. int main()
    3. {
    4. std::vector <int> u1 {9, 14, 21, 18};
    5. std::vector <int> u2 {14, 14, 14, 14};
    6. std::cout << "u1 contains:";
    7. for(int j = 0; j < u1.size(); j++)
    8. std::cout << " " << u1[j];
    9. std::cout << "\n";
    10. std::cout << "u2 contains:";
    11. for(unsigned int j = 0; j < u2.size(); j++)
    12. std::cout << " " << u2[j];
    13. std::cout << "\n\n";
    14. std::move (u1.begin(), u1.begin() + 4, u2.begin() + 1);
    15. std::cout << "u2 contains after move function:";
    16. for(unsigned int j = 0; j < u2.size(); j++)
    17. std::cout << " " << u2[j];
    18. std::cout << "\n";
    19. return 0;
    20. }

    输出:

    u1 contains:9 14 21 18
    u2 contains:14 14 14 14
    
    u2 contains after move function:14 9 14 21
    

    复杂度

    函数的复杂度从第一个元素到最后一个元素是线性的。

    数据竞争

    访问部分或全部容器对象。

    异常

    如果任何容器元素抛出一个异常,该函数就会抛出异常。



    好了, 以上是本文所有内容,希望对大家有所帮助,也希望大家对码农之家多多支持,你们的支持是我创作的动力!祝大家生活愉快!

  • 相关阅读:
    注册阿里云账号,免费领取云服务器!
    Servlet---ServletConfig类使用介绍
    银联扫码支付及静态码回调验签
    ZooKeeper 8:请求处理逻辑与源码分析
    【无标题】
    PDF怎么转图片格式?分享三个不错的方法
    Java高级特性之多线程,java实战项目视频
    2023年中国制服需求量、市场规模及行业细分需求现状分析[图]
    秒杀项目——多级缓存
    02【MyBatis框架的CRUD】
  • 原文地址:https://blog.csdn.net/wuxiaopengnihao1/article/details/127567061