std::assign


Function: std::assign

std::assign is a member function of several standard C++ containers, including std::vector, std::list, and std::forward_list. It provides a convenient way to replace the contents of a container with a new set of elements. This function is particularly useful when you want to overwrite the existing elements of a container or initialize it with a specific set of values.

Example 1: Basic Usage with std::vector

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

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};

    // Assign new values to the vector
    vec.assign({10, 20, 30, 40, 50});

    std::cout << "Vector after assign: ";
    std::for_each(vec.begin(), vec.end(), [](int n) { std::cout << n << " "; });
    std::cout << std::endl;

    return 0;
}

Explanation

In this example, we start with a vector containing the values 1 to 5. We then use assign to replace its contents with a new set of values (10 to 50). The assign function overwrites the existing elements and adjusts the size of the vector if necessary.

Example 2: Assigning a Range of Elements

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

int main() {
    std::list<double> source = {1.1, 2.2, 3.3, 4.4, 5.5};
    std::vector<double> destination;

    // Assign elements from the list to the vector
    destination.assign(source.begin(), source.end());

    std::cout << "Vector after assign: ";
    std::for_each(destination.begin(), destination.end(), [](double n) { std::cout << n << " "; });
    std::cout << std::endl;

    return 0;
}

Explanation

Example 3: Assigning Multiple Copies of a Value

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

int main() {
    std::vector<std::string> vec;

    // Assign 5 copies of "Hello" to the vector
    vec.assign(5, "Hello");

    std::cout << "Vector after assign: ";
    std::for_each(vec.begin(), vec.end(), [](const std::string& s) { std::cout << s << " "; });
    std::cout << std::endl;

    return 0;
}

Explanation

Example 4: Using assign with std::list

#include <iostream>
#include <list>
#include <algorithm>

int main() {
    std::list<char> charList;

    // Assign characters from an array
    char chars[] = {'a', 'b', 'c', 'd', 'e'};
    charList.assign(std::begin(chars), std::end(chars));

    std::cout << "List after assign: ";
    std::for_each(charList.begin(), charList.end(), [](char c) { std::cout << c << " "; });
    std::cout << std::endl;

    // Reassign with multiple copies
    charList.assign(3, 'X');

    std::cout << "List after reassign: ";
    std::for_each(charList.begin(), charList.end(), [](char c) { std::cout << c << " "; });
    std::cout << std::endl;

    return 0;
}

Explanation

Performance Considerations

When using assign, it's important to note:

  1. assign replaces all elements in the container, which can be more efficient than clearing the container and then inserting new elements one by one.
  2. For vectors, if the new size is larger than the current capacity, reallocation will occur.
  3. For lists and forward lists, no reallocation occurs, but all existing nodes are destroyed and new ones are created.

Summary

std::assign is a powerful and flexible function for replacing the contents of containers in C++. It can be used with initializer lists, ranges specified by iterators, or to fill a container with multiple copies of a value. This function is supported by several standard containers including std::vector, std::list, and std::forward_list.

Key points to remember: - It replaces all existing elements in the container. - It can change the size of the container. - It provides a convenient way to initialize or reset a container's contents. - It works with different types of input (initializer lists, ranges, count and value).

By using std::assign, you can write more concise and efficient code when you need to replace the entire contents of a container, making it a valuable tool in C++ programming.

Related

Previous Page | Course Schedule | Course Content