What is Array?
์ง์ ์ ์ผ๋ก ๊ฐ์ ์์ฐจ์ ์ผ๋ก ๋งคํํ๋ค. C์ธ์ด์์ ์ค๋๋ ๊ธฐ์ ์ด๊ณ , ๊ฐ์ฒด๊ฐ ์๋๋ค.
์ฒ์์ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๊ณ ์ ์ธํ๋ ์ ์ ๋ฐฐ์ด(static Array)์ด ์๊ณ , ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ์ ๋์ ์ผ๋ก ์กฐ์ ํ ์ ์๋ ๋์ ๋ฐฐ์ด(Dynamic Array)์ด ์๋ค.
๋ฐฐ์ด๋ ํจ์ ํฌ์ธํฐ์ ๋ง์ฐฌ๊ฐ์ง๋ก, ํน์ ๋ฐฐ์ด์ ๋ด๊ฐ ์ ์ธํ๊ฒ ๋๋ฉด ๋ฐฐ์ด์ ์ด๋ฆ์ผ๋ก ์ ์ธํด์ค ๋ณ์๋ย ํฌ์ธํฐย ์ด๋ค.
์ ์ ๋ฐฐ์ด (Static Array)
๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์์ ๋ฐฐ์ด ์์์ ๋ฐฐ์น
#include <iostream>
using namespace std;
int main(){
int ary[3] = {1, 2, 3};
cout << ary << endl;
cout << endl;
cout << ary[0] << endl;
cout << ary + 0 << endl;
cout << *(ary+0) << endl;
cout << endl;
cout << ary[1] << endl;
cout << ary + 1 << endl;
cout << *(ary+1) << endl;
cout << endl;
cout << ary[2] << endl;
cout << ary + 2 << endl;
cout << *(ary+2) << endl;
return 0;
}
0x7ffeefbff4fc
1
0x7ffeefbff4fc
1
2
0x7ffeefbff500
2
3
0x7ffeefbff504
3
Program ended with exit code: 0
๊ฐ๊ฐ์ ์ฃผ์๋ฅผ ๊ฐ๋จํ๊ฒ ๋ํ๋ด๊ณ , ์ด๊ฒ์ ํ๋ก ๋ํ๋ด๋ณด๋ฉด,
Array element ์ ๊ทผ, ์ถ๋ ฅ ๋ฐฉ๋ฒ
#include <iostream>
using namespace std;
int main(){
int ary[] = {1,2,3,4,5};
for(int i = 0; i < 5; i++){
cout << a[i] << endl;
}
return 0;
}
์ด๋ฒ์๋ ์ด ๋ฐฐ์ด์ ํจ์์ ์ธ์๋ก ๋ฐ์ ์ถ๋ ฅํด์ฃผ๋ย print
ย ํจ์๋ฅผ ๋ง๋ค์ด๋ณด์.
#include <iostream>
using namespace std;
void print(const int *ary, int length){ // ์ด ํจ์ ๋ด์์ ary๋ ๋ณํํ์ง ์์์ ์๋ฏธํจ.
for(int i = 0; i < length; i++){
cout << *(ary + i) << "\t";
}
cout << endl;
}
int main(){
int ary[] = {1,2,3,4,5};
print(ary, 5);
return 0;
}
์ด๋ฒ์๋ ์ด ๋ฐฐ์ด์ ํจ์์ ์ธ์๋ก ๋ฐ์ ๋ชจ๋ ์์์ ํฉ์ ๊ตฌํด์ฃผ๋ย sum
ย ํจ์๋ฅผ ๋ง๋ค์ด๋ณด์.
#include <iostream>
using namespace std;
int sum(int* begin, int* end){ // ์ธ์๋ก ๋๊ธด ๊ฐ์ด ์ฃผ์์ด๊ธฐ ๋๋ฌธ์ ํฌ์ธํฐ ๋ณ์๋ก ๋ฐ์์ค๋ค.
int result = 0;
for(int* i = begin; i < end; i++){ // ํฌ์ธํฐ ๋ณ์์ ์์์ ๋ถํฐ ๋ฃจํ๋ฅผ ๋๋ ค์ผ ํ๋ฏ๋ก
// i๋ ํฌ์ธํฐ ๋ณ์๋ก ์ก์์ค๋ค.
result += *i;
}
return result;
}
int main(){
int ary[] = {1,2,3,4,5};
int* begin, *end; // int ์๋ฃํ์ ์ฃผ์๋ฅผ ๋ฐ๋ ํฌ์ธํฐ ๋ณ์ ๋๊ฐ๋ฅผ ์ ์ธํ๋ค.
begin = ary; // ๋ฐฐ์ด์ ์์์ฃผ์๋ฅผ ํฌ์ธํฐ ๋ณ์ begin์ ๋ฐ๋๋ค.
end = ary + 5; // ๋ฐฐ์ด์ ๋์ฃผ์๋ฅผ end์ ๋ฐ๋๋ค.
cout << sum(begin, end) << endl; // ๋ ์ฃผ์๋ฅผ ๋๊ฒผ์ ๋, ์ถ๋ ฅ ๊ฐ์ ๋ฐ์ ํ๋ฉด์ ๋์ด๋ค.
return 0;
}
while ๋ฌธ๋ ํ๋ฒ ์ฌ์ฉํด๋ณด์.
#include <iostream>
using namespace std;
int sum(int* begin, int* end){
int* curr;
curr = begin;
int result = 0;
while(curr != end){
result += *(curr);
curr++;
}
return result;
}
int main(){
int ary[] = {1,2,3,4,5};
int* begin, *end; // int ์๋ฃํ์ ์ฃผ์๋ฅผ ๋ฐ๋ ํฌ์ธํฐ ๋ณ์ ๋๊ฐ๋ฅผ ์ ์ธํ๋ค.
begin = ary; // ๋ฐฐ์ด์ ์์์ฃผ์๋ฅผ ํฌ์ธํฐ ๋ณ์ begin์ ๋ฐ๋๋ค.
end = ary + 5; // ๋ฐฐ์ด์ ๋์ฃผ์๋ฅผ end์ ๋ฐ๋๋ค.
cout << sum(begin, end) << endl; // ๋ ์ฃผ์๋ฅผ ๋๊ฒผ์ ๋, ์ถ๋ ฅ ๊ฐ์ ๋ฐ์ ํ๋ฉด์ ๋์ด๋ค.
return 0;
}
์ง๊ธ๊น์ง ์งํํ ์์ ์์ย ary[]
ย ๋ฅผ ์ ์ธํ ๋, ํฌ๊ธฐ๊ฐ ์ ํด์ง ๋
์์ ๋ฃ์ด์ฃผ์๋ค.
์ด๋ฐ ๊ฒ์ ์ ์ ๋ฐฐ์ด์ด๋ผํ๊ณ , ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ ์ ์๋ค.
#include <iostream>
using namespace std;
int main(){
const int length = 5;
int ary[length];
return 0;
}
์ ์ ๋ฐฐ์ด์ ์ด๋ ๊ฒ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ์ ์ธํ๋ ๋ณ์ length๋ฅผ ๋ง๋ค๋, ์ด ๊ฐ์ constant๋ก ๋ง๋ค์ด ์ฃผ์ด์ผ ํ๋ค.
๋ง์ฝย int length = 5
ย ์ ๊ฐ์ด ์ ์ธํด์ค๋ค๋ฉด, ๊ฐ์ด ๋ฐ๋ ์ ์์ผ๋ฏ๋ก ์ค๋ฅ๋ฅผ ๋ฑ๋๋ค.
๋์ ๋ฐฐ์ด (Dynamic Array)
#include <iostream>
using namespace std;
int main(){
int size;
cout << "Size : ";
cin >> size;
int* dyary;
dyary = new int [size]; // ๋์ ๋ฐฐ์ด ์์ฑ
// ์
๋ ฅ ๋ฐ๊ธฐ
for(int i = 0; i < size; i++){
cin >> dyary[i];
}
// ์ถ๋ ฅ ํ๊ธฐ
int *curr = 0;
int* begin, *end;
begin = dyary;
end = dyary + size;
curr = begin;
while(curr != end){
cout << *(curr) << "\t";
curr++;
}
cout << endl;
// ๋ฐฐ์ด ์ญ์ ํ๊ธฐ
delete[] dyary;
return 0;
}
Output
Size : 3
1
2
3
1 2 3
Program ended with exit code: 0
Matrix ๋ง๋ค๊ธฐ
#include <iostream>
using namespace std;
int main(){
double dMatrix[2][3] = { {1,2,3},
{4,5,6}
};
// ๋๋ ์ด๋ ๊ฒ ์ ์ธํด๋ ๋๋ค.
// dMatrix[0][0] = 1; dMatrix[0][1] = 2; dMatrix[0][2] = 3;
// dMatrix[1][0] = 4; dMatrix[1][1] = 5; dMatrix[1][2] = 6;
for (int row = 0; row < 2; row++){
for(int col = 0; col < 3; col++){
cout << dMatrix[row][col] << "\t";
}
cout << endl;
}
return 0;
}
2์ฐจ์ ๋ฐฐ์ด์์ ํฌ์ธํฐ์ ๊ด๊ณ
p[i] = *(p+i);
p[j][i] = *(*(p+j) + i)
๊ฒฐ๊ตญ 1์ฐจ์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๋์ด๋์ด ์๋ ์์๋ค์ ์ฃผ์๊ฐ๋ค์ ๊ด๊ณ๋ฅผ ์ ์ํด ๋์ ๊ฒ์ด๋ค.
๋ฌธ์์ด์์ ๋ฌธ์ ์ฐพ๊ธฐ
#include <iostream>
using namespace std;
bool find_char(const char *s, char c){
while(*s != '\0'){
if(*s == c)
return true;
s++;
}
return 0;
}
int main(){
const char* ch = "phrase";
for (char c = 'a'; c <= 'z'; c++){
cout << "[" << c << "] is ";
if(!find_char(ch, c))
cout << "Not ";
else
cout << " ";
cout << "in " << ch << endl;
}
return 0;
}
- main ์์ย
"phrase"
ย ๋ผ๋ ๋ฌธ์์ด์ ์ฒซ๋ฒ์งธ ์ฃผ์๋ฅผ char ํ ํฌ์ธํฐ ๋ณ์ ch์ ๋ด๋๋ค. - ๋ฐ๋ณต๋ฌธ ์์์ ์ ์ธ๋ย
char c
ย ๋ a~z ์ ๋ฌธ์๊ฐ์ ๊ฐ์ง๋๋ฐ, - ๋ง์ฝ ์ด ๋ฌธ์์ด์ดย
"phrase"
ย ์์ ์๋ค๋ฉด ๊ณต๋ฐฑ์, ์๋ค๋ฉดยNot
ย ์ ์ถ๋ ฅํ๋๋ก ๋ง๋ค์. - ๊ทธ๋ผ ํด๋น ๋ฌธ์์ด์ด ์๋์ง ์๋์ง๋ฅผ ํ๋จํด์ค ํจ์๋ฅผ ๋ฐ์ ๋ง๋ค๊ฑด๋ฐ,
- ์ด๋ ์
๋ ฅ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฌธ์์ด์ ์์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๋ย
ch
ย ์ ๊ฒ์ฌํ ๋ฌธ์ยc
ย ๋ฅผ ๋๊ฒจ์ฃผ์. ch
ย ๋ ๋ฌธ์์ด ํฌ์ธํฐ ๋ณ์์ด๋ฏ๋กยchar*
ย ๋ก ์ ์ธํด์ ๋ฐ์์ฃผ๊ณ ,c
ย ๋ ๋ณต์ฌํด์ ๋ฐ์์ค์.- ๋ฐ์์จ ์ฃผ์ย
s
ย ์ ๊ฐ์ผ๋ก ์ ํํย*s
ย ๋ ๋ฌธ์์ด์ ์ ์ผ ์ฒซ๋ฒ์งธ ๊ฐ์ธยp
ย ๋ฅผ ๋ํ๋ด๊ณ ์์ ๊ฒ์ด๋ค. - ์ด์ ์ฐ๋ฆฌ๋ ์ด ๊ฐ์ด NULL(
'\0'
) ์ด ์๋๋ฉด ๋์๊ฐ๋๋ฐ, - ๋ง์ฝ์ ์ถ๊ฐ๋ก ์
๋ ฅ๋ฐ์ย
c
ย ์ ๊ฐ๊ณผ ๊ฐ๋ค๋ฉด TRUE๋ฅผ, ์๋๋ฉด FALSE๋ฅผ ๋ฐํํ๋ค. - ์ด์ ์ถ๋ ฅ๊ฐ์ ๋ฐ๋ผ ์ ์ดํ๋ฉด ๋๋ค.
์ถ๊ฐ๋ก ๋ฌธ์์ด์ด ๊ตฌ์ฑ๋ ๋ ์ปดํจํฐ๋ ๋ง์ง๋งย '\0'
ย NULL ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ๋ค.
What is Vector Container?
- ๋ฉ๋ชจ๋ฆฌ์ ๋ธ๋ฝ์ ๊ด๋ฆฌํด์ฃผ๋ ๊ฐ์ฒด
- ์๋์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํด์ฃผ๋ ๋ฐฐ์ด
- ๋ฒกํฐ์ ๋ค์ด๊ฐ๋ ์๋ฃํ์ ๋ชจ๋ ๋์ผํด์ผ ํ๋ค.
Vector ์ ์ธ ๋ฐฉ๋ฒ
- ํค๋ํ์ผ ์ถ๊ฐ
- namespace ์ถ๊ฐ // std:: ์ฌ์ฉ
- ๋ฒกํฐ ์ ์ธ
#include <iostream>
using namespace std;
int main(){
vector<int> vec_a; // ์ด๋ฆ๋ง ์ ์ธํจ
vector<int> vec_b(10); // ์ต์ด ํฌ๊ธฐ 10์ผ๋ก ์ ์ธํจ
vector<int> vec_c(10, 8); // ์ต์ด ํฌ๊ธฐ 10, ์ด๊ธฐํ 8๋ก ์ ์ธํจ
vector<int> vec_d{10, 20, 30, 40} // 4๊ฐ ์์๋ก ๋ฒกํฐ๋ฅผ ๋ง๋ฆ
return 0;
}
๋ฒกํฐ ์ธ๋ฑ์ฑ
๋ฐฐ์ด๊ณผ ๋์ผํ๊ฒย 0~size -1
ย ์ ๋ฒ์๋ฅผ ๊ฐ์ง๋ค.
Vector primary Method
v.front(); // ๋งจ ์ ๊ฐ์ ์ฝ์ด์จ๋ค.
v.back(); // ๋งจ ๋ค ๊ฐ์ ์ฝ์ด์จ๋ค.
v.push_back(); // ๋งจ ๋ค์ ๊ฐ์ ์ง์ด ๋ฃ๋๋ค.
v.pop_back(); // ๋งจ ๋ค๊ฐ์ ์ฝ์ด์ค๊ณ ์ง์ด๋ค.
v.at(index_value); // v[index_value]์ ๋์ผํ๋ค.
// ํ์ง๋ง ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ฐ๋ฉด์ ์๊ธฐ๋ ์ค๋ฅ๋ฅผ ๋ฐฉ์งํ๋๋ฐ ์ ์ฉํ๋ค.
v.size(); // ๋ฒกํฐ์ ํฌ๊ธฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
// ํจ์์ ๋ฐํ๊ฐ์ด unsigned int ์ด๋ฏ๋ก ์ด ๊ฐ์ ์ฐ๋๋ฐ ์์ด์
// unsigned int๋ก ๋ฐ์์ฃผ์ด์ผ ํ๋ค.
Vector Example
#include <iostream>
#include <vector>
using namespace std;
void print(vector<int>& v){
for(unsigned int i = 0; i < v.size(); i++){
cout << v[i] << "\t";
// ๋๋
// cout << v.at(i) << "\t";
}
cout << endl;
}
int main() {
vector<int> v{10, 20, 30};
cout << "๊ฐ ๋ฐ๊พธ๊ธฐ" << endl;
cout << v[0] << ", " << v[1] << endl;
v[0] = 1000;
cout << v[0] << ", " << v[1] << endl;
cout << "push_back" << endl;
v.push_back(2000);
print(v);
cout << "pop_back" << endl;
v.pop_back();
print(v);
return 0;
}
Output
๊ฐ ๋ฐ๊พธ๊ธฐ
10, 20
1000, 20
push_back
1000 20 30 2000
pop_back
1000 20 30
Program ended with exit code: 0
๊ฐ ๋ฃ๊ธฐ/ ์ถ๋ ฅํ๊ธฐ ๋ค๋ฅธ ๋ฐฉ๋ฒ
int main(){
vector<double> v(3);
for (double elem : v)
cin >> elem;
// v ๋ฒกํฐ ๊ฐ์ ๋ณต์ฌํํ ํ๋์ฉ ์ถ๋ ฅํจ
for (double elem : v)
cout << elem << '\t';
cout << endl;
// v ๋ฒกํฐ ๊ฐ์ reference ํด์ ์ถ๋ ฅํจ
for (double& elem : v)
cout << elem << '\t';
cout << endl;
return 0;
}
๋ฒกํฐ๋ฅผ ์ฌ์ฉํ ์์ ์ฐพ๊ธฐ
#include <iostream>
#include <vector>
using namespace std;
bool is_prime(int num){
if (num < 2)
return false;
for(int i = 2; i < num; i++){
if(num % i == 0)
return false;
}
return true;
}
vector<int> primes(int low, int up){
vector<int> vec;
for(int i = low; i<= up; i++){
if(is_prime(i)){
vec.push_back(i);
}
}
return vec;
}
int main(){
int lower = 10, upper = 100;
vector<int> v_prime = primes(lower, upper);
for(int elem : v_prime)
cout << elem << '\t';
cout << endl;
return 0;
}
Output
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Program ended with exit code: 0
๋ฒกํฐ๋ฅผ ์ฌ์ฉํด ๋งคํธ๋ฆญ์ค ๋ง๋ค๊ธฐ
#include <iostream>
#include <vector>
using namespace std;
int main(){
// vector<int>๋ฅผ ์๋ฃํ์ผ๋ก ๊ฐ์ง๋ ๋ฒกํฐ๋ฅผ ์ ์ธ
// ๊ฐ์ฅ ๋ฐ๊นฅ ์ชฝ์ ์๋ ๋ฒกํฐ๋ ํฌ๊ธฐ 2, ๊ทธ๋ฆฌ๊ณ ์์ ์์๋ ํฌ๊ธฐ๊ฐ 3์ธ vector<int>๋ก ์ด๊ธฐํํ๋ค.
vector<vector<int>> v(2, vector<int>(3));
// ์
๋ ฅ
for (vector<int>& row : v)
for(int& elem : row)
cin >> elem;
// ์ถ๋ ฅ
for(vector<int>& row : v){
for(int& elem : row){
cout << elem << '\t';
}
cout << endl;
}
return 0;
}