2016-08-19 2 views
1

현재 텍스트 파일에서 출력 할 수있는 코드 조각을 만들었습니다. 텍스트를 읽고 각기 다른 정보를 배열에 넣습니다.배열 배열을 해당 배열로 정렬

저장하려는 정보가 4 가지이므로 4 가지 배열을 사용했습니다. 이 코드는 예상대로 작동하지만 배열 중 하나가 사전 순으로 정렬 된 방식으로 정보를 정렬 할 수있는 방법이 확실하지 않으며 모든 해당 배열이 올바른 시간에 출력되고 출력됩니다.

void displayfile(){ 

string filename1; 
string rartist[NUM]; 
string rtitle[NUM]; 
string ryear[NUM]; 
string rcategory[NUM]; 
ifstream openFile; 
int counter = 0; 
int continu 
bool error = false; 
cout << "test"; 

do{ 
    //Loop to retrieve the file name from user, then open file 
    do{ 
     cout << "Please enter the name of the menu you would like to open: "; 
     cin >> filename1; 
     filename1 += ".txt"; 
     openFile.open(filename1.c_str()); 
     if(openFile.fail()){ 
      cerr << "Check spelling of file name.\n"; 
      error = true; 
     } 
    //Storing text from file into arrays 
    }while(error == true); 
    while(getline(openFile, rartist[counter], ':') && getline(openFile, rtitle[counter], ':') && 
     getline(openFile, ryear[counter], ':') && getline(openFile, rcategory[counter])){ 
    counter++; 
    } 
    //outputting the information stored in the array 
    cout << "ARTIST " << " DVDTITLE " << " YEAR " << " CATEGORY \n"; 
    for(int i = 0; i < counter; i++){ 
     cout << rartist[i] << "    " << rtitle[i] << "    " 
      << ryear[i] << "    " << rcategory[i] << "\n"; 
    } 
    cout << "\n\nIf you would like to read another file, Press 1: "; 
    cin >> continu; 
    }while(continu == 1) 
} 

이것은 현재 텍스트를 표시하는 데 사용하는 기능입니다.

+1

가장 쉬운 방법은 관련 항목이 아닌 각 항목 유형에 대해 별도의 배열을 포함하는 구조의 단일 배열을 수 있도록 데이터를 재구성하는 것입니다. –

+0

이 기능을 유지하고 다른 옵션으로 정렬을 수행하는 새 기능을 생성 할 때 이것이 내가 필요한 것일 수 있습니다. 고마워, 이제 이걸 들여다 봐라. – Thecube

답변

3

나는 당신이 노래에 관한 정보 조각을 읽고 있다고 가정하고, 노래 제목에 따라 그것을 분류하고 싶다. 각 노래에 대해 동일한 종류의 데이터를 읽고 있으므로 별도의 배열이 아닌 단일 배열의 구조를 사용하십시오.

예를 들어 제목을 기준으로 노래를 정렬하는 방법입니다.

struct Song { 
    std::string artist, 
    std::string title, 
    std::string year, 
    std::string category 
}; 

std::vector<Song> songs(NUM); 

// Read data 

std::sort(songs.begin(), songs.end(), 
    [](const Song &a, const Song &b) { 
     return a.title < b.title; 
    }); 
+0

구조체에 루프를 사용하여 별도의 배열에 저장하는 것과 동일한 것을 저장할 수 있습니까? – Thecube

+0

물론. 'rartist [i] '대신'songs [i] .artist'를 사용하십시오. – Nelfeal

1

는 완전히 테스트되지 않은 ++는 11 코드 c를

std::vector<int> indexes(NUM); 
// fill with 0..NUM-1 
std::iota(indexes.begin(), indexes.end(), 0); 

// example sort after artist. 
std::sort(indexes.begin(), indexes.end(), 
    [&rartist](const int &lhs, const int &rhs) { 
     return rartist[lhs] < rartist[rhs]; 
    }); 
// indexes is now sorted in the same way rartist would have been. 

// now iterate in order. 
for (int i : indexes) { 
    std::cout << rartist[i] << "    " 
       << rtitle[i] << "    " 
       << ryear[i] << "    " 
       << rcategory[i] << "\n"; 
} 
+0

['std :: iota'] (http://en.cppreference.com/w/cpp/algorithm/iota)에 관심이있을 수 있습니다. – Jarod42

+0

* 범위에 대해 * std :: for_each보다 자연스러운 것 같습니다 – Jarod42

+0

@ Jarod42, 나는 당신의 권리를 생각하는 것을 싫어하지 않을 것입니다. – Surt

관련 문제