list assign() function in C++ STL

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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
    }

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
    }

    chevron_right

    
    

    Output:

    100 100 100 100 100
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.