I am currently working through C++ Primer 5th edition. In chapter 3, Exercise 3.42, I am asked to "Write a program to copy a vector of int's into an array of int's."
the below code works. However I have a few questions.
1) on line 16, int arr[5]; initializes an array with 5 elements. How can I alter the array so that it automatically gets/has the same size as the vector?
2) Is there a simpler way to write the program with what the book has taught so far?
//Exercise 3.42
#include "stdafx.h"
#include <iostream>
#include <vector>
using std::vector;
using std::begin;
using std::endl;
using std::cout;
using std::end;
int main(){
vector<int> ivec = { 1, 2, 3, 4, 5 }; //initializes vector ivec.
int arr[5]; //initializes array "arr" with 5 null elements.
int i1 = 0; // initializes an int named i1.
for (vector<int>::iterator i2 = ivec.begin(); i2 != ivec.end(); ++i2){
arr[i1] = *i2; //assigned the elements in ivec into arr.
++i1; // iterates to the next element in arr.
}
for (int r1 : arr){ //range for to print out all elements in arr.
cout << r1 << " ";
}
cout << endl;
system("pause");
return 0;
}