분류 전체보기
-
c++ 배열 사용 팁백준코딩일기 2020. 10. 17. 09:20
1. 전체를 특정 값으로 초기화 시킬 때, 효율적으로 할 수 있는 방법 1). cstring 헤더에 있는 memset함수를 활용하는 방법 -> 실수할 여지가 굉장히 많음. 0이나 -1 이 아닌 다른 값을 넣으면 오동작하거나, 2차원 이상의 배열을 함수의 인자로 넘겨 그 곳에서 memset을 하면 잘 못 들어가기도 함. int a[10]; memset(a, 0, sizeof a); 2). for문을 돌면서 하나하나 값을 바꾸는 방식 -> 실수할 여지가 없어서 무난하고 좋음 int b[10]; for(int i=0; i 실수의 여지가 적음. 코드가 짧아서 익숙해진다면 가장 추천하는 방식 int c[10]; fill(c, c+10, 0); int c[10]; for(int i=0; i