#include <iostream>
#include <list>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
// 1. 초기화 방법
list<int> l; // 빈 리스트
list<int> l2(5); // 크기5, 기본값 0
list<int> l3(5, 10); // 크기5, 기본값 10
list<int> l4 = { 1,2,3,4 }; // 초기화 리스트
// 2. 데이터 추가
l.push_back(10);
l.push_back(20);
l.push_back(30);
l.push_front(5); // 앞쪽에 추가
// 3. 데이터 접근
cout << "front: " << l.front() << endl; // 5
cout << "back: " << l.back() << endl; // 30
// 4. 데이터 삭제
l.pop_back();
l.pop_front();
// 5. iterator로 순회
cout << "리스트 출력: ";
for (auto it = l.begin(); it != l.end(); it++) {
cout << *it << " ";
}
cout << endl;
// for-each
for (int x : l) {
cout << x << " ";
}
// 6. insert로 중간 삽입
// 인덱스 1에 15를 추가하는 상황
auto it = l.begin();
it++;
l.insert(it, 15); // 1번째 인덱스에 15추가
// 7. erase로 중간 삭제
// 인덱스 2값 삭제
it = l.begin();
it++;
it++;
l.erase(it);
// 8. 상태확인
cout << "size: " << l.size() << endl; // 2
cout << "empty: " << l.empty() << endl; // false (0)
// 9. 전체 삭제
l.clear();
return 0;
}
// 6. insert로 중간 삽입
l.insert(l.begin() + 1, 15); // 1번째 인덱스에 15추가
// 7. erase로 중간 삭제
l.erase(l.begin() + 1);
// 6. insert로 중간 삽입
// 인덱스 1에 15를 추가하는 상황
auto it = l.begin();
it++;
l.insert(it, 15); // 1번째 인덱스에 15추가
// 7. erase로 중간 삭제
// 인덱스 2값 삭제
it = l.begin();
it++;
it++;
l.erase(it);
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Student{
string name;
int score;
}
struct Compare{
bool operator()(const Student& a, const Student& b){
return a.score < b.score;
}
}
int main(){
priority_queue<Student, vector<Student>, Compare> pq;
pq.push({"tom", 95});
pq.push({"goong", 80});
pq.push({"rtan", 99});
return 0;
}
vector<int> v = {1, 2, 3};
do{
for (int x : v) cout << x << " ";
cout << endl;
} while (next_permutation(v.begin(), v.end()));
vector<int> v = {9, 1, 4, 7, 2, 6};
nth_element(v.begin(), v.begin()+2, v.end());
cout << "3번째로 작은 원소 : " << v[2] << endl;
Q1. 배열 {15, 3, 9, 8, 5, 2, 10, 7, 6}에서 짝수만 고려하여 3번째로 큰 수를 출력하세요. (10분)
vector<int> vec = { 15, 3, 9, 8, 5, 2, 10, 7, 6 };
vector<int> vec2;
for (int v : vec) {
if (v % 2 == 0) {
vec2.push_back(v);
}
}
nth_element(vec2.begin(), vec2.begin() + vec2.size() - 3, vec2.end());
cout << vec2[vec2.size() - 3];
Q2. 배열 {1, 2, 3, 4}에서 길이가 3인 순열을 모두 출력하세요. 단, 순열의 첫 번째 원소는 항상 1이어야 합니다.
vector<int> vec = { 1, 2, 3, 4 };
vector<int> vec2 = { 2, 3, 4 };
do {
cout << "1" << vec2[0] << vec2[1] << "\n";
} while (next_permutation(vec2.begin(), vec2.end()));
Q3. 배열 {4, 1, 7, 3, 8, 5}를 5 이상의 값만 내림차순으로 출력하세요.
vector<int> vec = { 4, 1, 7, 3, 8, 5 };
priority_queue<int> pq;
for (int v : vec) {
if (v >= 5) {
pq.push(v);
}
}
while (!pq.empty()) {
cout << pq.top() << "\n";
pq.pop();
}
#include <numeric>
#include <vector>
using namespace std;
vector<int> v = {1, 2, 3, 4};
vector<int> result(4);
partial_sum(v.begin(), v.end(), result.begin());
// result = {1, 3, 6, 10}
vector<int> v = {1, 1, 2, 2, 3, 3, 3, 4};
// 1. 정렬
sort(v.begin(), v.end());
// 2. unique로 중복 제거
auto it = unique(v.begin(), v.end());
// 3. erase로 실제 제거
v.erase(it, v.end());
int product = accumulate(v.begin(), v.end(), 1,[](int a, int b) { return a * b; });
cout << fixed -> 소수점 고정
cout.precision(n) -> n번째까지 출력
string의 find
if (ring.find(keyword) != string::npos) {};
해당하는값이 없으면 string::npos를 반환한다.
알파벳 찾기
int index = S[i] - 'a';
S[i]가 소문자일때 -를 통해서 둘의 아스키코드차이 만큼을 int로 저장가능하다.