C++ 排序时如何保持数组的位置不变?
我正在写一个凯撒密码解码程序,它按降序排列消息中字母的频率。我的问题是当我打印出结果时,数组中频率的位置不再与我设置的字母匹配。我该如何解决这个问题?我还有其他代码,可以删除被解码消息中的标点符号和大写字母,以及除空格和小写字母以外的所有字符 我已经把代码删减到了被质疑的地方C++ 排序时如何保持数组的位置不变?,c++,arrays,function,sorting,frequency,C++,Arrays,Function,Sorting,Frequency,我正在写一个凯撒密码解码程序,它按降序排列消息中字母的频率。我的问题是当我打印出结果时,数组中频率的位置不再与我设置的字母匹配。我该如何解决这个问题?我还有其他代码,可以删除被解码消息中的标点符号和大写字母,以及除空格和小写字母以外的所有字符 我已经把代码删减到了被质疑的地方 #include<iostream> #include<string> #include<fstream> using namespace std; void sortArray(i
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void sortArray(int*, int);
int main()
{
string fileContent = "a coded message which is several hundreds of characters long is being passed into the program";
int count[26];
// This code is skipping over spaces and other characters
for(int f = 0; f < fileContent.length(); f++)
{
if(fileContent[f] == 32)
{
continue;
}
if(fileContent[f] >= 48 && fileContent[f] <= 57)
{
continue;
}
count[(fileContent[f]-'a')%26]++;
}
// Here is where my issue begins. In sortArray, the position of the characters are being changed.
cout << "Letter frequency: Most common to least common" << endl;
sortArray(count, 26);
for(int p = 0; p < 26; p++)
{
cout << char(p + 97) << ": " << count[p] << endl;
}
return 0;
}
void sortArray(int* srcArray, int numElements)
{
for(int x = 0; x < numElements; x++)
{
int max = srcArray[x];
int maxIndex = x;
int hold;
for(int y = x + 1; y < numElements; y++)
{
if(srcArray[y] > max)
{
max = srcArray[y];
maxIndex = y;
}
}
hold = srcArray[x];
srcArray[x] = max;
srcArray[maxIndex] = hold;
hold = 0;
}
}
#包括
#包括
#包括
使用名称空间std;
无效排序(int*,int);
int main()
{
string fileContent=“一条数百个字符长的编码消息正在被传递到程序中”;
整数计数[26];
//此代码跳过空格和其他字符
对于(int f=0;f 在计算计数数组中的频率后,如果(fileContent[f]>=48&&fileContent[f]
std::array<std::pair<char, int>, 26> pairArray;
for (int i = 0; i < 26; ++i)
{
pairArray[i] = std::make_pair('a' + i, count[i]);
}
std::sort(pairArray.begin(), pairArray.end(), myCompare);
for (int i = 0; i < 26; ++i)
std::cout << pairArray[i].first << ": " << pairArray[i].second << std::endl;
这应该按降序对数组进行排序。您面临的问题是,数组中有频率,但频率没有映射到相应的字符。排序频率时,数组会重新排列,但频率的打印不依赖于字符,而是从a-z打印字符以及按排序数组中的顺序分配频率
你能做的就是用相应的字符映射频率。一种解决方案是使用无序映射,字符是键。无序映射是因为它不会在内部根据字符值对映射进行排序,所以你也可以保持频率顺序
您还可以按照@lamandy的建议使用向量对
vector< pair <char, int> > vect;
for (int i = 0; i < 26; i++)
{
vect.push_back(make_pair(char(i + 97), count[i]));
}
sort(vect.begin(), vect.end(), sortbysecVal);
// Printing the sorted vector(after using sort())
cout << "The vector after sort operation is:\n";
for (int i = 0; i<26; i++)
{
// "first" and "second" are used to access
// 1st and 2nd element of pair respectively
cout << vect[i].first << " "
<< vect[i].second << endl;
}
vectorvect;
对于(int i=0;i<26;i++)
{
向量推回(组成向量对(char(i+97),count[i]);
}
排序(vect.begin()、vect.end()、sortbysecVal);
//打印已排序向量(使用sort()后)
cout答案可能是一个标准库专家的三行代码,我还不是。我讨厌标准库。它让编程变得如此简单,以至于任何人都能做到
这是我破解的两个版本。这很有趣
#include <map>
#include <string_view>
#include <vector>
#include <algorithm>
using counted = std::pair<char, unsigned>;
std::vector<counted>
counted_chars(const std::string_view input) {
// Return a vector of <char, count> pairs, where char is an uppercase
// letter, and count is the number of occurrences of the letter (upper or lower).
// It is sorted from highest count to lowest.
using namespace std;
map<char, unsigned> count;
// Count them.
for(char next: input) {if (isalpha(next)) {count[toupper(next)] += 1;}}
// Sort them
vector<counted> sorted(count.size());
copy(count.cbegin(), count.cend(), sorted.begin());
sort(sorted.begin(), sorted.end(), [](counted c1, counted c2)
{ return c1.second > c2.second; });
return sorted;
}
int main() {
std::string str = "a coDed; MESSage which_is several hundreds of characters long is being passed into the program";
auto result = counted_chars(str);
return 0;
}
#包括
#包括
#包括
#包括
使用counted=std::pair;
向量
计数字符(常量标准::字符串视图输入){
//返回成对向量,其中char为大写
//letter,count是该字母的出现次数(上限或下限)。
//它从最高计数到最低计数排序。
使用名称空间std;
地图计数;
//数一数。
对于(char next:input){if(isalpha(next)){count[toupper(next)]+=1;}
//分类
向量排序(count.size());
复制(count.cbegin()、count.cend()、sorted.begin());
排序(sorted.begin(),sorted.end(),[](计数c1,计数c2)
{返回c1.second>c2.second;});
返回排序;
}
int main(){
std::string str=“一条长达数百个字符的编码消息正在被传递到程序中”;
自动结果=计数字符(str);
返回0;
}
另一个不使用std::map的
#include <map>
#include <vector>
#include <algorithm>
using counted = std::pair<char, unsigned>;
std::vector<counted> counted_chars(std::string input) {
using namespace std;
input.resize(remove_if(input.begin(), input.end(), [](char ch) { return !isalpha(ch); })-input.begin());
for(char &ch: input) { ch = toupper(ch); }
sort(input.begin(), input.end());
string present {input};
present.resize(unique(present.begin(), present.end())-present.begin());
std::vector<counted> sorted;
for (char ch:present) {sorted.push_back(make_pair(ch, count(input.begin(), input.end(), ch)));}
sort(sorted.begin(), sorted.end(), [](counted c1, counted c2) { return c1.second > c2.second; });
return sorted;
}
int main() {
std::string str = " -- I have always wished for my computer to be as easy to use as my telephone; My wish has come true because I can no longer figure out how to use my telephone.";
auto result = counted_chars(std::move(str));
return 0;
}
#包括
#包括
#包括
使用counted=std::pair;
std::向量计数字符(std::字符串输入){
使用名称空间std;
resize(如果(input.begin(),input.end(),[](char ch){return!isalpha(ch);})-input.begin(),则删除_);
对于(char&ch:input){ch=toupper(ch);}
排序(input.begin(),input.end());
字符串存在{input};
resize(唯一的(present.begin(),present.end())-present.begin());
std::向量排序;
对于(char ch:present){sorted.push_back(生成_对(ch,count(input.begin(),input.end(),ch));}
排序(sorted.begin(),sorted.end(),[](计数c1,计数c2){return c1.second>c2.second;});
返回排序;
}
int main(){
std::string str=“--我一直希望我的电脑像我的电话一样易于使用;我的愿望实现了,因为我再也不知道如何使用我的电话了。”;
自动结果=计数字符(标准::移动(str));
返回0;
}
可以输入一个输出u和得到什么?考虑创建一个包含字符和整数的结构,用于频率或者仅仅是STD::对,创建一个数组和排序。accordingly@Valgrind1691是的。所以在“count[(fileContent[f]-'a')%26]+;”行中,我将每个字符保存为数组int count[26]中的一个位置。然后我将此数组传递给Sortaray。这是否回答了您的问题?请告诉我您是否希望我编辑此提供的代码以包含程序的更多内容或运行后返回的内容。@hammonak更新了我的答案,请检查,这将解决您的问题。使用此方法,我根本不需要Sortaray函数,是吗?还有,Pairar是否需要ay表示要替换计数[]?您仍然可以选择使用sortArray,但需要修改它以基于第二个值(即频率)进行排序。上面的代码表示在您完成count[]中的频率计算之后
,但您也可以选择立即从pairArray开始,并使用它计算频率。还请注意,通过执行int count[26]={0}将计数[]初始化为0当你声明变量或数组时,C++没有初始化值。抱歉,我只是C++中的初学者。我很困惑,因为我不知道如何初始化MyCube函数。它在父中得到了什么?例如,“空SoTalA射线(int *,int)”。(uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu您可以通过将值复制到映射中来减少排序,交换对的顺序。如果您可以使用Boost,则a在这种情况下完全正确。@Caleth-我不理解该部分
#include <map>
#include <string_view>
#include <vector>
#include <algorithm>
using counted = std::pair<char, unsigned>;
std::vector<counted>
counted_chars(const std::string_view input) {
// Return a vector of <char, count> pairs, where char is an uppercase
// letter, and count is the number of occurrences of the letter (upper or lower).
// It is sorted from highest count to lowest.
using namespace std;
map<char, unsigned> count;
// Count them.
for(char next: input) {if (isalpha(next)) {count[toupper(next)] += 1;}}
// Sort them
vector<counted> sorted(count.size());
copy(count.cbegin(), count.cend(), sorted.begin());
sort(sorted.begin(), sorted.end(), [](counted c1, counted c2)
{ return c1.second > c2.second; });
return sorted;
}
int main() {
std::string str = "a coDed; MESSage which_is several hundreds of characters long is being passed into the program";
auto result = counted_chars(str);
return 0;
}
#include <map>
#include <vector>
#include <algorithm>
using counted = std::pair<char, unsigned>;
std::vector<counted> counted_chars(std::string input) {
using namespace std;
input.resize(remove_if(input.begin(), input.end(), [](char ch) { return !isalpha(ch); })-input.begin());
for(char &ch: input) { ch = toupper(ch); }
sort(input.begin(), input.end());
string present {input};
present.resize(unique(present.begin(), present.end())-present.begin());
std::vector<counted> sorted;
for (char ch:present) {sorted.push_back(make_pair(ch, count(input.begin(), input.end(), ch)));}
sort(sorted.begin(), sorted.end(), [](counted c1, counted c2) { return c1.second > c2.second; });
return sorted;
}
int main() {
std::string str = " -- I have always wished for my computer to be as easy to use as my telephone; My wish has come true because I can no longer figure out how to use my telephone.";
auto result = counted_chars(std::move(str));
return 0;
}