mardi 4 mai 2021

How to print contents of a vector that has its size declared

I can print the contents of a vector normally when it doesn't have its size declared with a simple 'cout <<', like this :

#include <iostream>
#include <vector>

using namespace std;

int main(){

   int N, T;

   cin >> N;
   vector<int> myvector;

   for(int i=0; i < N; i++) {
    cin >> T;
    myvector.push_back(T);
    }

    for(int i=0; i < N; i++) {

        cout << myvector[i] << endl;
        }
}

But when i add a size to the vector, it just doesn't work anymore. Here's an exemple :

#include <vector>

using namespace std;

int main(){

   int N, T;

   cin >> N;
   vector<int> myvector[N];

   for(int i=0; i < N; i++) {
    cin >> T;
    myvector[i].push_back(T);
    }

    for(int i=0; i < N; i++) {

        cout << myvector[i] << endl;
        }
}

I've been searching for a while, but i haven't found an answer to this. I suppose that it's because the key words are too common, so i can't find a solution to this exact problem.

Aucun commentaire:

Enregistrer un commentaire