Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. filling a vector with pairs
Forum Updated to NodeBB v4.3 + New Features

filling a vector with pairs

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 926 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • N Natural_Bugger

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 floats 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);
      
      1 Reply Last reply
      1
      • N Offline
        N Offline
        Natural_Bugger
        wrote on last edited by
        #3

        @JonB said in filling a vector with pairs:

        thnx very much for your answer

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

        that solved the issue

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved