zoukankan      html  css  js  c++  java
  • vector-swap

    ////////////////////////////////////////
    //      2018/04/22 7:37:29
    //      vector-swap
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    template<class T>
    class Print
    {
    public:
        void operator()(T& t){
            cout << t << " ";
        }
    };
    
    //==================
    int main(){
        int ary[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        Print<int> print;
    
        vector<int> v1(ary, ary + 7);
        vector<int> v2(ary + 7, ary + 10);
    
        cout << "Vector v1:";
        for_each(v1.begin(),v1.end(), print);
        cout << endl;
        cout << "Size of v1 = " << v1.size() << endl << endl;
    
        cout << "Vector v2:";
        for_each(v2.begin(), v2.end(), print);
        cout << endl;
        cout << "Size of v2 = " << v2.size() << endl << endl;
    
        v1.swap(v2);
    
        cout << "After swaping:" << endl;
        cout << "Vector v1:";
        for_each(v1.begin(),v1.end(),print);
        cout << endl;
        cout << "Size of v1 = " << v1.size() << endl << endl;
    
        cout << "Vector v2:";
        for_each(v2.begin(), v2.end(),print);
        cout << endl;
        cout << "Size of v2 = " << v2.size() << endl;
    
        return 0;
    }
    
    /*
    OUTPUT:
        Vector v1:1 2 3 4 5 6 7
        Size of v1 = 7
    
        Vector v2:8 9 10
        Size of v2 = 3
    
        After swaping:
        Vector v1:8 9 10
        Size of v1 = 3
    
        Vector v2:1 2 3 4 5 6 7
        Size of v2 = 7
    */ 
  • 相关阅读:
    动态可配置表单的设计构思
    mysql之视图
    mysql学习之数据备份和恢复
    mysqli操作
    mysql登录出现1045错误修改方法
    mysql之简单的多表查询
    sql优化的方法总结
    mysql语句操作
    linux 批量替换文件内容
    zend framework 数据库操作(DB操作)总结
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12538022.html
Copyright © 2011-2022 走看看