Home > Enterprise >  Why can't we use square brackets in the code below?
Why can't we use square brackets in the code below?

Time:10-02

vector<int> mergeKSortedArrays(vector<vector<int>*> input) {
      vector<int> ans;
      //min priority queue
      priority_queue<int, vector<int>, greater<int>> pq;
      for(int i = 0; i < input.size(); i  ){
        for(int j = 0; j < input[i] -> size(); j  ){
           pq.push(input[i][j]); //THIS LINE
         }
       }
       while(!pq.empty()){
          ans.push_back(pq.top());
          pq.pop();
       }
       return ans;
     }

The main function that calls this function mergeKSortedArrays

int main() {
        int k;
        cin >> k;
        vector<vector<int> *> input;
        for (int j = 1; j <= k; j  ) {
          int size;
          cin >> size;
          vector<int> *current = new vector<int>;
          for (int i = 0; i < size; i  ) {
             int a;
             cin >> a;
             current->push_back(a);
            }
           input.push_back(current);
          }

         vector<int> output = mergeKSortedArrays(input);

         for (int i = 0; i < output.size(); i  ) {
            cout << output[i] << " ";
         }
        return 0;
    }

Can anyone tell why using input[i][j] inside the first loop giving an error? What i know of how square brackets work is that it just goes to the address and dereferences. So I don't see why using square brackets will be a problem. Input[i] takes me to the vector pointer and then Input[i][j] takes me to the address of that vector and dereference. Like input[i][j] =*(address of the vector j). One more thing guys, i'm able to do input[i] -> at(j). It compiles without a problem. But isn't isn't input[i][j] same as input[i] -> at(j) unless i'm doing an illegal access of the memory.

CodePudding user response:

With this declaration:

vector<vector<int>*> input;

input is not a vector of int vectors but it's a vector of pointers to int vectors.

Therefore you need this: (*input[i])[j]

  • input[i] is a pointer to vector<int>
  • *input[i] is a vector<int>
  • (*input[i])[j] is an int

This being said, you should not use pointers in the first place but simply use a vector<vector<int>> (vector of int vectors). Then you can use input[i][j].

Complete code:

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

vector<int> mergeKSortedArrays(vector<vector<int>> input) {
  vector<int> ans;
  //min priority queue
  priority_queue<int, vector<int>, greater<int>> pq;
  for (int i = 0; i < input.size(); i  ) {
    for (int j = 0; j < input[i].size(); j  ) {
      pq.push(input[i][j]); //THIS LINE
    }
  }
  while (!pq.empty()) {
    ans.push_back(pq.top());
    pq.pop();
  }
  return ans;
}

int main() {
  int k;
  cin >> k;
  vector<vector<int>> input;
  for (int j = 1; j <= k; j  ) {
    int size;
    cin >> size;
    vector<int> current;
    for (int i = 0; i < size; i  ) {
      int a;
      cin >> a;
      current.push_back(a);
    }
    input.push_back(current);
  }

  vector<int> output = mergeKSortedArrays(input);

  for (int i = 0; i < output.size(); i  ) {
    cout << output[i] << " ";
  }
  return 0;
}

NB:
This code compiles, but I didn't check if it actually works as intended by you.

Bonus:
The signature of mergeKSortedArrays should be

vector<int> mergeKSortedArrays(const vector<vector<int>> & input)`

this will avoid an unnecessary copy of the vector upon calling mergeKSortedArrays.

CodePudding user response:

Hopefully a simple example will show the the difference between vector<vector<int>> and vector<vector<int>*>

#include <vector>
#include <iostream>

int main(){
    std::vector<int> v0 = {1, 2, 3};
    std::vector<int> v1 = {1, 2, 3};
    std::vector<std::vector<int>> vv = {v0, v1};
    std::vector<std::vector<int> *> vpv = {&v0, &v1};
    v0[0] = 4;
    v0[1] = 5;
    v0[2] = 6;

    std::cout
    << ' ' << v0[0] 
    << ' ' << v0[1]
    << ' ' << v0[2]
    << ' ' << v1[0] 
    << ' ' << v1[1]
    << ' ' << v1[2]
    << "\n" 
    << ' ' << vv[0][0] 
    << ' ' << vv[0][1]
    << ' ' << vv[0][2]
    << ' ' << vv[1][0] 
    << ' ' << vv[1][1] 
    << ' ' << vv[1][2] 
    << "\n"
    << ' ' << vpv[0][0][0] 
    << ' ' << vpv[0][0][1]
    << ' ' << vpv[0][0][2]
    << ' ' << vpv[1][0][0] 
    << ' ' << vpv[1][0][1]
    << ' ' << vpv[1][0][2]
    << "\n"; }

And the printed result is:

4 5 6 1 2 3
1 2 3 1 2 3
4 5 6 1 2 3
  • Related