2017-02-26 47 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; 
}