copy

dest 目标起始迭代器

  • copy(iterator beg, iterator end, iterator dest);
title:Important
容器浅拷贝常用算法
#include <iostream>
#include <vector>
using namespace std;
 
void print(vector<int>& v)
{
    for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
 
int main()
{
    vector<int>v1;
    
    for(int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    print(v1);
    
    v2.reize(v1.size());
    
    copy(v1.begin(), v1.end(), v2.begin());
    print(v2);
    return 0;
}
/*output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
 
*/

replace

  • replace(iterator beg, iterator end, oldvalue, newvalue);
title:Important
替换目标容器值常用算法
#include <iostream>
#include <vector>
using namespace std;
 
void print(vector<int>& v)
{
    for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
 
int main()
{
    vector<int>v1;
    
    for(int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    print(v1);
    
    repalce(v1.begin(), v1.end(), 2, 100);
    print(v1);
    return 0;
}
/*output:
0 1 100 3 4 5 6 7 8 9
 
*/

swap

  • swap(iterator beg, iterator end, _pred, newvalue);
title:Important
vector收缩常用算法
#include <iostream>
#include <vector>
using namespace std;
 
void print(vector<int>& v)
{
    for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
 
int main()
{
    vector<int>v1;
    
    for(int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    print(v1);
    
    repalce(v1.begin(), v1.end(), 2, 100);
    print(v1);
    return 0;
}
/*output:
0 1 100 3 4 5 6 7 8 9
 
*/