std::assign
std::assign
is a member function of several standard C++ containers, including std::vector
, std::list
, and std::forward_list
. It provides a convenient way to replace the contents of a container with a new set of elements. This function is particularly useful when you want to overwrite the existing elements of a container or initialize it with a specific set of values.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Assign new values to the vector
vec.assign({10, 20, 30, 40, 50});
std::cout << "Vector after assign: ";
std::for_each(vec.begin(), vec.end(), [](int n) { std::cout << n << " "; });
std::cout << std::endl;
return 0;
}
In this example, we start with a vector containing the values 1 to 5. We then use assign
to replace its contents with a new set of values (10 to 50). The assign
function overwrites the existing elements and adjusts the size of the vector if necessary.
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
int main() {
std::list<double> source = {1.1, 2.2, 3.3, 4.4, 5.5};
std::vector<double> destination;
// Assign elements from the list to the vector
destination.assign(source.begin(), source.end());
std::cout << "Vector after assign: ";
std::for_each(destination.begin(), destination.end(), [](double n) { std::cout << n << " "; });
std::cout << std::endl;
return 0;
}
assign
with iterators.std::list
of doubles and an empty std::vector
.assign
function is used to copy all elements from the list to the vector.assign
, as it can work with different container types and transfer elements between them.#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<std::string> vec;
// Assign 5 copies of "Hello" to the vector
vec.assign(5, "Hello");
std::cout << "Vector after assign: ";
std::for_each(vec.begin(), vec.end(), [](const std::string& s) { std::cout << s << " "; });
std::cout << std::endl;
return 0;
}
assign
that takes a count and a value.#include <iostream>
#include <list>
#include <algorithm>
int main() {
std::list<char> charList;
// Assign characters from an array
char chars[] = {'a', 'b', 'c', 'd', 'e'};
charList.assign(std::begin(chars), std::end(chars));
std::cout << "List after assign: ";
std::for_each(charList.begin(), charList.end(), [](char c) { std::cout << c << " "; });
std::cout << std::endl;
// Reassign with multiple copies
charList.assign(3, 'X');
std::cout << "List after reassign: ";
std::for_each(charList.begin(), charList.end(), [](char c) { std::cout << c << " "; });
std::cout << std::endl;
return 0;
}
assign
with std::list
.assign
works similarly across different container types that support it.When using assign
, it's important to note:
assign
replaces all elements in the container, which can be more efficient than clearing the container and then inserting new elements one by one.std::assign
is a powerful and flexible function for replacing the contents of containers in C++. It can be used with initializer lists, ranges specified by iterators, or to fill a container with multiple copies of a value. This function is supported by several standard containers including std::vector
, std::list
, and std::forward_list
.
Key points to remember: - It replaces all existing elements in the container. - It can change the size of the container. - It provides a convenient way to initialize or reset a container's contents. - It works with different types of input (initializer lists, ranges, count and value).
By using std::assign
, you can write more concise and efficient code when you need to replace the entire contents of a container, making it a valuable tool in C++ programming.