std::vector::swap
STD::向量::SWAP
void swap( vector& other | | (until C++17) |
---|---|---|
void swap( vector& other ) noexcept(/* see below */ | | (since C++17) |
将容器的内容与other
不对单个元素调用任何移动、复制或交换操作。
所有迭代器和引用仍然有效。过去的迭代器无效.
If std::allocator_traits | (since C++11) |
---|
参数
other | - | container to exchange the contents with |
---|
返回值
%280%29
例外
(none). | (until C++17) |
---|---|
noexcept specification: noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value || std::allocator_traits<Allocator>::is_always_equal::value) | (since C++17) |
复杂性
常量。
另见
std::swap(std::vector) | specializes the std::swap algorithm (function template) |
---|
例
二次
#include <vector>
#include <iostream>
void printVector(std::vector<int>& vec)
{
for (int a : vec)
{
std::cout << a << " ";
}
}
int main()
{
std::vector<int> v1{1, 2, 3};
std::vector<int> v2{7, 8, 9};
std::cout << "v1: ";
printVector(v1
std::cout << "\nv2: ";
printVector(v2
std::cout << "\n-- SWAP\n";
v2.swap(v1
std::cout << "v1: ";
printVector(v1
std::cout << "\nv2: ";
printVector(v2
}
二次
产出:
二次
v1: 1 2 3
v2: 7 8 9
-- SWAP
v1: 7 8 9
v2: 1 2 3
二次
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。