Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

풀이 보관함

[C++] memset(), fill(), fill_n() (메모리 초기화 함수, 사용 방법) 본문

etc.

[C++] memset(), fill(), fill_n() (메모리 초기화 함수, 사용 방법)

viin 2022. 8. 27. 21:18

memset()

https://en.cppreference.com/w/cpp/string/byte/memset

 

std::memset - cppreference.com

Copies the value static_cast (ch) into each of the first count characters of the object pointed to by dest. If the object is a potentially-overlapping subobject or is not TriviallyCopyable (e.g., scalar, C-compatible struct, or an array of trivially copyab

en.cppreference.com

- 메모리 변화가 있는건 fill() 권유 하고 있다. (위에 글 읽기)

- 1 byte 단위씩 초기화 하기 때문에 0, false, NULL로 초기화 할 때만 사용해야 한다. 

 

 

2차원 배열 초기화 방법

#include <memory.h>
bool visited[22][33];
std::memset(visited, false, sizeof(visited));

 

fill()

https://en.cppreference.com/w/cpp/algorithm/fill

 

std::fill - cppreference.com

(1) template< class ForwardIt, class T > void fill( ForwardIt first, ForwardIt last, const T& value ); (until C++20) template< class ForwardIt, class T > constexpr void fill( ForwardIt first, ForwardIt last, const T& value ); (since C++20) template< class

en.cppreference.com

- memset과 달리 자료형의 제약이 없는 편

 

초기화 사용 방법

std::fill(arr,arr+5,0); //배열, 배열+범위 값, 초기화 값

 

fill_n()

https://en.cppreference.com/w/cpp/algorithm/fill_n

 

std::fill_n - cppreference.com

(1) template< class OutputIt, class Size, class T > void fill_n( OutputIt first, Size count, const T& value ); (until C++11) template< class OutputIt, class Size, class T > OutputIt fill_n( OutputIt first, Size count, const T& value ); (since C++11) (until

en.cppreference.com

 

초기화 사용 방법

std::fill_n(arr, 100, 0) // 배열, 초기화할 원소 개수, 초기화 값

 

 

🔗 더 자세하고 한글로 정리 잘 된 블로그 글 소개

 https://blog.naver.com/oddish0513/222802738143