참고자료
배열 : https://learn.microsoft.com/ko-kr/cpp/cpp/arrays-cpp?view=msvc-170
constexpr : https://learn.microsoft.com/ko-kr/cpp/cpp/constexpr-cpp?view=msvc-170
constexpr size_t size = 1000;
double numbers[size]{ 0 };
numbers[0] = 1;
for (size_t i = 1; i < size; ++i)
{
numbers[i] = numbers[i - 1] * 1.1;
}
for (const auto n : numbers)
{
cout << n << " "; //1 1.1 1.21 1.331 ...
}
size_t size2 = 100;
int* numbers2 = new int[size2] {0};
numbers2[0] = 1;
for (size_t i = 1; i < size2; ++i)
{
numbers2[i] = 100;
}
int* p = numbers2;
for (size_t i = 0; i < size2; ++i)
{
cout << *(p++) << " "; //100 100 100 100 ...
}
#include <array>
필요array<int, 3> a1;
a1[0] = 100;
a1[1] = 200;
a1[2] = 300;
int Sum(const int *num, const size_t len)
{
int sum = 0;
for (size_t i = 0; i < len; ++i)
{
sum += num[i];
}
return sum;
}
constexpr
변수constexpr float x = 42.0f;
-> okconstexpr int i;
-> error (초기화 불가)constexpr int k = i+1
size_t
typedef unsigned __int64 size_t;
std::vector<int> name1;
std::vector<int> name2(name1);
: 복사 생성자
std::vector<int> name3(10);
: 0으로 값 초기화
reserve()
써서 속도 빠르게 하는 것 좋다.push_back()
: 삽입
pop_back()
: 삭제
reserve()
: 공간 할당 하기
capacity()
: 할당된 요소의 공간 수
size()
: 실제 들어있는 요소의 수
insert(iterator, value)
: value를 특정 위치에 삽입
erase(iterator)
: 특정 위치의 요소 삭제
assign(num, value)
: vector를 value로 num 만큼 할당하기
swap(vector)
: vector 교환하기
resize()
:
clear()
: 모든 요소 제거, 크기 0이지만, 용량은 그대로
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec1;
vector<int> vec2;
vec1.reserve(10);
vec1.push_back(10);
vec1.push_back(20);
vec1.push_back(30);
vec2.assign(11, 100);
//순차적으로 출력
for (vector<int>::const_iterator it = vec1.begin(); it != vec1.end(); ++it)
{
cout << *it << endl; //10 20 30
}
cout << "\n";
//역으로 출력
for (vector<int>::reverse_iterator it = vec1.rbegin(); it != vec1.rend(); ++it)
{
cout << *it << endl; //30 20 10
}
cout << "\n";
//vector 교환
vec1.swap(vec2);
for (vector<int>::const_iterator it = vec1.begin(); it != vec1.end(); ++it)
{
cout << *it << endl; //100 100 100 100 100 100 100 100 100 100 100
}
//vector 지우기
vec1.clear();
cout << vec1.size() << " " << vec1.capacity() << endl; //0 11
cout << "\n";
//특정 위치 삽입
vector<int>::iterator iter = vec2.begin();
vec2.insert(iter + 1, 100);
for (vector<int>::const_iterator it = vec2.begin(); it != vec2.end(); ++it)
{
cout << *it << endl; //10 100 20 30
}
cout << "\n";
}
push_front()
push_back()
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> scores;
scores.push_back(10);
scores.push_back(20);
scores.push_front(30);
list<int>::const_iterator it = scores.begin();
for (; it != scores.end(); ++it)
{
cout << *it << endl; //30 10 20
}
}
insert()
: key와 value값 삽입
-> 이미 있는 key 값을 insert 하면 false 반환한다.
[]
operator : 참조 반환
-> 없는 키를 쓰면 새 요소 삽입, 있는 키값이면 덮어쓰기
find()
: key값을 찾으면 해당 key 가지는 iterator 반환
swap()
: 두 map의 key와 value 바꾸기
clear()
: map 비우기
erase()
: 한 요소 제거
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<string, int> Scores;
Scores.insert(pair<string, int>("A", 100));
Scores.insert(pair<string, int>("B", 80));
Scores.insert(pair<string, int>("C", 50));
Scores.insert(pair<string, int>("C", 10)); //false
Scores["D"] = 30;
cout << Scores["A"] << endl; //100
cout << Scores["C"] << endl; //50
cout << Scores["D"] << endl; //30
//복사 생성자
map<string, int> copyScores(Scores);
for (map<string, int>::iterator it = copyScores.begin(); it != copyScores.end(); ++it)
{
cout << it->first << " " << it->second << endl;
}
map<string, int>::iterator it = Scores.find("A");
if (it != Scores.end()) // 찾았을 때 -> 못찾으면 end iterator 반환
{
it->second = 99;
}
cout << Scores["A"] << endl; //99
Scores.swap(copyScores);
cout << copyScores["A"] << " " << Scores["A"] << endl; //99 100
}
class Player
{
public:
Player(int s,int i, string n)
:Score(s)
,ID(i)
,Name(n)
{
}
bool operator<(const Player& other)const //operator 구현
{
return this->getScores() < other.getScores();
}
int getScores() const
{
return this->Score;
}
string getName() const
{
return this->Name;
}
private:
int Score;
int ID;
string Name;
};
class PlayerCompare //compare 함수 구현
{
public:
bool operator() (const Player& left, const Player& right) const
{
return (left.getScores() < right.getScores());
}
};
int main()
{
Player p1{ 100, 1, "AAA" };
Player p2{ 50, 2, "BBB" };
//operator 연산자
map<Player, string> test;
test.insert(pair<Player, string>(p1, "aaa"));
test.insert(pair<Player, string>(p2, "bbb"));
cout << test[p1] << " " << test[p2] << endl; // aaa bbb
//compare 함수
map<Player, string, PlayerCompare> test2;
test2.insert(pair<Player, string>(p1, "aaa"));
test2.insert(pair<Player, string>(p2, "bbb"));
for (map<Player, string>::iterator it = test2.begin(); it != test2.end(); ++it)
{
cout << it->first.getName() << ", " << it->second << endl; //BBB bbb AAA aaa
}
}