The Wayback Machine - https://web.archive.org/web/20240711062414/https://www.geeksforgeeks.org/list-assign-function-in-c-stl/
Open In App

list assign() function in C++ STL

Last Updated : 18 Jun, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The list::assign() is a built-in function in C++ STL which is used to assign values to a list. It can also be used to copy elements from one list to another.

  1. To assign elements to a list.
    Syntax:

    list_name.assign(count, value) 
    

    Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below:

    • count: The number of values needed to be assigned to the list with name list_name.
    • value: This is the value that will be assigned count number of times starting from the first element. If the list already contains some elements then those elements will be replaced by the element specified in the parameter value. The data type of this parameter must be of the same type as that of list_name.

    Return Value: This function assigns the element specified in the value parameter count number of times starting from the first element in the list list_name. The return type of this function is void and it does not returns any value.

    Below program illustrates the list::assign() function.




    // CPP program to illustrate the
    // list::assign() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // Initialization of list
        list<int> demo_list;
      
        // Assigning the value 100, 5 times
        // to the list, list_demo.
        demo_list.assign(5, 100);
      
        // Displaying the list
        for (int itr : demo_list) {
            cout << itr << " ";
        }
      
        return 0;
    }

    
    

    Output:

    100 100 100 100 100
    
  2. To copy elements from an existing list to a new list.
    Syntax:

    first_list.assign(second_list.begin(), second_list.end());
    

    Parameters: This function accepts two parameter as shown in the above syntax. The first parameter is the beginning iterator of the second list and second parameter is the ending iterator of the second list.

    Return Value: This function copies the second_list to first_list. This function does not returns any value.

    Below program illustrate the list::assign() function:




    // CPP program to illustrate the
    // list::assign() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // Initialization of list
        list<int> first_list;
      
        // Initializing second list
        list<int> second_list;
      
        // Assigning the value 100, 5 times
        // to the second_list.
        second_list.assign(5, 100);
      
        // Copying second_list to first_list
        first_list.assign(second_list.begin(),
                          second_list.end());
      
        for (int itr : first_list) {
            cout << itr << " ";
        }
      
        return 0;
    }

    
    

    Output:

    100 100 100 100 100
    


Previous Article
Next Article

Similar Reads

forward_list assign() function in C++ STL
The forward_list::assign() is a function in C++ STL which assigns new content to a forward list, replacing its current content and adjusting its size as required.Syntax: Version 1:forward_list_name.assign(iterator it1, iterator it2) Version 2:forward_list_name.assign(int n, val) Version 3:forward_list_name.assign(initializer_list li) Parameters: Th
2 min read
vector :: assign() in C++ STL
vector:: assign() is an STL in C++ which assigns new values to the vector elements by replacing old ones. It can also modify the size of the vector if necessary. The syntax for assigning constant values: vectorname.assign(int size, int value) Parameters: size - number of values to be assigned value - value to be assigned to the vectorname Program 1
3 min read
C++ 17 | New ways to Assign values to Variables
C++ 17 introduced many new ways to declare a variable. Earlier assignment and declaration was done using "=" Example: int a = 5; But now 2 more ways are introduced in C++17. They are: Constructor initialization: In this way, the value of the variable is enclosed in parentheses ( () ). In this way, value can be passed in two ways shown below. #inclu
2 min read
Difference Between deque::cbegin and deque::assign in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::at in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::back in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Deque::assign deque::assign is
3 min read
Difference Between deque::begin and deque::assign in C++
Deque or Double-ended queue are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the difference
3 min read
Difference Between deque::assign and deque::empty in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
2 min read
Conditionally assign a value without using conditional and arithmetic operators
Given 4 integers a, b, y, and x, where x can assume the values of either 0 or 1 only. The following question is asked: If 'x' is 0, Assign value 'a' to variable 'y' Else (If 'x' is 1) Assign value 'b' to variable 'y'. Note: - You are not allowed to use any conditional operator (including the ternary operator) or any arithmetic operator (+, -, *, /)
6 min read
std::string::assign() in C++
The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string&amp; string::assign (const string&amp; str) str : is the string to be assigned. Returns : *this C/C++ Code // CPP code for assign (const string&amp; str) #include &lt;iostre
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg