简体   繁体   中英

Multidimensional Vector?

So I recently started learning C++ and I'm trying to figure out how to access a multidimensional vector or a vector with vectors stored inside of it. I've looked all over and I can't find exactly what I'm looking for. I want to be able to test the contents of each vector inside of the multidimensional vector by printing them out. Also whenever I try to see the size of the vector after each iteration, I'm getting random constants for each iteration. They look like they may be memory locations but I'm not sure.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
int numofArrays;
int numofQueries;

vector<vector<int>> arr(numofArrays);

cin >> numofArrays;
cin >> numofQueries;

int arrSize;
int number;
vector<int> indArr; 

// Outer loop, appends vectors containing ints to a multidimensional vector
for (int i = 0; i < numofArrays; i++) {
    cin >> arrSize; // Getting number of elements from user

    // Inner loop, gets user inputted values then creates a vector which is added to the multidimensional vector
    for (int x = 0; x < arrSize; x++) {
        cin >> number; 
        indArr.push_back(number); 
        cout << "Last number added to vector: " << number << endl; // Checking to see if correct numbers are being added. 
    }

    arr.push_back(indArr);
    cout << "Multidimensional Vector size: " << arr.size() << endl; // Checking size of vector after each iteration
    indArr.clear(); // Empties vector for next iteration
}


  return 0;
}

As I am very new to C++, I welcome constructive criticism.

Newly Revised Code:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
int numofArrays;
int numofQueries;


cin >> numofArrays;
cin >> numofQueries;

vector<vector<int>> arr(numofArrays);


// Outer loop, appends vectors containing ints to a multidimensional vector
for (int i = 0; i < numofArrays; i++) {
    int arrSize;
    vector<int>indArr;
    cin >> arrSize; // Getting number of elements from user
    indArr.resize(arrSize); // Resizing array for next values
    // Inner loop, gets user inputted values then creates a vector which is added to the multidimensional vector
    for (int x = 0; x < arrSize; x++) {
        int number;
        cin >> number; 
        indArr.push_back(number); 
        cout << "Last number added to vector: " << number << endl; // Checking to see if correct numbers are being added. 
    }

    arr.push_back(indArr);
    cout << "Multidimensional Vector size: " << arr.size() << endl; // Checking size of vector after each iteration
}

int test = arr[0][0];
cout << test;



  return 0;
}

Just some tips:

vector<vector<int>> arr(numofArrays); constructs a vector of vectors of size numofArrays . But numofArrays is not yet read from cin, so it is an undefined number. Care with the order of operations.

arr.push_back(indArr); adds a vector at the end of arr.

Hence, in your loop, even if you fix the order of reading numofArrays and declaring arr , you start with an array of size numofArrays , and each cycle you add an element. You will end up with arr holding 2 times numofArrays elements.

Also, indArr is a temporary variable, which you reset each cycle. Although there is nothing wrong about what you are doing, in general is better to keep each variable limited to its scope. If you define indArr inside the loop, it will be automatically cleared and re-created. May not be the most efficient way to do things in all cases, but I'd leave this details for later.

Something like this should work for you:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
  int numofArrays;
  int numofQueries;

  cin >> numofArrays; 
  cin >> numofQueries; // this is an unused variable

  vector<vector<int> > arr;

  // Outer loop, set vectors containing ints to a multidimensional vector
  for (int i = 0; i < numofArrays; i++) {
    int arrSize;
    vector<int> indArr; 
    cin >> arrSize; // Getting number of elements from user

    // Inner loop, gets user inputted values then creates a vector which is added to the multidimensional vector
    for (int x = 0; x < arrSize; x++) {
      int number;
      cin >> number; 
      indArr.push_back(number); 
        cout << "Last number added to vector: " << number << endl; // Checking to see if correct numbers are being added. 
    }

    arr.push_back(indArr);
    cout << "Multidimensional Vector size: " << arr.size() << endl; // Checking size of vector after each iteration
  }

  return 0;
}

The way you're inspecting the size is correct. For example in your code you're printing the integer number of elements in the first dimension of the array arr .

The odd results you're getting is because of how you're initializing the vector. In the constructor for arr there you specify a size describing the number of default-constructed elements to fill the vector with. However you're providing numofArrays which is an undefined value. Hence you receive random looking vector sizes when you print this out.

The real size of the vector arr 's first dimension will be whatever this undefined value is plus one, due to the push_back operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM