2014-11-22 97 views
0

03利賓納7 2.20從txt文件讀入矢量然後排序數值C++

17咖啡76 1.50

24茶21 1.10

39的Sprite 3 1.70

56可樂18 1.70

68桔子106 2.20

77青檸11 2.10

86葡萄34 2.30

55巧克力15 2.70

16霧20 2.20

65檸檬水21 1.90

55酸奶99 3.40

05衝頭3 1.70

這些記錄需要進行排序到第一欄的項目編號。

void record_initialize() 
{ 
    vector<string> record; 
    cout << "\n"; 
    record.push_back("03""\t\t""Ribena""\t\t""7""\t\t""2.20"); 
    record.push_back("17" "\t\t""Coffee" "\t\t""76" "\t\t""1.50"); 
    record.push_back("24" "\t\t""Tea"  "\t\t""21" "\t\t""1.10"); 
    record.push_back("39" "\t\t""Sprite" "\t\t""3" "\t\t""1.70"); 
    record.push_back("56" "\t\t""Cola" "\t\t""18" "\t\t""1.70"); 
    record.push_back("68" "\t\t""Orange" "\t\t""106""\t\t""2.20"); 
    record.push_back("77" "\t\t""Lime" "\t\t""11" "\t\t""2.10"); 
    record.push_back("86" "\t\t""Grape"  "\t\t""34" "\t\t""2.30"); 
    record.push_back("55" "\t\t" "Chocolate" "\t""15" "\t\t""2.70"); 
    record.push_back("16" "\t\t""Frosty" "\t\t""20" "\t\t""2.20"); 
    record.push_back("55" "\t\t" "Lemonade" "\t""21" "\t\t""1.90"); 
    record.push_back("55" "\t\t""Yoghurt" "\t\t""99" "\t\t""3.40"); 
    record.push_back("05" "\t\t""Punch"  "\t\t""3" "\t\t""1.70"); 
    cout << "\n"; 
    //sort(record.begin(), record.end()); 
    ofstream output_file("Drinks.txt"); 
    ostream_iterator<string> output_iterator(output_file, "\n"); 
    copy(record.begin(), record.end(), output_iterator); 
} 
void record_add() 
{ 
    DrinkRecord d; 
    cout << "\n"; 
    cout << "Please enter the item no.: "; 
    cin >> d.no; 
    cout << "\n"; 
    cout << "Please enter name of drink: "; 
    cin >> d.name; 
    cout << "\n"; 
    cout << "Please enter the quantity: "; 
    cin >> d.quantity; 
    cout << "\n"; 
    cout << "Please enter the unit price: "; 
    cin >> d.price; 
    cout << "\n"; 
    ofstream myfile; 
    myfile.open("Drinks.txt", ios::app | ios::out); 
    cout << "\n"; 
    myfile << d.no << "\t\t" << d.name << "\t\t" << d.quantity << "\t\t" << d.price << endl; 
    myfile.close(); 
} 

void record_view() 
{ 

    cout << "\n"; 
    cout << "ItemNo" << "\t\t" << "ItemName" << "\t" << "Quantity" << "\t" << "Unit Price" << endl; 
    cout << "\n"; 
    string getcontent; 
    ifstream openfile("Drinks.txt"); 
    if (openfile.is_open()) 
    { 
     while (!openfile.eof()) 
     { 
      getline(openfile, getcontent); 
      cout << getcontent << endl; 
     } 
    } 
} 

我設法做到了這一點。但是,在向飲料庫存添加新項目後,我現在遇到了問題。我的講師告訴我要將txt文件讀入矢量,然後對矢量進行排序,然後顯示結果。我似乎無法得到正確的步驟。任何幫助將非常感激。 我嘗試過這樣的事情。

void read_File() //Read the file into the vector function definition 
{ 

    vector<string> logs; 
    string line; 


    cout << "Testing loading of file." << endl; 
    ifstream myfile("Drinks.txt"); 
    if (myfile.is_open()) 
    { 
     while (!myfile.eof()) 
     { 
      getline(myfile, line); 
      logs.push_back(line); 
      sort(line.begin(), line.end()); 

     } 
     myfile.close(); 
    } 
    else{ 
     cout << "Unable to open file." << endl; 
    } 

} 
+0

您沒有排序的載體。你正在排序每行的字母。另請不要在'eof()'循環:http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Galik 2014-11-22 21:28:03

回答

0

看來你正在閱讀每一行後分類。請注意,sort的呼叫位於循環內部。這意味着在每次迭代中(每行讀後),都可以撥打sort。如果我正確理解您的作業,您需要對文件中的所有行(logs)進行排序。如果是這樣,那麼這意味着你只需要撥打sort一次。而不是將該行作爲參數傳遞,您可能需要將它作爲參數傳遞給logs向量,因爲您想對日誌進行排序。

0

您必須在閱讀整個文件後進行一次排序。另外,sort(line.begin(),line.end());將根據字符串比較規則對矢量中的字符串進行排序,但是,您需要根據第一列進行排序。
您需要編寫自己的自定義比較器,它從兩個字符串中讀取第一列進行比較。

由於這看起來像一個類的問題,我會讓你寫下面的比較函數。
請參閱下面的鏈接以獲取更多幫助。

sort(line.begin(), line.end(), mycolumncomparator); 

// Fill the below function correctly 
bool mycolumncomparator(const std::string& i, const std:string& j) 
{ 
    // Read the first column from both strings 
    // covert them into int using atoi 
    // return (int1 < int2); 
} 

請參考這裏:http://www.cplusplus.com/reference/algorithm/sort/