文字列から文字を数え、数とcout
結果で並べ替える必要があります。この目的のために、私はvector
andを使用しようとしています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;
}
このコードには、対処方法がわからない 2 つの部分があります。
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?
このコードで、他の間違いや潜在的な問題を確認できるかもしれません。