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

2022. 8. 27. 21:18·programming/개념 정리

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

저작자표시 비영리 변경금지 (새창열림)
'programming/개념 정리' 카테고리의 다른 글
  • [C++] NULL vs nullptr, 포인터에서 nullptr을 사용해야 하는 이유
  • [C++] #define과 const 그리고 constexpr
  • [Javs SE 8] Function<T, R> 과 apply()
  • [OS] LRU 알고리즘이란? 정의부터 구현까지 알아보자!
u1qns
u1qns
http://github.com/u1qns
  • u1qns
    개발 블로그
    u1qns
  • 전체
    오늘
    어제
    • 분류 전체보기 (173)
      • 회고 (1)
      • programming (17)
        • 개념 정리 (6)
        • CI CD (1)
        • 트러블 슈팅 (0)
        • 환경설정 및 팁 (7)
      • problem solving (155)
        • 개념 정리 (3)
        • 백준 (129)
        • SWEA (15)
        • 프로그래머스 (4)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

    • 깃허브
  • 공지사항

  • 인기 글

  • 태그

    C++
    POW
    cpp
    되추적
    boj
    투포인터
    SWEA
    완전탐색
    미해결
    DFS
    HELLOSSAFY
    그리디
    구현
    SSAFY수료식
    SSAFY
    삼성청년SW아카데미
    BFS
    DP
    cmath
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
u1qns
[C++] memset(), fill(), fill_n() (메모리 초기화 함수, 사용 방법)
상단으로

티스토리툴바