문자열에서 문자를 계산해야하고 개수에 따라 정렬해야하며 cout
결과가 필요합니다. 이를 위해 vector
및 struct
을 사용하려고합니다. 여기에 내 코드의 일부이지만, 내가 뭔가 구현하는 방법을 모르기 때문에 그것은 작동하지 않습니다 :벡터 및 구조체를 사용하는 방법?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct int_pair{
int key;
int value;
};
bool sort_by_value(int_pair left, int_pair right){
return left.value < right.value;
}
int main() {
string characters = "aasa asdfs dfh f ukjyhkh k wse f sdf sdfsdf";
vector<int_pair> most_frequent;
for (string::size_type i = 0; i <= characters.length(); i++) {
int int_char = (int)characters[i];
most_frequent[int_char]++; <-- I want to do something like this, but it's not working
}
sort(most_frequent.begin(), most_frequent.end(), sort_by_value);
for (vector<int_pair>::iterator it = most_frequent.begin(); it != most_frequent.end(); ++it) <-- is this call correct?
cout << " " << it->key << ":" << it->value << endl;
return 0;
}
을
most_frequent[int_char]++; <-- I want to do something like this, but it's not working
및
for (vector<int_pair>::iterator it = most_frequent.begin(); it != most_frequent.end(); ++it) <-- is this call correct?
은 아마 당신은이 코드에서 다른 실수와 잠재적 인 문제를 볼 수 있습니다.
벡터를 사용하는 연습입니까? 지도 또는 해시 맵이 작은 문자열에 더 잘 맞기 때문입니다. –
@PeteKirkham 아니, 그렇지 않아. 내 해결책이야. –