# filling a vector with pairs

• hi there,

I'm filling this vector with pages of a book.

``````    float mid = (float)n / 2; // n = the number of pages
int pages = 0;
std::vector<pair<int, int>> pagesVec;
for(int i = 0; i = mid; i++){
if(pages >= n){
pagesVec.emplace_back(pages , 0); // books holds an even number of pages, an empty page should be added.
break;
}
else{
pagesVec.emplace_back(pages , pages++);
pages++;
}
}
``````

the problem is in the for loop definition.

``````for(int i = 0; i < n; i++){ // adds to many pages
for(int i = 0; i < mid; i++){ // works pretty good, but ...
for(int i = 0; i = mid; i++){
``````

this is how it should look, but depending on the number of a pages a book has.
(empty,1)(2,3)(4,5) < --- odd number of pages, no empty page on the back.
or
(empty,1)(2,3)(4,5)(6.empty) < --- even number of pages, zero denotes empty page

would anyone know a better way or more efficient to fill the vector of pairs with pages?

4 page book looks like this (good):

``````// using for(int i = 0; i = mid; i++){
first;1second: 0
first;3second: 2
first;4second: 0
``````

a 5 page looks like this (good):

``````// using for(int i = 0; i < mid; i++){
first;1second: 0
first;3second: 2
first;5second: 4
``````

a 5 page looks like this (bad):

``````// using for(int i = 0; i = mid; i++){
first;1second: 0
first;3second: 2
first;5second: 4
first;6second: 0 // to much
``````

a 5 page looks like this (bad):

``````// using for(int i = 0; i = n; i++){
first;1second: 0
first;3second: 2
first;5second: 4
first;6second: 0

``````

p.s. i don't get it, why the first pair, first value is 1 and the second is 0 "pages++"

kind regards.

• @Natural_Bugger
I'm sorry I am not the brightest at understanding what people are trying to do from their explanations, and I don't get whatever your problem is here, but here are some observations about what is wrong in what you show:

• `float mid = (float)n / 2; // n = the number of pages`: There are never a "floating number of pages" in a book! Please make `mid` an `int`, you should have no `float`s in your code. Depending on which way you want to round an odd or even number, you probably want `int mid = (n + 1) / 2` (round an odd up to half of the next even).

• `for(int i = 0; i = mid; i++)`: `i = mid` can never be correct. The middle argument in a `for()` must always be a condition. `for(int i = 0; i < mid; i++)` looks better.

• `pagesVec.emplace_back(pages , pages++);`: You must never do this in C/C++. You cannot tell whether the first occurrence of `pages` here has the value of `pages` before or after the second argument, `pages++`, has been evaluated.

I suspect you're making something much more complex than is needed? Why any `mid` at all? Don't you just want something like:

``````for (int i = 1; i < n; i += 2)
pagesVec.emplace_back(i - 1, i);
``````

EDIT let me see if I can understand what you want:

(empty,1)(2,3)(4,5) < --- odd number of pages, no empty page on the back.
(empty,1)(2,3)(4,5)(6.empty) < --- even number of pages, zero denotes empty page

In the first case `n == 5`, in the second `n == 6`. So one way would be:

``````if (n > 0)
for (int i = 1; i <= n + 1; i += 2)
pagesVec.emplace_back(i - 1, (i <= n) ? i : 0);
``````

• @JonB said in filling a vector with pairs:

``````for (int i = 1; i <= n + 1; i += 2)