Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Coding style

C++ Coding style

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionalgorithmsdata-structuresdiscussion
4 Posts 3 Posters 0 Views 1 Watching
  • 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.
  • E Offline
    E Offline
    Emilio Guijarro
    wrote on last edited by
    #1

    Hi guys, here is a question from a C++ programming course. We have to implement the BubbleSort algorithm over a STL list. As we know, STL lists haven´t got random access iterators, so we can only increment and decrement the iterators (we can´t use expressions such as "i = i-1"). What implementation is better? a) Using an additional function to get the iterator pointing to the previuos element: template inline T Prev(const T& i) { T j = i; return --j } template void Bubble(T Begin, T End) { for(T i = Begin, i != End, i++) { for(T j = Prev(End); j!=i; j--) { if(*j < *(Prev(j)) swap(*j, *(Prev(j))); } } } b) Do not use any adittional function, and just use "temporal" stack variables. Guess the implementation ;P I say that "a" is better, my taecher and my best friend think that "b" is better. What do you think? Thanks. "When I look into your eyes, there´s nothing there to see, nothing but my own mistakes staring back at me"

    J 1 Reply Last reply
    0
    • E Emilio Guijarro

      Hi guys, here is a question from a C++ programming course. We have to implement the BubbleSort algorithm over a STL list. As we know, STL lists haven´t got random access iterators, so we can only increment and decrement the iterators (we can´t use expressions such as "i = i-1"). What implementation is better? a) Using an additional function to get the iterator pointing to the previuos element: template inline T Prev(const T& i) { T j = i; return --j } template void Bubble(T Begin, T End) { for(T i = Begin, i != End, i++) { for(T j = Prev(End); j!=i; j--) { if(*j < *(Prev(j)) swap(*j, *(Prev(j))); } } } b) Do not use any adittional function, and just use "temporal" stack variables. Guess the implementation ;P I say that "a" is better, my taecher and my best friend think that "b" is better. What do you think? Thanks. "When I look into your eyes, there´s nothing there to see, nothing but my own mistakes staring back at me"

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      Benchmark it. And realize that you're arguing about a bubble sort! Who cares how you implement it?

      A 1 Reply Last reply
      0
      • J Joe Woodbury

        Benchmark it. And realize that you're arguing about a bubble sort! Who cares how you implement it?

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        The question is not about the algorithm, is about correct implementation. Thanks anyway.

        J 1 Reply Last reply
        0
        • A Anonymous

          The question is not about the algorithm, is about correct implementation. Thanks anyway.

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          Anonymous wrote: is about correct implementation. There's no such thing--if it works. (Except in this case there may be implementation that will get you an 'A' and implementation that won't.)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups