-2

there is a question in which I need to manipulate vector of pairs but it seems to show some errors

Code


#include "bits/stdc++.h"
using namespace std;
bool compare(vector<int> arr1[3], vector<int> arr2[3])
{
  return arr1[1] < arr2[1];
}
int max_trains(vector<vector<int>> arr[][3], int n, int m)
{

  vector<pair<int, int>> vect(n + 1);

  //departure and arrival respectively
  for (int i = 0; i < m; i++)
  {
    vect[arr[i][2]].push_back(make_pair(arr[i][1], arr[i][0]));
  }

  for (int i = 0; i <= n; i++)
    sort(vect[i].begin(), vect[i].end());

  int count = 0;
  for (int i = 0; i <= n; i++)
  {
    if (vect[i].size() == 0)
      continue;

    int x = 0;

    count++;
    for (int j = 1; j < vect[i].size(); j++)
    {
      if (vect[i][j].second >= vect[i][x].first)
      {
        x = j;
        count++;
      }
    }
  }
  return count;
}

int main()
{
  int n, m;
  cin >> n >> m;
  vector<vector<int>> arr[n][3];
  
   for(int i=0;i<n;i++)
   {
     for(int j=0;j<3;j++)
        cin>>arr[i][j];
   }
  return 0; 
}

Part of code where I have declared vector of pairs and initialised it

 vector<pair<int, int>> vect(n + 1);

  //departure and arrival respectively
  for (int i = 0; i < m; i++)
  {
    vect[arr[i][2]].push_back(make_pair(arr[i][1], arr[i][0]));
  }

Also,I know that my driver function is incomplete(I am planning to complete it after debugging max_trains function) ,but can anyone tell me why it is showing error at line no 52 cin>>arr[i][j]

  • Don't use "#include bits/stdc++.h" https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – ShadowMitia Jul 22 '21 at 18:14
  • `vector> arr[n][3];` is non-standard C++. And I somehow doubt that such VLAs work properly with C++ classes – UnholySheep Jul 22 '21 at 18:15
  • Why are you using 2d arrays of 2d vectors? That's 4 dimensions. Is that what you intended? – NathanOliver Jul 22 '21 at 18:15
  • It's an error because `arr[i][j]` is a `std::vector>`, and there is no suitable `>>` overload for that. See your C++ textbook for more information. By the way, in which C++ textbook did you learn about `bits/stdc++.h`? This is non-standard C++. Your C++ textbook must be a real bad textbook, and you should get a better one. – Sam Varshavchik Jul 22 '21 at 18:15
  • is ```bits/stdc++.h``` wrong?I find many references using it if you don't want to include each library particularly – coder_newbie Jul 22 '21 at 18:24
  • Yes it's wrong. Let me guess: all these "references" are only found on a bunch of random challenge/hacking/competition sites that have nothing more than lists of useless coding puzzles, instead of C++ tutorials and learning material, right? The only thing anyone learns there are bad programming habits, and non-standard C++. You will not find any examples using this non-standard header file, and non-standard C++ like variable-length arrays, in any reputable C++ textbook. Knowing which C++ function comes from which library is something that every experienced C++ developer needs to know. – Sam Varshavchik Jul 22 '21 at 18:30

1 Answers1

1

You have mixed array and vector, generally, we don't need to use c styles arrays when we have a vector, I have fixed your code to be compilable:

#include "bits/stdc++.h"
using namespace std;
bool compare(vector<int> arr1, vector<int> arr2) { return arr1[1] < arr2[1]; }
int max_trains(vector<vector<int>> arr, int n, int m) {
  vector<vector<pair<int, int>>> vect(n + 1);

  // departure and arrival respectively
  for (int i = 0; i < m; i++) {
    vect[arr[i][2]].push_back(make_pair(arr[i][1], arr[i][0]));
  }

  for (int i = 0; i <= n; i++) sort(vect[i].begin(), vect[i].end());

  int count = 0;
  for (int i = 0; i <= n; i++) {
    if (vect[i].size() == 0) continue;

    int x = 0;

    count++;
    for (int j = 1; j < vect[i].size(); j++) {
      if (vect[i][j].second >= vect[i][x].first) {
        x = j;
        count++;
      }
    }
  }
  return count;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> arr(n, std::vector<int>{3});

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) cin >> arr[i][j];
  }
  return 0;
}
prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42