lundi 27 novembre 2017

Printing contents of a vector

I am trying to get a feel for C++, and I want to either print the contents of a vector, or, to confirm my program is correct, I could add the contents of my vector and print the result.

Here is my code:

#include <iostream>
#include <vector>

using std::vector;
using std::cin;
using std::cout;

int main(){
  int n;
  vector<int> result;
  cin >> n;
  vector<int> numbers(n);
  for(int i = 0; i < n; ++i){
    cin >> numbers[i];
  }
  result = numbers;
  cout << result;
  return 0;
}

I found some solutions online for printing a vector, but I didn't understand what the code was doing, so I am hoping someone can help.

Aucun commentaire:

Enregistrer un commentaire