1
- 벡터 크기 설정
#include <vector>
#include <iostream>
using namespace std;
int main() {
int n;
cout << "정수의 개수: ";
cin >> n;
vector<int> v(n);
for (auto& e : v) {
cout << "정수를 입력하시오: ";
cin >> e;
}
int max = v[0];
int min = v[0];
for (auto& e : v) {
if (e > max)
max = e;
if (e < min)
min = e;
}
cout << "최대값: " << max << endl;
cout << "최소값: " << min << endl;
return 0;
}
2
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cout << "문자열의 개수: ";
cin >> n;
vector<string> v(n); // 벡터 크기 지정
for (auto& e : v) {
cout << "문자열을 입력하시오: ";
cin >> e;
}
for (auto& e : v) {
cout << e << endl;
}
return 0;
}
3
#include <iostream>
#include <vector>
using namespace std;
class Rect {
public:
int w, h;
Rect() {
w = 0;
h = 0;
}
Rect(int _w, int _h) {
w = _w;
h = _h;
}
int area() {
return w * h;
}
void print() {
cout << "(" << w << ", " << h << ")" << endl;
}
};
int main() {
int n;
cout << "사각형의 개수: ";
cin >> n;
vector<Rect> myRect(n);
for (auto& e : myRect) {
cout << "사각형의 폭: ";
cin >> e.w;
cout << "사각형의 높이: ";
cin >> e.h;
}
for (auto& e : myRect) {
if (e.area() > 100)
e.print();
}
return 0;
}
4
/* 객체 배열 이용하기 */
#include <iostream>
#include <string>
using namespace std;
class SMS {
public:
string sender;
string receiver;
string text;
SMS() {
sender = "0";
receiver = "0";
text = "0";
}
SMS(string s, string r, string t) {
sender = s;
receiver = r;
text = t;
}
void print() {
cout << "발신자: " << sender << " ";
cout << "수신자: " << receiver << " ";
cout << "내용: " << text << endl;
}
};
int main() {
SMS mySMS[2];
mySMS[0] = { "010-1234-5678", "010-1208-3719", "C++ 공부는 잘 돼가나요?" };
mySMS[1] = { "010-1223-1423", "010-1928-1283", "네 5장까지는 문제 없네요." };
for (auto& e : mySMS)
e.print();
return 0;
}
/* 벡터 이용하기 */
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class SMS {
public:
string sender;
string receiver;
string text;
SMS() {
sender = "0";
receiver = "0";
text = "0";
}
SMS(string s, string r, string t) {
sender = s;
receiver = r;
text = t;
}
void print() {
cout << "발신자: " << sender << " ";
cout << "수신자: " << receiver << " ";
cout << "내용: " << text << endl;
}
};
int main() {
vector<SMS> mySMS(2);
mySMS[0] = { "010-1234-5678", "010-1208-3719", "C++ 공부는 잘 돼가나요?" };
mySMS[1] = { "010-1223-1423", "010-1928-1283", "네 5장까지는 문제 없네요." };
for (auto& e : mySMS) {
e.print();
}
return 0;
}
5: 정렬 알고리즘
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
string name;
double marks;
Student() {
name = "0";
marks = 0;
}
Student(string n, double m) {
name = n;
marks = m;
}
// get_marks()
double get_marks() {
return marks;
}
void print() {
cout << "이름 : " << name << endl;
cout << "학점 : " << marks << endl;
}
};
// compare
bool compare(Student& a, Student& b) {
return a.marks > b.marks;
}
int main() {
vector<Student> myStudent(3);
for (auto& e : myStudent) {
cout << "이름 : ";
cin >> e.name;
cout << "학점 : ";
cin >> e.marks;
}
// 정렬 알고리즘
sort(myStudent.begin(), myStudent.end(), compare);
for (auto& e : myStudent) {
e.print();
}
return 0;
}
6
#include <iostream>
#include <vector>
using namespace std;
class Contact {
public:
string name;
string tel;
};
int main() {
vector<Contact> myContact(3);
for (auto& e : myContact) {
cout << "이름을 입력하시오: ";
cin >> e.name;
cout << "전화번호를 입력하시오: ";
cin >> e.tel;
}
string input;
cout << "탐색하고 싶은 이름을 입력하시오: ";
cin >> input;
for (auto& e : myContact) {
if (input == e.name)
cout << "전화번호: " << e.tel << endl;
}
return 0;
}
'Programming > C++' 카테고리의 다른 글
[어서와 C++는 처음이지!] CH8 Exercise (1) | 2024.02.12 |
---|---|
[C++] 8. 포인터와 동적객체 생성 (1) | 2024.02.12 |
[C++] 6. 객체 배열과 벡터 (1) | 2024.01.30 |
[어서와 C++는 처음이지!] CH5 Exercise (0) | 2024.01.29 |
[C++] 5. 생성자와 접근제어 (0) | 2024.01.29 |