Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::vector::swap — exchanging two vectors

Exchange two vectors’ contents and capacities in constant time. Check allocator compatibility and learn which iterators remain usable after the swap.

Version: ISO C++23; implementation support may vary

Syntax

// <vector>, ISO C++23; member declaration inside std::vector<T, Allocator>
constexpr void swap(vector& other) noexcept(
    std::allocator_traits<Allocator>::propagate_on_container_swap::value ||
    std::allocator_traits<Allocator>::is_always_equal::value);

// Call with two mutable lvalues of the same vector type:
a.swap(b);
// The non-member overload for vectors delegates to the member:
std::swap(a, b);

std::vector::swap is a member function from <vector>. Calling a.swap(b) modifies two vectors of the same type and returns void. It exchanges their contents, including their sizes, and their capacities in constant time without copying, moving, or swapping individual elements. The non-member overload std::swap(a, b) for vectors calls the member function.

If the allocator trait propagate_on_container_swap is true, the allocators are exchanged too. Otherwise they stay with their original vectors and must compare equal; if they do not, the behavior is undefined. In ISO C++23 the member is constexpr, and its noexcept specification uses the logical OR of the propagate_on_container_swap and is_always_equal traits. Default std::allocator instances of the same type always compare equal, and swapping their vectors is noexcept.

Iterators, pointers, and references to elements remain valid: they still refer to the same elements, now in the other vector. A saved end() iterator does not refer to an element and may be invalidated. After the swap, obtain a fresh end() from the vector that now owns the elements.

To commit a change, prepare and validate a separate vector before swapping it with the active one. push_back appends an element and can reallocate, so obtain the iterator after appending. std::find_if searches from begin() up to but not including end(); a result equal to end() means no match. size() returns the element count, not the capacity.

If preparation fails, the active vector is unchanged. After the commit, the working vector holds the previous contents of the active vector. swap is not atomic and does not replace synchronization for conflicting accesses from different threads.

Examples

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

int main() {
    std::vector<int> active{1, 2};
    auto pending = active;
    pending.push_back(7);

    auto found = std::find_if(pending.begin(), pending.end(),
                              [](int value) { return value == 7; });
    if (found == pending.end()) {
        return 1;
    }

    active.swap(pending);

    std::cout << "active size: " << active.size() << '\n';
    std::cout << "pending size: " << pending.size() << '\n';
    std::cout << "saved element: " << *found << '\n';
}

Result

active size: 3
pending size: 2
saved element: 7

`push_back` adds `7` to `pending`. After `active.swap(pending)`, `active` holds three elements, `pending` holds two, and `found` still refers to the same `7`, now in `active`.

Related lessons

  • C++ · Lesson 15 · Advanced · Version: 3

Sources