简体   繁体   中英

trying to sort a txt file in c++ using a function and writing it back out to a txt file

 void insertionSort (int arrtosort[], int size)
{
 int temp = arrtosort[0];
 for(int i = 1; i < size; i++)
{
    temp = arrtosort[i];
    int j = 0;
    for(j = i; j > 0; j--)
        if(temp < arrtosort[j - 1])
           arrtosort[j] = arrtosort[j - 1];
        else break;
    arrtosort[j-1] =  temp;
}
}

I am trying to use this sort function to sort a txt file inside of a case.

what I tried was

case 1:

        insertionSort(fileid,SIZE);
        ingrades.open("dataout.txt");
        for (idx=0;idx<SIZE;idx++)
        {
            int id,n_grade;
            string l_grade;
            ingrades>>id>>n_grade>>l_grade;
            for(int i=0;i<SIZE;i++)
            {
                if(fileid[i]==id)
                {
                    out.open("data.txt");
                    out<<id<<" "<<n_grade<<" "<<l_grade<<endl;
                    out.close();
                }
            }

        }
        ingrades.close();

    break;

I have tried different variations with this code not just writing to the txt file but to just simply display it in the console

14731 4
15960 6
15517 8
16638 1
34974 6
32684 4
35157 2
33904 4
23132 7
37344 3

These are the numbers I'm trying to sort in my program letter grades are written to it before it hits the case. What I'm trying to do is use the function to sort the file and write it out to a txt file or simply just display on the console all help is appreciated.

another problem is that when i use the function i seem to get the same number over and over stead of all of them as though the rest were deleted and replaced by the largest number

Read the file, store the key value pairs into a std::map, then iterate the map from the beginning to the end writing it's contents to the file.

The trick is that std::map automaticly sorts data for you and you can even specify a comparator!

Maps are sorted based on their key.

if you want to sort only numbers here is an example from cplusplus.com

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
    bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
    int myints[] = {32,71,12,45,26,80,53,33};
    std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

    // using default comparison (operator <):
    std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

    // using function as comp
    std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

    // using object as comp
    std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

    // print out content:
    std::cout << "myvector contains:";
    for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}

output:

myvector contains: 12 26 32 33 45 53 71 80

if you want more values just use std::pair<int,int> for the vector and create your own comparision function like in the example.

bool myfunction (std::pair<int,int> i,std::pair<int,int> j) { return (i.first<j.first); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM