首页 > 编程语言 > 详细

C++ copy和copy_backward用法实例

时间:2015-04-30 09:03:03      阅读:278      评论:0      收藏:0      [点我收藏+]
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    // 数组大小为7
    int myInts[] = {10,20,30,40,50,60,70};
    // 容器大小为8,里面有8个7
    vector<int> myVector(8,7);

    // 1.将数组中的7个数复制到容器的第一个位置,将覆盖容器的前7个数
    copy(myInts,myInts + 7,myVector.begin());
    for(vector<int>::iterator it = myVector.begin();it != myVector.end();it++)
        cout<<" "<<*it;
    cout<<endl;
    /*
     * 10 20 30 40 50 60 70 7
     */

    // 2.复制容器的后3个数,复制到第二个位置
    /*
    copy(myVector.end()-3,myVector.end(),myVector.begin()+1);
    for(vector<int>::iterator it = myVector.begin();it != myVector.end();it++)
        cout<<" "<<*it;
    cout<<endl;
    *
    * 10 60 70 7 50 60 70 7
    */
    // 3.采用copy_backward函数,性质和copy不同,但殊途同归 
    copy_backward(myVector.end()-3,myVector.end(),myVector.begin()+4);
    for(vector<int>::iterator it = myVector.begin();it != myVector.end();it++)
        cout<<" "<<*it;
    cout<<endl;
    /*
    * 10 60 70 7 50 60 70 7
    */

    return 0;
}

C++ copy和copy_backward用法实例

原文:http://blog.csdn.net/u011487593/article/details/45371865

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!