Parallel one dimensional array

Arrays, regardless of dimension, can only store 1 type.

You have many options to consider when choosing how you will store this data.
I recommend using STL containers whenever possible, but I do not know the full requirements.

#include <iostream>
#include <array>
#include <vector>
#include <utility>
using namespace std;

static const size_t SIZE = 10;

struct Student{
  std::string str;
  int i;
};

int main() {

    //example using two arrays for each of the types:

    std::string names[SIZE];
    int ages[SIZE];

    //1 dimensional array examples:

    //C++ std array of paired type
    std::array<std::pair<std::string,int>,SIZE> students1;

    //C++ std array of struct data
    std::array<Student,SIZE> students2;

    //C style array of paired type
    std::pair<std::string,int> students3[SIZE];

    //C style array of struct data
    Student students4[SIZE];

    //vector examples:

    //use a vector to allow dynamic sizes
    std::vector<std::pair<std::string,int>> students5;
    std::vector<Student> students6;


}

Leave a Comment