c++ - 排序数组文件 I/O

标签 c++ arrays

我正在寻找对数组排序的帮助。我必须这样做,但我收到一些我不理解的错误消息,例如:

  1. [警告“索引”的名称查找已更改。
  2. 根据 ISO 标准规则匹配此 'char* 索引 [const char*, int]
  3. 根据旧规则匹配此索引
  4. 数组下标的无效类型 'int&[char()(const char*, int)]
  5. 在全局范围内

我有点怀疑它是什么,但有点不知道如何修复它。 我打开的文件是这样一句话:奥利弗是一只金毛猎犬,它的毛又长又金。

如您所知,我是一个完全的初学者,所以任何提示都将不胜感激提前致谢!

#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++ - 排序数组文件 I/O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13437151/

相关文章:

c++ - 如何在 3D 物体(如球体)上绘制文字

c++ - 在 C++ 中编译未定义和未使用的函数

c++ - 为什么带有 const 参数的函数声明允许调用带有非常量参数的函数?

c - 在 C 中使用数组的序列中出现频率最高的元素

javascript - 具有非空值的唯一键

c - 从函数传递结构数组

arrays - 在没有循环的情况下为每一行将矩阵的不同元素归零

c++ - QReadWriteLock递归

c++ - 在随机数组中查找最小值和最大值

java - 如何使用groovy从soapui Json Response构造JsonPath?