๊ตฌ์กฐ์ฒด (struct)
: ๊ตฌ์กฐ์ฒด๋ ์ฌ์ฉ์๊ฐ ์ง์ ์๋ฃํ์ ์ง์ ํจ.
์๋์ ๊ฐ์ด POS๋ผ๋ ์๋ฃํ์ ๊ตฌ์กฐ์ฒด๋ฅผ ํ์ฑํ ์ ์๋ค.
struct POS
{
int y,x;
string name;
};
๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ ์ ๊ทผ
POS a, b;
cin >> a.x >> a.y >> b.x >> b.y;
:๋ฉ์ธ ํจ์์์
์์ ๊ฐ์ด POS๋ผ๋ ์๋ฃํ์ ๋ฉค๋ฒ๋ณ์๋ฅผ ์ ์ธํ๊ณ ,
'.'๋ฅผ ์ฌ์ฉํด์ ์์ ๊ตฌ์กฐ์ฒด์ ํ์ฑํ ๊ฐ๋ณ ๋ฉค๋ฒ์ ์ ๊ทผํ ์ ์๋ค.
๊ตฌ์กฐ์ฒด์ ์์์ push_back
์๋์ ๊ฐ์ด ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํด์ ์์์์ vector์ push_back ํ ์ ์๋ค.
case 01
#include <iostream>
#include <vector>
using namespace std;
struct POS
{
int y, x;
};
int main()
{
vector<POS>v;
for (int i = 0; i < 5; i++)
{
int tmpY,tmpX;
cin >> tmpY>>tmpX;
v.push_back({ tmpY,tmpX });
}
return 0;
}
๋๋ฒ๊น ํ๋ฉด vector ์์ ์์์ y,x์ด ๋ค์ด๊ฐ์์์ ํ์ธํ ์ ์๋ค.
case 02
#include <iostream>
#include <vector>
using namespace std;
struct POS
{
int y, x;
};
int main()
{
vector<POS>v;
for (int i = 0; i < 5; i++)
{
POS tmp;
cin >> tmp.y >> tmp.x;
v.push_back(tmp);
}
return 0;
}
๋๋ฒ๊น ํ๋ฉด ๋ง์ฐฌ๊ฐ์ง๋ก vector ์์ ์์์ y,x์ด ๋ค์ด๊ฐ์์์ ํ์ธํ ์ ์๋ค.
๊ตฌ์กฐ์ฒด ์์
: ์ซ์ 2๊ฐ์ ์ด๋ฆ์ 5์ ์ ๋ ฅ๋ฐ๊ณ , ์ด๋ฆ์ ๊ธธ์ด๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ผ.
(๋ง์ฝ ์ด๋ฆ์ ๊ธธ์ด๊ฐ ๊ฐ์ ๊ฒฝ์ฐ์๋ ์ฒซ๋ฒ์งธ ์ซ์๊ฐ ํฐ ์์๋๋ก,
์ฒซ๋ฒ์งธ ์ซ์๊ฐ ๊ฐ์ ๊ฒฝ์ฐ, ๋๋ฒ์งธ ์ซ์์์ผ๋ก ์ ๋ ฌํ ๊ฒ)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct POS
{
int y, x;
string name;
};
bool compare(POS a, POS b)
{
if (a.name.size() < b.name.size())
{
return true;
}
else if (a.name.size() == b.name.size())
{
if (a.y < b.y)
{
return true;
}
else if (a.y == b.y && a.x < b.x)
{
return true;
}
}
else return false;
return false;
}
int main()
{
vector<POS>v;
for (int i = 0; i < 5; i++)
{
POS tmp;
cin >> tmp.y >> tmp.x >> tmp.name;
v.push_back(tmp);
}
sort(v.begin(), v.end(), compare);
return 0;
}
๋๋ฒ๊น ํด๋ณด๋ฉด v ์์ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌ์กฐ์ฒด๋ก ํธ์๋ฐฑ๋ผ์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ ๋ ฌ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
ํ๋ฆฐ ๋ถ๋ถ์ด๋ ์ด์ํ ๋ถ๋ถ์ด ์์ผ๋ฉด ๋๊ธ๋ก ์ง์ ํด์ฃผ์ธ์!
๊ฐ์ฌํฉ๋๋ค :)
'๐ป > C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] replace() ํจ์, find() ํจ์ / ๋ฌธ์์ด ์ฐพ๊ธฐ (2) | 2024.11.27 |
---|---|
[c++] vector push_back๊ณผ emplace_back (0) | 2024.06.27 |
[c++] ๋ฐ์ฌ๋ฆผ / ์ฌ๋ฆผ / ๋ด๋ฆผ ํจ์ (0) | 2024.04.11 |
[c++] DFS ๊น์ด ์ฐ์ ํ์ , ์ฌ๊ทํจ์ (0) | 2024.04.02 |
[c++] DAT (0) | 2024.03.21 |