0

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Walaz
  • 1
  • 2
    Change the return type of `summarize` to `std::vector>` and return `vout` instead of `{{vout}}` – Indiana Kernick Jan 18 '20 at 05:12
  • 1
    Also, https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Indiana Kernick Jan 18 '20 at 05:14
  • A better name for `summarize` might be `histogram` – Sneaky Turtle Jan 18 '20 at 05:33
  • 2
    vector>vout = { {} } - does that actually do what you want it to do? That puts a default constructed pair in your vector, so it starts out with one pair in it. It kinda feels like you're just throwing braces in places without understanding why. Also, you need to be posting *minimal* examples, so the homework assignment isn't meaningful. All the logic in the middle isn't meaningful - you can literally trim the function down to two lines and reproduce the problem and just post that. – xaxxon Jan 18 '20 at 07:51

0 Answers0