Skip to content
Related Articles

Related Articles

Improve Article

list assign() function in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 18 Jun, 2018
Geek Week

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
    
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :