复制

复制操作都会覆盖掉原有的元素

普通复制

使用copy()函数

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 6};
    vector<int> b(4);
    copy(a.begin(), a.end(), b.begin());
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

复制n个元素

使用copy_n()

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 6};
    vector<int> b(3);
    copy_n(a.begin(), 3, b.begin());
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

条件复制

使用copy_if()函数

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 6};
    vector<int> b(3);
    copy_if(a.begin(), a.end(), b.begin(), [](int i) {
        return i < 6;
    });
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

从背后复制

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 6};
    vector<int> b(4);
    copy_backward(a.begin(), a.begin() + 3, b.end());
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

移动操作

移动操作与复制操作差不多

普通移动操作

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 6};
    vector<int> b(4);
    move(a.begin(), a.end(), b.begin());
    for(auto it = a.begin(); it != a.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

从后面移动

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 6};
    vector<int> b(4);
    move_backward(a.begin(), a.end(), b.end());
    for(auto it = a.begin(); it != a.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

对换

对换既可以对换两个数,又可以对换两个数组

普通对换

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    int x = 10;
    int y = 20;
    swap(x, y);
    cout << "x: " << x << "  " << "y: " << y << "\n";
    vector<int> a(4, 10);
    vector<int> b(4, 20);
    swap(a, b);
    for(auto it = a.begin(); it != a.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

范围交换

使用swap_range()函数

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a(4, 10);
    vector<int> b(4, 20);
    swap_ranges(a.begin() + 1, a.end() - 1, b.begin());
    for(auto it = a.begin(); it != a.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

迭代器交换

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a(4, 10);
    vector<int> b(4, 20);
    iter_swap(a.begin(), b.begin());
    for(auto it = a.begin(); it != a.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

转换

主要是通过一个lambda表达式来转换,并且把转换的结果赋到另一个序列中

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a(4, 10);
    vector<int> b(4, 20);
    transform(a.begin(), a.end(), b.begin(), [](int i) {
        return ++i;
    });
    for(auto it = a.begin(); it != a.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

替换

普通替换

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {10, 30, 10, 50};
    vector<int> b(4, 20);
    //将迭代器范围内的所有10替换为20
    replace(a.begin(), a.end(), 10, 20);
    for(auto it = a.begin(); it != a.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

条件替换

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {10, 30, 10, 50};
    vector<int> b(4, 20);
    replace_if(a.begin(), a.end(), [](int i) {
        return i == 30;
    }, 20);
    for(auto it = a.begin(); it != a.end(); it++) {
        cout << *it << " ";
    }
    cout << "\n";
    for(auto it = b.begin(); it != b.end(); it++) {
        cout << *it << " ";
    }
    return 0;
}

替换副本

将副本进行替换后再复制到

🌹💗正文结束💗🌹