1. fill()
#include<iostream>
#include <algorithm> // std::fill
using namespace std;
int a[10];
int b[10][10];
int main(){
fill(&a[0], &a[10], 100);
for(int i = 0; i<10; i++){
cout<<a[i]<<" ";
}
cout << endl;
//100 100 100 100 100 100 100 100 100 100
Code language: PHP (php)
fill(&a[0], &a[10], 100);:a[0]부터 시작해a[9]까지 초기화하고 싶을 때.a[10]은 포함하지 않고 그 이전값인a[9]까지 전부 초기화한다.
끝값은 포함하지 않고 초기화합니다!!!
...
fill(&b[0][0], &b[9][10], 2);
for(int i=0; i<10; i++){
cout << i << " : ";
for(int j=0; j<10; j++){
cout << b[i][j] << " ";
}
cout << endl;
}
/*
0 : 2 2 2 2 2 2 2 2 2 2
1 : 2 2 2 2 2 2 2 2 2 2
2 : 2 2 2 2 2 2 2 2 2 2
3 : 2 2 2 2 2 2 2 2 2 2
4 : 2 2 2 2 2 2 2 2 2 2
5 : 2 2 2 2 2 2 2 2 2 2
6 : 2 2 2 2 2 2 2 2 2 2
7 : 2 2 2 2 2 2 2 2 2 2
8 : 2 2 2 2 2 2 2 2 2 2
9 : 2 2 2 2 2 2 2 2 2 2
*/
Code language: JavaScript (javascript)
fill(&b[0][0], &b[9][10], 2);: 2차원 배열 초기화. b[9][9]까지만 초기화.
...
fill(a, a+10, 99);
for(int i=0; i<10; i++){
cout << a[i] << " ";
}
cout << endl;
//99 99 99 99 99 99 99 99 99 99
Code language: JavaScript (javascript)
fill(a, a+10, 99);: 1차원 배열을 초기화하는 또 다른 방법.
...
fill(&b[0][0], &b[0][0] + 9*10, 9);
for(int i=0; i<10; i++){
cout << i << " : ";
for(int j=0; j<10; j++){
cout << b[i][j] << " ";
}
cout << endl;
}
/*
0 : 9 9 9 9 9 9 9 9 9 9
1 : 9 9 9 9 9 9 9 9 9 9
2 : 9 9 9 9 9 9 9 9 9 9
3 : 9 9 9 9 9 9 9 9 9 9
4 : 9 9 9 9 9 9 9 9 9 9
5 : 9 9 9 9 9 9 9 9 9 9
6 : 9 9 9 9 9 9 9 9 9 9
7 : 9 9 9 9 9 9 9 9 9 9
8 : 9 9 9 9 9 9 9 9 9 9
9 : 2 2 2 2 2 2 2 2 2 2
*/
Code language: JavaScript (javascript)
- 해당 방식으로 초기화할 경우 2차원 이상일 경우
&를 사용해야 한다. fill(&b[0][0], &b[0][0] + 9*10, 9);:10*10이나 9*9아닌9*10이어야 한다.
fill(&b[0][0], &b[0][0] + 10*10, 1);
for(int i=0; i<10; i++){
cout << i << " : ";
for(int j=0; j<10; j++){
cout << b[i][j] << " ";
}
cout << endl;
}
return 0;
}
/*
0 : 1 1 1 1 1 1 1 1 1 1
1 : 1 1 1 1 1 1 1 1 1 1
2 : 1 1 1 1 1 1 1 1 1 1
3 : 1 1 1 1 1 1 1 1 1 1
4 : 1 1 1 1 1 1 1 1 1 1
5 : 1 1 1 1 1 1 1 1 1 1
6 : 1 1 1 1 1 1 1 1 1 1
7 : 1 1 1 1 1 1 1 1 1 1
8 : 1 1 1 1 1 1 1 1 1 1
9 : 1 1 1 1 1 1 1 1 1 1
*/
Code language: JavaScript (javascript)
- fill(&b[0][0], &b[0][0] + 10*10, 1); : 이 방법이 가장 사용하기 편한것 같다.
2. memset()
memset은 fill과는 다르게 바이트 단위로 초기화를 수행한다.
0, -1, char형의 문자 하나로 초기화 할 수 있다. (2의 보수 표기법에 따라서 11111…은 -1이 된다.)
#include<iostream>
#include <cstring> //std::memset
using namespace std;
const int max_n = 1004;
int a[max_n];
int a2[max_n][max_n];
int main(){
memset(a, -1, sizeof(a));
memset(a2, -1, sizeof(a2));
for(int i=0; i<10; i++) cout <<a[i]<<" ";
return 0;
}
//-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Code language: PHP (php)
memset(a, -1, sizeof(a));: memset(배열의 이름, k, 배열의 크기)
0 또는 -1이란 값으로 초기화할 때는 memset을 사용하는 것 추천한다.
3. memcpy
#include<iostream>
#include<string.h> //std::memcpy
using namespace std;
//void * memcpy ( void * destination, const void * source, size_t num );
int a[5], temp[5];
int main(){
for(int i=0; i<5; i++)a[i] = i;
//copy a
memcpy(temp, a, sizeof(a));
for(int i : temp) cout << i << ' ';
cout << endl;
//change a
a[4] = 9999;
for(int i : a) cout << i << ' ';
cout << endl;
// put a to temp (call by value)
memcpy(a, temp, sizeof(temp));
for(int i : a) cout << i << " ";
return 0;
}
/*
0 1 2 3 4
0 1 2 3 9999
0 1 2 3 4
*/
Code language: PHP (php)
call by value로 복사하고 싶을 때 사용한다.