2014-10-09 14 views
1

對不起,如果這篇文章不是最好的,我是全新的網站和編碼的一般。我的節目以「Sam 16 Emily 4 Molly 19」的形式輸入並按字母順序排序,同時保持年齡相同。它的作用就像魅力,除非你輸入5對以上。輸入5後,以下名稱排序良好,但年齡並非正確。我不確定代碼在哪裏得到它輸出的數字。這是我的代碼。我很抱歉發佈一切(〜30行),但我真的不知道問題出在哪裏。C++:按字母順序排列名稱/年齡對,只能管理5對

#include"../std_lib_facilities.h" 


vector<string> name; 
vector<int> age; 

void read_pairs() 
{ 
    string n; 
    int v; 

    while (cin >> n >> v && n != "NoName") { // read string int pair 
     for (int i = 0; i<name.size(); ++i) 
     name.push_back(n); 
     age.push_back(v); 
    } 
} 

void write_pairs(string label) 
{ 
    cout << label; 
    for (int i = 0; i<name.size(); ++i) 
     cout << '(' << name[i] << ',' << age[i] << ")\n"; 
} 

int find_index(const vector<string>& v, const string& n) 
// find n's index in v 
{ 
    for (int i = 0; i<n.size(); ++i) 
    if (n == v[i]) return i; 
} 

int main() 
try 
{ 
    cout << "Please enter your name/age pairs. When finished, enter 'No More'\n"; 
    read_pairs(); 


    vector<string> original_names = name; // copy the names 
    vector<int> original_ages = age;  // copy the ages 

    sort(name.begin(), name.end());   // sort the names 

    for (int i = 0; i<name.size(); ++i)  // update ages 
     age[i] = original_ages[find_index(original_names, name[i])]; 

    write_pairs("\nSorted:\n"); 

    keep_window_open("~"); 
} 
catch (runtime_error e) { 
    cout << e.what() << '\n'; 
    keep_window_open("~"); 
} 
catch (...) { 
    cout << "exiting\n"; 
    keep_window_open("~"); 
} 
+0

我認爲你的問題與通過引用分配向量的方式有關。很快就會發布答案。 – Hosch250 2014-10-09 02:40:43

+2

這不可能工作 - 由於這個循環,'name'向量總是空的:'for(int i = 0; i 2014-10-09 02:42:13

+0

@ hosch250:我不太清楚你在想什麼,但是「std :: vector'不是」通過引用分配「。 – Blastfurnace 2014-10-09 03:06:42

回答

0

在代碼中有許多問題。我甚至無法將它編譯一段時間。

首先,for循環在read_pairs()正在阻止您在name中存儲任何名稱。其次,您需要查看n中的read_pairs()是否爲之前的輸入年齡 - 否則您將中斷cin,您將在後面學習,因爲我知道您正在使用Bjarne Stroustrup的書 - 同一個我從中學到了。第三,您的代碼突破find_index(),因爲您在for循環中調用了i < n.size();而不是i < v.size();。最後,你可能想要處理同一個名字的多個實例,所以你不會得到一個年齡。我已經實現了一個簡單的方法來實現這一點,在我返回數組中名稱的位置之前,將v[i]的值設置爲"",find_index() - 這樣,您不能多次獲取該名稱的年齡。

這是工作代碼,包含一些註釋。隨意問你是否不明白某些事情,但我認爲你有足夠的理解來弄明白。

#include "stdafx.h" 
#include"../std_lib_facilities.h" 

vector<string> name; 
vector<int> age; 

void read_pairs() 
{ 
    string n; 
    int v; 

    while (true) { // read string int pair 

    cin >> n; // input name 

    if (n == "No") // if name is "No", break so we do not input a string for age 
     break; 

    cin >> v; // input age 

    // insert name/age into vectors 
    name.push_back(n); 
    age.push_back(v); 
    } 
} 

void write_pairs(string label) 
{ 
    cout << label; 
    for (int i = 0; i<name.size(); ++i) 
    cout << '(' << name[i] << ',' << age[i] << ")\n"; // output name/age pairs 
} 

int find_index(vector<string>& v, const string n) 
// find n's index in v 
{ 
    for (int i = 0; i < v.size(); i++) 
    { 
    if (n == v[i]) 
    { 
     v[i] = ""; // remove name in case two of same name 
     return i; 
    } 
    } 
} 

int main() 
try 
{ 
    cout << "Please enter your name/age pairs. When finished, enter 'No More'\n"; 

    read_pairs(); // read name/age pairs 

    vector<string> original_names = name; // copy the names 
    vector<int> original_ages = age; // copy the ages 

    sort(name.begin(), name.end());  // sort the names 

    for (int i = 0; i < name.size(); i++) // update ages 
    age[i] = original_ages[find_index(original_names, name[i])]; 

    write_pairs("\nSorted:\n"); // write name/age pair 

    keep_window_open("~"); 
} 
catch (runtime_error e) { 
    cout << e.what() << '\n'; 
    keep_window_open("~"); 
} 
catch (...) { 
    cout << "exiting\n"; 
    keep_window_open("~"); 
} 
+0

這絕對美妙,謝謝!它比我在嘗試結束時更有意義。我似乎沒有能夠投票,但我在精神上擁抱你。 – Nickless 2014-10-09 03:48:19

+0

@Nickless不客氣。 – Hosch250 2014-10-09 04:25:50