Reputation: 29
I want to make the vector( mycount) that indicates frequency of the elements in myvec. Can you please let me know what is wrong?
#include <iostream>
#include <vector>
#include <cstdlib>
#include <functional>
#include <algorithm>
using namespace std;
int main() {
int num;
cout << "How many numbers do you want to read in?" << endl;
cin >> num;
vector<int> myvec(num);
std::generate(myvec.begin(), myvec.end(), []()->int {return rand(); });
for (int i = 0; i < num; ++i) {
vector<int> mycount[i] = count(myvec.begin(), myvec.end(), myvec[i]);
cout << mycount[i] << endl;
}
return 0;
}
Upvotes: 0
Views: 54
Reputation: 206567
I suspect you meant to use:
vector<int> myvec(num);
// Create a vector for storing the counts as well.
vector<int> mycount(num);
std::generate(myvec.begin(), myvec.end(), []()->int {return rand(); });
for (int i = 0; i < num; ++i) {
// Get the counts of the element at this index and store it.
mycount[i] = count(myvec.begin(), myvec.end(), myvec[i]);
cout << mycount[i] << endl;
}
Upvotes: 1
Reputation: 2143
your mycount definition is wrong. Check the below code
#include <iostream>
#include <vector>
#include <cstdlib>
#include <functional>
#include <algorithm>
using namespace std;
int main() {
int num;
cout << "How many numbers do you want to read in?" << endl;
cin >> num;
vector<int> myvec(num);
std::generate(myvec.begin(), myvec.end(), []()->int {return rand(); });
vector<int> mycount(num); \\declare mycount with num elements
for (int i = 0; i < num; ++i) {
\\populate the counter for each element
mycount[i] = count(myvec.begin(), myvec.end(), myvec[i]);
cout << mycount[i] << endl;
}
return 0;
}
Upvotes: 0