Initialize a pair of vectors with a certain length and same numbers

111 views Asked by At

What I want to achieve is to initialize a pair of a vector with a certain length and a certain initialization number

I know that a vector can be initialized with the same elements:

std::vector v(length, number); 

and a pair:

std::pair<int> p(number, number);

so combining those two together I thought of:

std::pair<std::vector<int>, std::vector<int>> pv((length, number),(length, number));

Unfortunately this doesn't work

2

There are 2 answers

1
AudioBubble On BEST ANSWER
size_t length = 5;
int number = 0;
std::pair<std::vector<int>, std::vector<int>> pv(std::vector<int>(length, number), std::vector<int>(length, number));
1
leslie.yao On

You could use braces (list initialization) from C++11.

std::pair<std::vector<int>, std::vector<int>> pv({length, number}, {length, number});