对数组文件 I/O 进行排序
Sorting an Array File I/O
我正在寻找一些关于对数组进行排序的帮助。我必须这样做,但是我收到一些我不理解的错误消息,例如:
- [警告 "索引"的名称查找已更改。
- 匹配此"字符* 索引[常量字符*,整数] undr ISO 标准规则
- 在旧规则下匹配此索引
- 数组下标的无效类型 'int&[char()(const char*, int)]
- 在全球范围内
我对它是什么有些怀疑,但对如何解决它有点迷茫。我打开的文件是这样写的一句话:奥利弗是一个金色的猎人,他的皮毛又长又金。
正如您所知,我是一个完整的初学者,因此任何提示将不胜感激,提前感谢您!
#include <iostream>
#include <fstream>
using namespace std;
void swap_values(int& v1, int& v2);
int index_of_smallest(int list[],int start_index, int number_used);
void initialize(int list[]);
void Sort(int list[],int&num);
void characterCount(char ch, int list[]);
void readText(ifstream& intext, char& ch, int list[]);
void totalCount(int list[]);
int main()
{
int index,letterCount[26];
char ch;
ifstream inFile;
ofstream outFile;
cout<<"This is the text of the file:"<<endl;
outFile.open("C:/temp/Data_Chapter_7_8.txt");
if(!outFile)
{
cout<<"Cannot open file."<<endl;
}
inFile.open("C:/temp/Data_Chapter_7_8.txt");
if (!inFile)
{
cout << " Cannot open file." <<endl;
}
initialize(letterCount);
inFile.get(ch);
while (inFile.get(ch))
{
int index;
readText(inFile,ch,letterCount);
index++;
}
totalCount(letterCount);
inFile.close();
system("PAUSE");
return 0;
}
void initialize(int list[])
{
for(int x = 0;x<26;x++)
list[x] = 0;
}
void characterCount (char ch, int list[])
{
ch = tolower(ch);
if(static_cast<int>(ch)>=97&&(static_cast<int>(ch)<=122))
list[static_cast<int>(ch)-97]++;
}
void readText(ifstream& intext, char& ch, int list[])
{
if (ch != '.')
{
characterCount (ch,list);
}
}
void totalCount(int letterCount[])
{
for(int x=0;x<26;x++)
if(letterCount[x]>0)
cout<<static_cast<char>(x+97)<<" "<<letterCount[x]<<endl;
}
void Sort(int list[], int number_used)
{
int index_of_next_smallest;
for(int index= 0; index<number_used -1; index++)
index_of_next_smallest = index_of_smallest(list, index,number_used);
swap_values(list[index],list[index_of_next_smallest]);
}
}
int index_of_smallest(int list[], int start_index, int number_used);
{
int min = list[start_index];
index_of_min = start_index;
for (int index= start_index + 1; index < number_used; index++)
if (list[index]>min)
{
min = list[index];
index_of_min = index;
}
return index_of_min;
}
void swap_values(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
在Sort
函数中,更改
for(int index= 0; index<number_used -1; index++)
自
int index;
for(index = 0; index < number_used-1; index++)
因为您需要在循环结束后访问index
。
相关文章:
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 显示错误输出的简单数组排序程序
- 为什么我的排序算法会更改数组值
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 合并排序不排序自创建数组类 c++
- 带有枚举方向/类型的气泡排序结构数组
- 为什么指针在对二维数组进行排序时无法正常工作?
- 排序并行数组
- 如何在数组 c++ 中对字符串进行排序
- 为什么在此排序算法实现中,向量明显比数组慢?
- 如何对不在数组中的变量进行排序?
- C++数组与向量排序(在我的情况下,向量比数组慢~2.5倍(无优化))
- 按字母顺序对结构内数组变量中的名称进行排序
- 使用排序函数 c++ 对字符数组进行排序
- 使用 std::sort 对二维 c 数组进行排序
- 使用STL对用户输入数组进行排序的错误有什么解决方案吗?
- 使用 std::sort 对 C 样式的 2D 数组进行部分排序
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 对 0 、1 和 2 的数组进行排序