Ctrl F5 눌러서 오류가 나면, F5 누르고 무시를 누르면 더욱 자세히 이유를 알 수 있음.
STL에서 반복문 사용할 때 auto로 주로 함.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Node
{
int a, b;
};
int main()
{
vector<Node> a;
a.push_back({7,5});
a.push_back({3,4});
for(auto i = a.begin(); i<a.end(); ++i)
cout << (*i).a << ' ' << i->b << '\n';
return 0;
}
vector는 = 으로 그대로 복사가 가능하다.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Node
{
int a, b;
};
int main()
{
vector<int> a(5, 6);
vector<int> b;
b = a;
for(auto i=b.begin(); i<b.end(); ++i)
cout << *i << ' ' ;
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Node
{
int a, b;
};
vector<int> bts(vector<int> arr)
{
arr.push_back(7);
arr.push_back(9);
return arr;
}
int main()
{
vector<int> a = {1, 3, 5};
vector<int> temp = bts(a);
for(auto i = temp.begin(); i < temp.end(); ++i)
cout << *i << '\n';
return 0;
}
레퍼런스는 별명이라 생각하는 게 편하다.
#include <iostream>
#include <vector>
using namespace std;
void swap(int &a, int &b)
{
int temp = b;
b = a;
a = temp;
}
void swapVect(vector<int> & v)
{
v[0] = 100;
}
int main()
{
int a, b;
cin >> a >> b;
swap(a, b);
cout << a << ' ' << b << '\n';
vector<int> t(10, 10);
swapVect(t);
cout << t[0];
return 0;
}
vecotr안에 vector 넣기
#include <iostream>
#include <vector>
using namespace std;
void swap(int &a, int &b)
{
int temp = b;
b = a;
a = temp;
}
void swapVect(vector<int> & v)
{
v[0] = 100;
}
int main()
{
vector<vector<int>> arr;
arr.push_back({1, 2, 3});
arr.push_back({5, 4});
arr.push_back({1, 2, 3, 6});
arr.push_back({5});
cout << arr[0][arr[0].size()-1] << '\n';
cout << arr[1][arr[1].size()-1] << '\n';
cout << arr[2][arr[2].size()-1] << '\n';
cout << arr[3][arr[3].size()-1] << '\n';
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
void swap(int &a, int &b)
{
int temp = b;
b = a;
a = temp;
}
void swapVect(vector<int> & v)
{
v[0] = 100;
}
int main()
{
vector<vector<int>> arr;
arr.push_back({1, 2, 3});
arr.push_back({5, 4});
arr.push_back({1, 2, 3, 6});
arr.push_back({5});
for(auto i = arr.begin(); i<arr.end(); ++i)
{
for(auto j = (*i).begin(); j < (*i).end(); ++j)
cout << *j << ' ' ;
cout << '\n';
}
return 0;
}
그래프에서 노드 값과 노드 번호를 헷갈리면 안된다.
자신이 지목한 노드 모두 출력
#include <iostream>
using namespace std;
int main()
{
int map[5][5] = {
0, 1, 0, 0, 0,
0, 0, 0, 1, 0,
0, 1, 0, 1, 1,
1, 1, 1, 0, 0,
0, 0, 0, 0, 0
};
int n;
cin >> n;
for(int i=0; i<5; i++)
{
if(map[n][i] == 1)
cout << i << ' ';
}
return 0;
}
가장 많이 지목받은 노드 출력
#include <iostream>
using namespace std;
int main()
{
int map[5][5] = {
0, 1, 0, 0, 0,
0, 0, 0, 1, 0,
0, 1, 0, 1, 1,
1, 1, 1, 0, 0,
0, 0, 0, 0, 0
};
int maxIndex = 0, maxCnt = -1e9;
for(int i=0; i<5; i++)
{
int cnt = 0;
for(int j=0; j<5; j++)
{
if(map[j][i] == 1)
cnt++;
}
if(maxCnt < cnt)
{
maxCnt = cnt;
maxIndex = i;
}
}
cout << maxIndex;
return 0;
}
'C++ > 공부 정리' 카테고리의 다른 글
간단한 그래프에서 싸이클 체크, queue STL (0) | 2022.04.27 |
---|---|
Vector로 그래프 관계 표현 (0) | 2022.04.27 |
string 클래스 find, length, substr (0) | 2022.04.26 |
재귀, 배열 원상복귀, (0) | 2022.04.22 |
순열, 조합 (0) | 2022.04.21 |