Re: write a program to count occurence of each element in an array
In article <01dfc185-fb73-472d-ab7f-246ada526127
@u36g2000prf.googlegroups.com>, jaideep.twr@gmail.com says...
Hello frends i am learning c language, I want to make a program which
count occurence of each element in an array .I write following code
for it but ity is not giving me desired result.pls help me.
Since you're posting in comp.lang.c++, I'm going to guess that a
solution in C++ is usable:
#include <iostream>
#include <vector>
#include <map>
typedef std::pair<int, int> p;
// Cheating...
namespace std {
std::ostream &operator<<(std::ostream &os, p const &data) {
return os << data.first << "\t" << data.second;
}
}
int main() {
std::vector<int> A;
std::cout << "Enter elements";
std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(A));
std::map<int, int> counts;
for (int i=0; i<A.size(); i++)
++counts[A[i]];
std::cout << "Counts:\n";
std::copy(counts.begin(), counts.end(),
std::ostream_iterator<p>(std::cout, "\n"));
return 0;
}
--
Later,
Jerry.
The universe is a figment of its own imagination.