2017-02-26 59 views
0

好了,所以我有一個這樣的數組:C++從陣列合併重複的搜索結果

class name { 
    public: 
    string first; 
    int last; 

    name(string a, int b){ 
    first = a; 
    last = b; 
    } 
}; 

name arr[] { { "John", 1 }, { "Jane", 2 }, { "Dick", 3 }, { "John", 1 }, { "Jane", 2 } }; 

現在我只能全部打印出來是這樣的:

int main() { 

    int icount = sizeof(arr)/sizeof(name); 

    for (int i = 0; i < icount; i++){ 
     cout << arr[i].first << " " << arr[i].last << endl; 
    } 

} 

Output will be like this: 
John 1 
Jane 2 
Dick 3 
John 1 
Jane 2 

不過,我需要合併任何類似的結果,這意味着如果名稱相同,我需要將它們後面的數字相加。所需的輸出應該是這樣的:

John 2 
Jane 4 
Dick 3 

有沒有我可以用它來合併它們,或者我應該如何去這樣做的任何功能?

+0

查找標準算法'唯一的()'或'unique_copy()'。根據您的需要,您可能需要先對容器進行分類。 – Peter

回答

0

由於您使用的是C++,因此您可以使用Map來完成您的工作。

這是您需要的完整程序。

#include <iostream> 
#include <map> 

using namespace std; 

map<string, int> mer; 

class name 
{ 
public: 
    string first; 
    int last; 

    name (string a, int b) 
    { 
     first = a; 
     last = b; 
    } 
}; 

name arr[] { { "John", 1 }, { "Jane", 2 }, { "Dick", 3 }, { "John", 1 }, { "Jane", 2 } }; 

void merge() 
{ 
    int icount = sizeof(arr)/sizeof(name); 
    for (int i = 0; i < icount; i++) 
    { 
     if (mer.count(arr[i].first) > 0) 
     { 
      mer[arr[i].first]++; 
     } 
     else 
     { 
      mer[arr[i].first] = 1; 
     } 
    } 
} 

int main() 
{ 
    int icount = sizeof(arr)/sizeof(name); 
    merge(); 
    map<string,int>::iterator it; 
    for(it = mer.begin(); it != mer.end(); it++) 
    { 
     cout << it->first << " " << it->second << endl; 
    } 

    return 0; 
} 

這將打印出您所期望的結果。當然,它會以相反的順序打印。您可以使用反向迭代器來獲得所需的結果。

+0

不適用於我..我得到這個警告:擴展的初始化程序列表僅適用於-std = C++ 11或-std = gnu ++ 11 |。 – Pokee

+0

@Pokee所以你的例子不應該編譯。將'-std = C++ 11'添加到編譯命令中。你初始化數組的方式需要C++ 11 – mpiatek

+0

它在Ideone中工作正常。試試這個鏈接http://ideone.com/ZSmnCd – Sridharan

0

這裏是另一種變體:

#include <iostream> 
#include <string> 
#include <vector> 
#include <map> 

using namespace std; 


class name { 
public: 
    string first; 
    int last; 

    name(string a, int b); 
}; 

name::name(string a, int b) : first(a), last(b) 
{ 

} 


int main(int argc, char **argv) 
{ 
    string names[] = 
    { 
     "John", "Jane", "Dick", "John", "Jane" 
    }; 

    int values[] = 
    { 
     1, 2, 3, 1, 2 
    }; 

    vector<name> people; 
    for (int i = 0; i < sizeof(names)/sizeof(*names); ++i) 
    { 
     people.push_back(name(names[i], values[i])); 
    } 

    map<string, int> unique_people; 
    for (vector<name>::const_iterator it = people.begin(), 
     end = people.end(); it != end; ++it) 
    { 
     if (unique_people.count(it->first) != 0) 
     { 
      unique_people[it->first] += it->last; 
     } 
     else 
     { 
      unique_people.insert(pair<string, int>(it->first, it->last)); 
     } 
    } 

    for (map<string, int>::const_reverse_iterator it = unique_people.rbegin(), 
     end = unique_people.rend(); it != end; ++it) 
    { 
     cout << it->first << " " << it->second << endl; 
    } 

    return 0; 
}