강의/알고리즘
201007 C++언어 20강 벡터
- 오트 -
2020. 10. 7. 13:19
www.youtube.com/watch?v=zE4pd65qMx0
20-1 : 객체 배열
코드 작성
#include <iostream>
#include <string>
using namespace std;
class Book
{
private:
string name;
int no;
public:
Book() { cout << " --- constructor --- " << endl; } // default 생성자
Book(string s, int n)
{
cout << " --- constructor --- " << endl;
name = s;
no = n;
} // 매개변수 받는 생성자
//~Book() { cout << " --- Destructor --- " << endl; } // 소멸자
void setName(string s) { name = s; } // setter
void setNo(int n) { no = n; }
string getName() { return name; } // getter
int getNo() { return no; }
};
int main(void)
{
Book books1[5]; // Book() { cout << " --- constructor --- " << endl; } 호출
Book books2[5] = { // 생성함과 동시에 초기화, Book(string s, int n) 호출
Book("C", 1),
Book("C++", 2),
Book("JAVA", 3)
};
Book books3[] = { // 크기 지정하지 않은 경우
Book("C", 1),
Book("C++", 2),
Book("JAVA", 3)
};
for(int i=0; i<3; i++) // 인덱스로 접근하는 법
{
cout << "books3[ " << i << "].getName() --- > " << books3[i].getName() << endl;
cout << "books3[ " << i << "].getNo() --- > " << books3[i].getNo() << endl;
}
for(Book book : books3) // for loop 문 이용(좀 더 간단하게)
{
cout << "book.getName() ---> " << book.getName() << endl;
cout << "book.getName() ---> " << book.getNo() << endl;
}
return 0;
}
* 인덱스로 접근하는 방법보다 for loop문을 이용하는 것이 간단하다
객체 배열은 배열의 크기 변경 불가 -> 벡터를 이용하면 동적으로 메모리를 확장, 축소하면서 배열의 크기 자유롭게 조절 가능
20-2 : 벡터(vector)
코드 작성
#include <iostream>
#include <string>
#include <vector> // 벡터 사용하기 위해 작성
using namespace std;
// 벡터(vector)
class BankAccount
{
private:
string a_name;
int a_no;
public:
BankAccount() { cout << " --- constructor --- " << endl; }
BankAccount(string s, int n)
{
cout << " --- constructor --- " << endl;
a_name = s;
a_no = n;
}
void setName(string s) { a_name = s; }
void setNo(int n) { a_no = n; }
string getName() { return a_name; }
int getNo() { return a_no; }
};
int main(void)
{
// 벡터(vector)
vector<BankAccount> bankAccounts(5); // BankAccount 타입의 크기는 5인 vector 사용(선언)
cout << endl;
cout << " bankAccounts.size() ---> " << bankAccounts.size() << endl; // 백터의 크기 반환
cout << endl;
bankAccounts[0].setName("박찬호");
bankAccounts[0].setNo(1);
bankAccounts[1].setName("이승엽");
bankAccounts[1].setNo(2);
bankAccounts[2].setName("류현진");
bankAccounts[2].setNo(3);
bankAccounts[3].setName("추신수");
bankAccounts[3].setNo(4);
bankAccounts[4].setName("박병호");
bankAccounts[4].setNo(5);
for (BankAccount ac : bankAccounts) // for loop문 이용
{
cout << "ac.getName() ---> " << ac.getName() << endl;
cout << "ac.getNo() ---> " << ac.getNo() << endl;
}
cout << endl;
BankAccount ac;
bankAccounts.push_back(ac);
cout << " bankAccounts.size() ---> " << bankAccounts.size() << endl;
cout << endl;
bankAccounts.pop_back();
cout << " bankAccounts.size() ---> " << bankAccounts.size() << endl;
cout << endl;
BankAccount tempF = bankAccounts.front(); // 배열의 첫번째 원소
cout << "tempF.getName() ---> " << tempF.getName() << endl;
cout << "tempF.getNo() ---> " << tempF.getNo() << endl;
cout << endl;
BankAccount tempB = bankAccounts.back(); // 배열의 마지막 원소
cout << "tempB.getName() ---> " << tempB.getName() << endl;
cout << "tempB.getNo() ---> " << tempB.getNo() << endl;
cout << endl;
vector<BankAccount>::iterator iter; // 반복자
//iter = bankAccounts.begin(); // 처음
iter = bankAccounts.end(); // 마지막 + 1
//iter += 1;
iter -= 1; // 마지막 요소 가리킴
bankAccounts.erase(iter); // iter가 가리키는 요소 삭제
for (BankAccount ac : bankAccounts)
{
cout << "ac.getName() ---> " << ac.getName() << endl;
cout << "ac.getNo() ---> " << ac.getNo() << endl;
}
cout << endl;
iter = bankAccounts.begin(); // 박찬호에 iter가 있음
bankAccounts.erase(iter+1, iter+3);
// 구간 설정(iter+1 : 이승엽, iter+3 : 추신수), 이승엽과 류현진이 사라지게 됨
// iter+1 은 포함, iter+3은 포함하지 않음
for(BankAccount ac : bankAccounts)
{
cout << "ac.getName() ---> " << ac.getName() << endl;
cout << "ac.getNo() ---> " << ac.getNo() << endl;
}
cout << endl;
return 0;
}
* bankAccounts.erase(iter+1, iter+3); : iter+1에서 iter+2까지의 요소를 삭제 (iter+3은 포함하지 않음)
+ 실제 실습 사진