vector에 대한 학습
벡터에서 원소를 찾아 해당 인덱스를 반환하는 소스 코드 구현해보자
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<int> v;
vector<int>::iterator it;
v.push_back(5);
v.push_back(9);
it = find(v.begin(), v.end(), 9);
if (it != v.end()) {
cout << it - v.begin();
}
return 0;
}
// 결과값으로 1이 출력된다.
이와 유사하게 count를 사용해서 원소 중에서 value의 개수를 찾을 수 있다.
count(v.begin(), v.end(), 5); // 5가 포함되어 있는 원소의 개수를 구한다.
int even = count_if(v.begin(), v.end(),[](int x){
return x%2 == 0;
}); // 짝수의 개수를 구한다. (조건에 해당하는 것의 개수를 구한다.)
그렇다면 최소값 최대값을 바로 구하는 라이브러리 역시 존재하지 않을까?
검색해보니 algorithm 라이브러리에 존재하였다.
for문을 돌리지 않고 algorithm 라이브러리를 이용하여 최소값 최대값을 바로 구하는 소스 코드를 작성해보자.
max_element와 min_element 이용하여 최대값, 최소값의 값과 인덱스를 구하기
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<int> v;
v.push_back(5);
v.push_back(9);
v.push_back(6);
v.push_back(4);
int max = *max_element(v.begin(), v.end());
cout << "최대값: " << max << "\n";
int maxIndex = max_element(v.begin(), v.end()) - v.begin();
cout << "최대값 인덱스: " << maxIndex << "\n";
int min = *min_element(v.begin(), v.end());
cout << "최소값: " << min << "\n";
int minIndex = min_element(v.begin(), v.end()) - v.begin();
cout << "최소값 인덱스: " << minIndex << "\n";
return 0;
}
문자열에 대한 학습
string "apple go od"을 공백을 기준으로 잘라서 벡터에 넣는 소스코드를 구현해보자
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<string> words;
string fruit = "apple go od";
stringstream s(fruit);
string word;
while(getline(s, word, ' ')) {
words.push_back(word);
}
cout << words[1];
return 0;
}
그렇다면 string "apple"을 벡터에 한 글자씩 잘라서 넣을 수 있을까?
벡터에 넣고 나서 벡터에 문자들이 잘 들어갔는지 확인해보자
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<string> words;
string fruit = "apple";
string n;
for(int i=0; i<fruit.length(); i++) {
n = fruit.at(i);
words.push_back(n);
}
for(int i=0; i<words.end()-words.begin(); i++) {
cout << words[i] << "\n";
}
return 0;
}
//결과
//a
//p
//p
//l
//e
검색해도 원하는 정보가 없어서 직접 벡터에 한글자씩 잘라넣는 코드를 작성해보았다.
굳이 벡터에 한글자씩 잘라 넣는 코드를 작성한 이후는
이제 이 코드를 베이스로 문자열 카운트나 찾기 문제에서 count나 find를 활용할 것이기 때문이다.
10818번 문제 : 최소, 최대
문제 :
소스 코드 ( vector와 sort를 이용 ) :
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector <int> v;
int n, a;
cin >> n;
for(int i=1; i<=n; i++) {
cin >> a;
v.push_back(a);
}
sort(v.begin(), v.end());
cout << v.front() << " " << v.back();
return 0;
}
소스 코드 ( vector와 sort 미사용 ) :
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, a;
int max = - 1000001, min = 1000001;
cin >> n;
for(int i=1; i<=n; i++) {
cin >> a;
if (a > max) max = a;
if (a < min) min = a;
}
cout << min << " " << max;
return 0;
}
2562번 문제 : 최댓값
문제 :
도입부에서 미리 공부했던 algorithm 라이브러리를 이용하면 쉽게 구현할 수 있는 문제다.
소스 코드 :
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<int> v;
int n;
for(int i=1; i<=9; i++) {
cin >> n;
v.push_back(n);
}
int max = *max_element(v.begin(), v.end());
int maxIndex = max_element(v.begin(), v.end()) - v.begin() + 1;
cout << max << "\n" << maxIndex;
return 0;
}
maxIndex에 + 1을 해야한다는 것을 주의하자.
구해야하는 것은 인덱스가 아니라 몇 번째 수인지이다.
문제를 정확히 읽고 코드를 작성해야한다.
2577번 문제 : 숫자의 개수
문제 :
소스 코드 :
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<string> v;
int n;
int u = 1;
string s;
for(int i=1; i<=3; i++) {
cin >> n;
u *= n;
}
s = to_string(u);
string ss;
for(int i=0; i<s.length(); i++) {
ss = s.at(i);
v.push_back(ss);
}
for(int i=0; i<=9; i++) {
cout << count(v.begin(), v.end(), to_string(i)) << "\n";
}
return 0;
}
A, B, C 를 곱해주고 그 결과를 벡터에 한 글자씩 잘라서 저장한 다음 count를 이용하여
0부터 9까지의 숫자가 각각 몇 번 쓰였는지 차례대로 출력하였다.
문제 출처 :
https://www.acmicpc.net/problem/10818
10818번: 최소, 최대
첫째 줄에 정수의 개수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 N개의 정수를 공백으로 구분해서 주어진다. 모든 정수는 -1,000,000보다 크거나 같고, 1,000,000보다 작거나 같은 정수이다.
www.acmicpc.net
https://www.acmicpc.net/problem/2562
2562번: 최댓값
9개의 서로 다른 자연수가 주어질 때, 이들 중 최댓값을 찾고 그 최댓값이 몇 번째 수인지를 구하는 프로그램을 작성하시오. 예를 들어, 서로 다른 9개의 자연수 3, 29, 38, 12, 57, 74, 40, 85, 61 이 주어
www.acmicpc.net
https://www.acmicpc.net/problem/2577
2577번: 숫자의 개수
첫째 줄에 A, 둘째 줄에 B, 셋째 줄에 C가 주어진다. A, B, C는 모두 100보다 크거나 같고, 1,000보다 작은 자연수이다.
www.acmicpc.net
'C++ 코딩테스트 공부 (중단) > c++ 백준 문제풀이' 카테고리의 다른 글
#5 함수 (0) | 2022.03.27 |
---|---|
#4 1차원 배열(2) (0) | 2022.03.26 |
#3 반복문 (0) | 2022.03.23 |
#2 조건문 (0) | 2022.03.23 |
#1 입출력과 사칙연산 (0) | 2022.03.23 |