第一步:把文本里面的数据提取出来付给一个变量容器vector<string>然后遍历一遍vector把string转化位float。
然后遍历一遍把float数据进行四舍五入和转化位精度位0.05的数据,再从新赋给一个vector<float>容器
然后再创建一个vector<string>容器把转化后的数据全部存入该容器中,
然后创建一个string和一个vector<string>容器上一步容器中的数据每8个赋值给string,然后vector.push_back(string)
string= std::accumulate(newstr_vec.begin(), newstr_vec.end(), string);//容器里面的数每八个付给一个数组
第二步:遍历一遍容器查找每个元素出现的个数count(),并把对应的数组和个数付给一个二维容器
for (auto &i : newstr_vecs)
{
temp_count1 = count(newstr_vecs.begin(), newstr_vecs.end(), i);//统计temp_vec容器中每个元素的个数
auto temp_it = find(newstr_vecs.begin(), newstr_vecs.end(), i);//遍历容器中每个元素
temp_vec_pair1.push_back(make_pair(temp_count1, i));//把对应的元素和元素出现的次数存入容器temp_vec_pair1中
}//上面是把每个元素和其对应的出现次数存到二维容器temp_vec_pair1中
第三步:把相同元素合并只留一个
std::stable_sort(temp_vec_pair1.begin(), temp_vec_pair1.end());
std::vector<std::pair<int, std::string>>::iterator iter = std::unique(temp_vec_pair1.begin(), temp_vec_pair1.end());
temp_vec_pair1.erase(iter, temp_vec_pair1.end());
第四步:按出现的次数降序排列
stable_sort(temp_vec_pair1.begin(), temp_vec_pair1.end(), [](std::pair<int, std::string> p1, std::pair<int, std::string> p2)-> bool {
return p1.first > p2.first; });//提取出现次数最多且相同的字符
第五步:输出到文件
fout<<""<<""<<""<<endl;
源码
void FindData::Split(const std::string& src, const std::string& separator, std::vector<std::string>& dest)
{
std::string str = src;
std::string substring;
std::string::size_type start = 0, index;
dest.clear();
index = str.find_first_of(separator, start);
do
{
if (index != std::string::npos)
{
substring = str.substr(start, index - start);
dest.push_back(substring);
start = index + separator.size();
index = str.find(separator, start);
if (start == std::string::npos) break;
}
} while (index != std::string::npos);
//the last part
substring = str.substr(start);
dest.push_back(substring);
}//遇到分隔符就执行赋值操作
float FindData::stringToNum(const std::string& str)
{
std::istringstream iss(str);
float num;
iss >> num;
return num;
}//string转换为float
void FindData::DataInt()
{
std::ifstream fin("Index.txt");//输入流文件
std::ofstream fout("round_off_result.txt");//输出数据到文件
std::string data_row;//输入流里面的数据
std::vector<std::string> data;
std::vector<float> newdata;
while (getline(fin,data_row))
{
Split(data_row, " ", data);//遇到空格符就把data_row转换到data中
for (int i = 0; i < (data.size()-1); i++)
{
float tp = ((float)((int)((((int)((stringToNum(data[i]) + 0.005) * 100)) + 2) / 5)) * 5) / 100;
newdata.push_back(tp);//转换好的数据存入容器
}
}//上面是把所有数据按0.05精度转换
std::string newstring;
std::string lastdata;
std::vector<std::string> newstr_vec,newstr_vecs;
std::strstream StrStm;
for (int i = 0; i < newdata.size(); i++)
{
StrStm.clear();
StrStm << newdata[i];
StrStm >> newstring;
newstring = newstring + " ";
newstr_vec.push_back(newstring);
if ((i+1)%8==0)
{
lastdata = std::accumulate(newstr_vec.begin(), newstr_vec.end(), lastdata);//容器里面的数每八个付给一个数组
newstr_vecs.push_back(lastdata);
lastdata.clear();
newstr_vec.clear();
}
}//上面是把float数据转换成一组一组的string数据并存到容器newstr_vecs中
std::vector<std::pair<int, std::string>> temp_vec_pair1, temp_vec_pair2;
int temp_count1 = 0;
for (auto &i : newstr_vecs)
{
temp_count1 = count(newstr_vecs.begin(), newstr_vecs.end(), i);//统计temp_vec容器中每个元素的个数
auto temp_it = find(newstr_vecs.begin(), newstr_vecs.end(), i);//遍历容器中每个元素
temp_vec_pair1.push_back(make_pair(temp_count1, i));//把对应的元素和元素出现的次数存入容器temp_vec_pair1中
}//上面是把每个元素和其对应的出现次数存到二维容器temp_vec_pair1中
std::stable_sort(temp_vec_pair1.begin(), temp_vec_pair1.end());
std::vector<std::pair<int, std::string>>::iterator iter = std::unique(temp_vec_pair1.begin(), temp_vec_pair1.end());
temp_vec_pair1.erase(iter, temp_vec_pair1.end());
stable_sort(temp_vec_pair1.begin(), temp_vec_pair1.end(), [](std::pair<int, std::string> p1, std::pair<int, std::string> p2)-> bool {
return p1.first > p2.first; });//提取出现次数最多且相同的字符
//上面是把相同元素融合并按出现的次数降序排列
for (int i = 0; i < 20; i++)
{
//temp_vec_pair2.push_back(temp_vec_pair1[i]);
fout << temp_vec_pair1[i].second << " 出现次数为: " << temp_vec_pair1[i].first << " 次" << std::endl;
std::cout << temp_vec_pair1[i].second << " 出现次数为: " << temp_vec_pair1[i].first << " 次" << std::endl;
}//上面是输出前N个数据,即排名前几的数据
//把出现次数最多的元素付给temp_vec_pair2
//for (auto &b : temp_vec_pair1)
//{
// temp_count2 = temp_vec_pair1.begin()->first;
// lastdata = temp_vec_pair1.begin()->second;
// if (temp_count2 == b.first && lastdata != b.second) {
// temp_vec_pair2.push_back(b);
// }
//}
//输出出现次数最多且相同的字符
/* for (auto &i : temp_vec_pair2) {
}*/
}