list assign() function in C++ STL - GeeksforGeeks

18 Jun, 2018
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.
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
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

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

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

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

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

CPP-Functions
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Operator Overloading in C++
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
List in C++ Standard Template Library (STL)
Pair in C++ Standard Template Library (STL)
Convert string to char array in C++
new and delete operators in C++ for dynamic memory
Destructors in C++
Queue in C++ Standard Template Library (STL)