I've been working on a school project lately and we were introduced to Vectors. I haven't fully mastered it yet, but my general notion of them is that vectors are pretty much like 2-d arrays that can store different types of data. I attached some code below but I get the following error:
no instance of constructor "std::vector<_Ty, _Alloc>::vector [with _Ty=std::vector<double, std::allocator<double>>, _Alloc=std::allocator<std::vector<double, std::allocator<double>>>]"
I'm not quite sure where I'm wrong exactly, but here is the declaration of the function and my code.
//EFFECTS: returns a summary of the dataset as (value, frequency) pairs
// In the returned vector-of-vectors, the inner vector is a (value, frequency)
// pair. The outer vector contains many of these pairs. The pairs should be
// sorted by value.
// {
// {1, 2},
// {2, 3},
// {17, 1}
// }
//
// This means that the value 1 occurred twice, the value 2 occurred 3 times,
// and the value 17 occurred once
std::vector<std::vector<double> > summarize(std::vector<double> v);
My code:
vector<vector<double> > summarize(vector<double> v) {
sort(v); //Sorts vector by values, rearranging from smallest to biggest.
double currentVal = v[0];
int count = 0;
vector<pair<double,int>>vout = { {} };
for (size_t i = 0; i < v.size(); i++) {
if (v[i] == currentVal) {
count++;
}
else {
vout.emplace_back(make_pair(currentVal, count));
currentVal = i;
count = 1;
}
}
vout.emplace_back(make_pair(currentVal, count));
return { {vout} }; //Error here
}
I appreciate your help and any explanations of what I'm doing wrong here. If any more information is needed, please let me know.