3

I have something like this:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

void shortestRemainingTime(map<string, string> processes[]){

    int size = (sizeof(processes)/sizeof(*processes));
    cout << size;

}

int main() {
    map<string, string> process { { "name", "open paint" }, { "remainingTime", "1000" } };
    map<string, string> process2{ { "name", "open photoshop" }, { "remainingTime", "500" } };
    map<string, string> process3{ { "name", "open word" }, { "remainingTime", "600" } };

    map<string, string> processes[] = {process, process2, process3};
    shortestRemainingTime(processes);
  return 0;
}

For now, I'm not making any calculation in shortestRemainingTime but, when I print the size of the array of the map processes, I'm getting 0, which isn't right.

How can I get the right length of this special array?

map<string, string> processes[] = {process, process2, process3};
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Layer
  • 326
  • 3
  • 12

1 Answers1

4

When you pass an array as an argument to a function, it decays into a pointer and, as such, you cannot use the sizeof(processes)/sizeof(*processes) paradigm/method on that.

You should be using a std::vector in place of your array, in which case you can then use its size() function. Here's a version of your code that does just that:

#include <iostream>
#include <map>
#include <vector>
#include <string>
using std::cout;
using std::vector;
using std::map;
using std::string;

// Note: Passing the vector by reference avoids having to copy a (potentially) 
// large object. Remove the "const" qualifier if you want the function to modify
// anything in the vector...
void shortestRemainingTime(const vector<map<string, string>> &processes)
{
    size_t size = processes.size();
    cout << size;
}

int main()
{
    map<string, string> process{ { "name", "open paint" }, { "remainingTime", "1000" } };
    map<string, string> process2{ { "name", "open photoshop" }, { "remainingTime", "500" } };
    map<string, string> process3{ { "name", "open word" }, { "remainingTime", "600" } };

    vector<map<string, string>> processes = { process, process2, process3 };
    shortestRemainingTime(processes);
    return 0;
}

Also, please see the following: Why should I not #include <bits/stdc++.h>? and Why is "using namespace std;" considered bad practice? for good coding guidelines.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Here's that link https://stackoverflow.com/questions/7454990/why-cant-we-pass-arrays-to-function-by-value – Kenny Ostrom Apr 06 '20 at 01:46
  • I was going to post the same. Not only are the sizeof results not what was expected, but also they are being obscured behind integer division. On my system, it produces 8 / 24 which is correctly 0 (as an integer) – Kenny Ostrom Apr 06 '20 at 01:47
  • 1
    Thank you, Adrian Mole, Kenny Ostrom and tadman, i'm going to apply your recomendations, this works well ;3 – Layer Apr 06 '20 at 01:50
  • of course you should use std::vector, but you "can" use that array paradigm, just not inside the function. Arrays have to be passed along with their length as a function argument. – Kenny Ostrom Apr 06 '20 at 02:57