-
[C++] struct형의 vector를 함수 인자로 넘기기공부/C, C++ 2021. 10. 8. 02:31
1. struct cand를 선언
2. cand형을 가지는 vector을 선언 (vector <cand> candidates;)
3. candidates를 정렬하기 위한 기준을 세워주는 compare 함수 정의
(bool cmp(vector <cand> &vec1, vector <cand> &vec2)
문제) cmp 함수에서 vec1의 멤버변수들이 참조가 되지 않음
해결)
cmp 함수의 인자로 cand &vec1, cand &vec2를 넘겨주었더니 멤버변수들이 잘 참조되는 것을 확인함
구조체형 vector를 함수의 인자로 넘겨받고 싶을 때:
구조체 tag명 &vector 변수명
'공부 > C, C++' 카테고리의 다른 글
[C++] 공백으로 문자열 자르기, for each문 (나중에 포스트 하기) (0) 2022.03.18 [C++] string 공백 포함 입력, 입력 버퍼 비우기 (0) 2022.01.31 [C++] sort( )를 이용한 구조체 정렬 (0) 2022.01.12 [C++] vector에 원소 추가하기 (0) 2021.10.10 [C] 배열의 주소 (0) 2021.08.29