int arr[5] = {1, 2, 3, 4, 5}; std::arraymyArr {5, 4, 3, 2, 1}; std::cout << "Before clear:\n"; for(int i=0; i<5; i++) { std::cout << arr[i] << " "; std::cout << myArr[i] << " "; } std::cout << "\n"; std::memset(arr, 0, sizeof(arr)); myArr.fill(0); std::cout << "After clear:\n"; for(int i=0; i<5; i++) { std::cout << arr[i] << " "; std::cout << myArr[i] << " "; } std::cout << "\n";
Before clear: 1 5 2 4 3 3 2 1 4 5 After clear: 0 0 0 0 0 0 0 0 0 0
char str[] = "hello, world!"; std::arraymyStr {'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'}; std::cout << "Before clear:\n" << str << "\n" << myStr.data() << "\n"; std::memset(str, 0, sizeof(str)); myStr.fill('\0'); std::cout << "After clear:\n" << str << "\n" << myStr.data() << "\n";
Before clear: hello, world! hello, world! After clear:In both examples, we use `std::memset` to set all the elements to their default value. We also manipulate an `std::array` instance using its `fill` method. These functions are part of the C++ Standard Library.