我生成一个2D矢量,想要检查特定单词在矢量的一行中出现的频率。 这是我的矢量示例:
Monkey Monkey Banana
Banana Monkey Monkey
Banana Banana Banana
我想要结果:
First row: Monkey: 2 of 3
Second row: Monkey: 2 of 3
Third row: Banana: 3 of 3
使用我的代码,我不知道如何从2D矢量中获取行。我可以获得代码中显示的列。 这是代码:
vector<string> read_file(string filename) {
string classname;
vector<string> v_classname;
ifstream myfile(filename);
while (myfile >> classname)
v_classname.push_back(classname);
return v_classname;}
int main(int argc, char* argv[]) {
int num;
vector<vector<string>> class_matrix;
vector<string> v_classes;
cout << "Enter number vectors: " << endl;
cin >> num;
for (int i = 0; i < num; i++) {
string name;
cout << "Enter name of file " << i + 1 << ": ";
cin >> name;
v_classes = read_file(name);
class_matrix.push_back(v_classes);
}
for (int i = 0; i < num; i++) {
unordered_map<string, size_t> m;
for (const auto& s : class_matrix[i])
++m[s];
for (const auto& p: m)
cout << i+1 << ". column: " << p.first << "\t" << p.second << " of " << v_classes.size() << endl;
}
return 0;}
答案 0 :(得分:0)
很抱歉这么晚。此功能可以帮助您:
void CalcMostFrequentInRow( const std::vector<std::vector<std::string>>& input )
{
std::vector<std::multimap<size_t, std::string, std::greater<size_t>>> VectorAllData;// Holds elements max_repetitions => key in each row. All data stored here
for ( auto SubVector : input )
{
std::map<std::string, size_t> TempCounterMapByKey; // Holds key => max_repetitions
std::multimap<size_t, std::string, std::greater<size_t>> TempKeysCounterMapByCount; // For current roe holds max_repetitions => key descending with a help of std::greater
std::for_each(SubVector.begin(), SubVector.end(), [&TempCounterMapByKey]( const std::string& key )
{
TempCounterMapByKey[key]++; // Counter of unique key in row
});
std::for_each(TempCounterMapByKey.begin(), TempCounterMapByKey.end(), [&TempKeysCounterMapByCount]( const auto pair )
{
TempKeysCounterMapByCount.insert(std::make_pair(pair.second, pair.first));
});
VectorAllData.push_back(TempKeysCounterMapByCount); // Save data statistic for each row
}
//Trace all data
size_t RowId = 1;
const size_t COUNT_ROWS = VectorAllData.size();
for ( size_t i = 0; i < COUNT_ROWS; i++ )
{
auto El = VectorAllData[i];
std::cout << "Row " << RowId++ << " contains";
const size_t COUNT_KEYS_IN_ROW = input[i].size();
if( El.size() > 0 )
{
size_t MostFrequentIdCount = El.begin()->first;
auto range = El.equal_range(MostFrequentIdCount);// Range of most frequent elements
const size_t CountMostFrequentElements = El.count(MostFrequentIdCount);
// Specific trace if we have several elements with max counter of most frequent keys
if ( CountMostFrequentElements > 1 )
{
std::cout << " several most frequent elements(" << MostFrequentIdCount << " repetition of " << COUNT_KEYS_IN_ROW << ") and their value is: ";
std::for_each(range.first, range.second, []( const auto El )
{
std::cout << El.second << ", ";
});
std::cout << std::endl;
}
else // Specific trace if we have one element with max counter of most frequent keys
{
std::cout << " one most frequent element(" << MostFrequentIdCount << " repetition of " << COUNT_KEYS_IN_ROW << ") and its value is: ";
std::for_each(range.first, range.second, []( const auto El )
{
std::cout << El.second << " ";
});
std::cout << std::endl;
}
}
}
}
各地的代码都有评论,希望一切都是明白的。因此,对于您的计划,您可以致电:
vector<vector<string>> class_matrix;
//...
//Input data
//...
CalcMostFrequentInRow(class_matrix);
一切都应该有效。例如,我将显示我的测试输入数据:
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
int main()
{
std::vector<std::vector<std::string>> InputVector {{"qwe","qwe","rty"}, {"rty","qwe","rty"}, {"asd","asd","asd"}, {"asd","qwe","rty"}};
CalcMostFrequentInRow(InputVector);
return 0;
}
输出是:
Row 1 contains one most frequent element(2 repetition of 3) and its value is: qwe
Row 2 contains one most frequent element(2 repetition of 3) and its value is: rty
Row 3 contains one most frequent element(3 repetition of 3) and its value is: asd
Row 4 contains several most frequent elements(1 repetition of 3) and their value is: asd, qwe, rty,