#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec;
vec.push_back(0);
vec.push_back(2);
vector<int>::iterator it = vec.begin();
cout << *it << endl; //0
cout << *(it + 1) << endl; //2
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec;
for (int i = 0; i < 5;i++) {
vec.push_back(i);
}
//반복자를 이용해 순회
for (vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
cout << *it << endl;
}
return 0;
}
vector<int>::const_iterator const_it = vec.cbegin();
*const_it = 100; // 값 변경 -> 에러
++const_it // 가르키는 대상 변경 -> 가능
for(vector<int>::reverse_iterator it = vec.rbegin(); it != vec.rand(); it++)
#include <iostream>
#include <array>
using namespace std;
int main() {
//크기가 5인 array생성
array<int, 5> myArray;
myArray = { 1, 2, 3, 4, 5 };
cout << "배열 출력: ";
for (const int& element : myArray) {
cout << element << " "; // 1, 2, 3, 4, 5
}
cout << endl;
cout << "배열 크기: " << myArray.size() << endl; // 5
cout << "첫번째 원소: " << myArray[0] << endl; // 1
myArray[1] = 10;
cout << "변경된 배열: ";
for (int i = 0; i < 5; i++) {
cout << myArray[i] << " "; // 1 10 3, 4, 5
}
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec;
vec.push_back(0);
vec.push_back(1);
vec.push_back(2);
for (int i = 0; i < 3; i++) {
cout << "vec 의 " << i + 1 << "번째 원소 : " << vec[i] << endl;
}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
void print_vector_all(vector<T>& vec) {
cout << "벡터 내 원소 개수 : " << vec.size() << endl;
for (typename vector<T>::iterator it = vec.begin(); it != vec.end(); it++) {
cout << *it << " ";
}
cout << endl << "----------" << endl;
}
int main() {
vector<int> vec;
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
vec.push_back(40);
cout << "원본" << endl;
print_vector_all(vec);
vec.insert(vec.begin() + 3, 25); // vector[3] 앞에 25 추가
cout << "insert 결과 출력" << endl;
print_vector_all(vec);
vec.erase(vec.begin() + 3); // vec[3] 제거
cout << "erase 결과 출력" << endl;
print_vector_all(vec);
return 0;
}
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> myList;
//뒤에 값 추가
myList.push_back(2);
myList.push_back(3);
myList.push_back(4);
//앞에 값 추가
myList.push_front(1);
myList.push_front(0);
cout << "리스트 출력: ";
for (const int& value : myList) {
cout << value << " ";
}
cout << endl;
myList.pop_front();//첫번째 원소 제거
myList.pop_back();//마지막 원소 제거
cout << "삭제 후 리스트 출력: ";
for (const int& value : myList) {
cout << value << " ";
}
cout << endl;
cout << "리스트 크기: " << myList.size() << endl;
cout << "리스트가 비었는가? " << (myList.empty() ? "예" : "아니요") << endl;
return 0;
}
벡터는 원소에 자주 접근하고 수정해야 할 때
리스트는 삽입과 삭제가 빈번할 때
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> myDeque;
//덱 뒤에 값 추가
myDeque.push_back(2);
myDeque.push_back(3);
myDeque.push_back(4);
//덱 앞에 값 추가
myDeque.push_front(1);
myDeque.push_front(0);
cout << "deque 출력: ";
for (const int& value : myDeque) {
cout << value << " ";
}
cout << endl;
myDeque.pop_front(); // 첫번째 덱 원소 제거
myDeque.pop_back(); // 마지막 덱 원소 제거
cout << "삭제 후 deque 출력: ";
for (const int& value : myDeque) {
cout << value << " ";
}
cout << endl;
cout << "deque 크기 : " << myDeque.size() << endl;
cout << "deque이 비어있는가? " << (myDeque.empty() ? "예" : "아니요") << endl;
cout << "deque 첫번째 원소: " << myDeque.front() << endl;
cout << "deque 마지막 원소: " << myDeque.back() << endl;
return 0;
}
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> mySet;
mySet.insert(5);
mySet.insert(2);
mySet.insert(8);
if (mySet.find(5) != mySet.end()) {
cout << "5는 set에 저장되어 있음" << endl;
}
for (auto it = mySet.begin(); it != mySet.end(); ++it) {
cout << *it << " ";
}
cout << endl;
int size = mySet.size();
cout << "set크기 : " << size << endl;
return 0;
}
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> myMultiset;
myMultiset.insert(5);
myMultiset.insert(2);
myMultiset.insert(5);
int count = myMultiset.count(5);
cout << "저장되어있는 5의 개수" << count << endl; // 2
for (auto it = myMultiset.begin(); it != myMultiset.end(); ++it) {
cout << *it << " "; // 2 5 5
}
cout << endl;
int size = myMultiset.size();
cout << "multiset크기: " << size << endl; // 3
return 0;
}
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
map<string, int> scores;
//키-값 쌍 삽입
scores.insert(make_pair("Bob", 85));
scores.insert(make_pair("Jane", 90));
scores.insert(make_pair("Tom", 70));
cout << "map 크기" << scores.size() << endl; // 3
//특정 키에 해당하는 값 검색
auto it = scores.find("Bob");
if (it != scores.end()) {
cout << "Bob의 점수 검색 결과 : " << it->second << endl; // 85
}
else {
cout << "Bob의 점수는 저장되어있지 않음" << endl;
}
cout << endl;
//특정 키에 해당하는 키-값 제거
scores.erase("Bob");
cout << "Bob 정보 제거 후, map크기 : " << scores.size() << endl << endl; // 2
cout << "---map 모든 원소 출력---" << endl;
for (const auto& pair : scores) {
cout << pair.first << ": " << pair.second << endl; // Jane: 90 / Tom: 70
}
return 0;
}
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
multimap<string, int> scores;
//키-값 쌍 삽입
scores.insert(make_pair("Bob", 85));
scores.insert(make_pair("Jane", 90));
scores.insert(make_pair("Tom", 70));
scores.insert(make_pair("Bob", 100));
cout << "map 크기" << scores.size() << endl; // 4
//특정 키에 해당하는 원소의 개수
int count = scores.count("Bob");
cout << "저장되어있는 Bob 점수의 개수 : " << count << endl; // 2
//특정 키를 가진 원소의 범위 구하기
auto range = scores.equal_range("Bob");
if (range.first != scores.end()) {
cout << "Bob의 모든 점수 : ";
for (auto it = range.first; it != range.second; ++it) {
cout << it->second << " "; // 85 100
}
cout << endl;
}
else {
cout << "Bob의 점수는 저장되어있지 않음" << endl;
}
cout << endl;
scores.erase("Bob");
cout << "Bob 정보 제거 후, multimap 크기: " << scores.size() << endl; // 2
cout << "---map 모든 원소 출력---" << endl;
for (const auto& pair : scores) {
cout << pair.first << ": " << pair.second << endl; // Jane: 90 / Tom: 70
}
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> myStack;
myStack.push(1);
myStack.push(2);
myStack.push(3);
cout << "맨 위 원소: " << myStack.top() << endl; // 3
myStack.pop();
cout << "맨 위 원소 제거 후, 새로운 맨 위 원소: " << myStack.top() << endl; // 2
cout << "스택 크기: " << myStack.size() << endl; // 2
if (myStack.empty()) {
cout << "스택이 비어있습니다." << endl;
}
else {
cout << "스택은 비어있지 않습니다." << endl;
}
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> myQueue;
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
cout << "큐의 맨 앞: " << myQueue.front() << endl;//1
cout << "큐의 맨 뒤: " << myQueue.back() << endl;//3
myQueue.pop(); // 꺼내기
cout << "pop 후 맨앞: " << myQueue.front() << endl;//2
cout << "pop 후 맨뒤: " << myQueue.back() << endl;//3
cout << "큐가 비어있나요? " << (myQueue.empty() ? "Y" : "N") << endl;//N
cout << "큐의 크기: " << myQueue.size() << endl;//2
return 0;
}