C++ std::list::assign

The std::list::assign function replaces the contents of a list with new elements. It can be used to assign elements from a range, a count of identical elements, or from an initializer list. This function ensures that the previous contents of the list are cleared before adding the new elements.


Syntax of std::list::assign

</>
Copy
// 1. Range
template <class InputIterator>
void assign(InputIterator first, InputIterator last);

// 2. Fill
void assign(size_type n, const value_type& val);

// 3. Initializer List
void assign(std::initializer_list<value_type> il);

Parameters

ParameterDescription
first, lastIterators defining the range of elements to assign.
nThe number of elements to assign.
valThe value to assign to the elements.
ilAn initializer list of elements to assign to the list.

Return Value

This function does not return any value. It modifies the contents of the list in place.

Exceptions

The function may throw exceptions if memory allocation fails or if the assignment of elements throws an exception. The function provides strong exception safety: if an exception occurs, the list remains unchanged.

To avoid undefined behavior, ensure that iterators passed to the range version of assign() are valid and point to the same container.


Examples for std::list::assign

Example 1: Assigning a Range of Elements

This example demonstrates how to use assign() to replace the contents of a list with elements from another container like a vector.

Program

</>
Copy
#include <iostream>
#include <list>
#include <vector>

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

    myList.assign(myVector.begin(), myVector.end());

    std::cout << "List contents: ";
    for (const auto& elem : myList) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. A std::list named myList is created and initially empty.
  2. A std::vector named myVector is initialized with the elements {1, 2, 3, 4, 5}.
  3. The function myList.assign(myVector.begin(), myVector.end()) is used to copy all the elements from the vector to the list:
    • myVector.begin(): Points to the first element of the vector.
    • myVector.end(): Points to one past the last element of the vector.
    • The assign function inserts all elements from the specified range into the list, replacing any existing elements in the list.
  4. A range-based for loop is used to iterate through all the elements of myList and print them to output.

Output:

List contents: 1 2 3 4 5

Example 2: Assigning a Count of Identical Elements

This example demonstrates how to use assign() to replace the contents of a list with multiple identical elements:

Program

</>
Copy
#include <iostream>
#include <list>

int main() {
    std::list<int> myList;

    myList.assign(5, 10); // Assign 5 elements, each with the value 10

    std::cout << "List contents: ";
    for (const auto& elem : myList) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. A std::list named myList is created and initially empty.
  2. The function myList.assign(5, 10) is used to fill the list with a specified number of elements, each initialized to the same value:
    • The first argument (5) specifies the number of elements to insert into the list.
    • The second argument (10) specifies the value to assign to each element.
    • After this operation, the list contains 5 elements, each with the value 10.
  3. A range-based for loop is used to iterate through all the elements of myList and print them to output.

Output:

List contents: 10 10 10 10 10

Example 3: Assigning from an Initializer List

This example demonstrates how to use assign() to replace the contents of a list with an initializer list:

Program

</>
Copy
#include <iostream>
#include <list>

int main() {
    std::list<int> myList;

    myList.assign({7, 8, 9}); // Assign elements from an initializer list

    std::cout << "List contents: ";
    for (const auto& elem : myList) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. A std::list named myList is created and initially empty.
  2. The function myList.assign is used with an initializer list {7, 8, 9}:
    • The initializer list {7, 8, 9} specifies the elements to be added to the list.
    • The assign function replaces any existing elements in the list with the elements provided in the initializer list.
    • After this operation, the list contains the elements 7, 8, and 9.
  3. A range-based for loop is used to iterate through all the elements of myList and print them to output.

Output:

List contents: 7 8 9