Caitlyn T
Caitlyn T

Reputation: 33

Vector Element Size

For the following code, vv[1].size() will return an output of 4. I was wondering where this number came from.

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<vector<int>> vv;
    vector<int> v1(3,5);
    vector<int> v2(4,7);
    vv.push_back(v1);
    vv.push_back(v2);
    cout << vv.size() << endl << vv[1].size() << endl;
}

Upvotes: 1

Views: 278

Answers (1)

jeff544
jeff544

Reputation: 51

This is because vector<int> v2(4,7); creates a vector of size 4 whose values are all 7. You most likely meant to write vector<int> v2 {4,7}; which creates a vector with 2 elements of 4 and 7.

Upvotes: 2

Related Questions