Thread: Array trying to be a vector

  1. #16
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    one last thing...
    Code:
    for(; i < m_Count; ++i)
             mp_List[i] = mp_List[i+1];
    I was under the impression that writing a for loop out like this was sloppy...clearly I'm in no position to even say ANYTHING about syntax or whatnot...it's helpful for me to know these kinds of things why not just use a while loop? Although, as we both are quite aware of, the for loop is the sexiest of all loops hahaha
    Last edited by Chaplin27; 03-03-2005 at 07:16 PM. Reason: sloppy wrapping

  2. #17
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    Oh hunter, one last thing, isn't i out of its scope in the nested for loop you provided in the updated code because it's defined in the first for loop as opposed to at the start of the function where it would be in scope throughout? Or is it within scope as long as it's inside the for loop where i is declared? If I'm bombarding you with questions...tell me so and I'll ask them in a whisper so you won't even know they're there
    Last edited by Chaplin27; 03-03-2005 at 08:14 PM.

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>does the new version look like this?:
    Assuming all elements are shifted forward properly, that's generally how it will look. However, as has been mentioned by grib, if you don't care about preserving the order of the elements (i.e. the array is just acting as a big collection of unsorted items), then it is more efficient to simply swap the element you're 'removing' with the last element, and then decrease the element count. That way the element you want deleted is removed from its position and moved to the 'back', where it will be safely ignored due to the decreased element count, and the element at the 'back' will be moved into the position vacated by the element you just removed so it will still be 'inside' the array.

    >>I was under the impression that writing a for loop out like this was sloppy
    Quite possibly. I think it's more a matter of style though (oooh, that 's' word!); when I see 'for', I think "this is going to loop for (the condition) number of times", but when I see 'while', I think "This is going to loop until for some reason (the condition) is false". Therefore, in this case, I used a 'for' because the loop should execute (from the current position to the end of the array) times. Also, when a for loop is of the form:
    Code:
    for(; condition; ++i)
    This, to me, is usually a clear indication that the loop is picking up where another has left off - in this case, it is continuing to the end of the array from where the outer loop has 'paused'. Admittedly, this isn't totally clear since it's continuing the outer loop from the inside; it would likely be more intuitive if the if statement checking for equality simply did break; and the nested loop were placed outside. Either way, however, the effect would be pretty much the same.

    isn't i out of its scope in the nested for loop you provided in the updated code because it's defined in the first for loop as opposed to at the start of the function where it would be in scope throughout? Or is it within scope as long as it's inside the for loop where i is declared?
    In very simplistic terms, an object is in scope as long as the innermost {} block it was defined inside is still open - literally. You can, in fact, do:
    Code:
    int main()
    {
       int i = 5;
       std::cout << i;
    
       {
          int j = 1;  //j temporarily brought into scope
          std::cout << j;
       }
    
       //j now out of scope
    
       return 0;
    }
    Of course, it's a *little* different in for loops:
    Code:
    //i is not in scope
    for(int i = 0; i < 5; ++i)  //i is in scope
    {
       //i is in scope
    }
    //i is not in scope
    As you can see, the variiable declared in () acts as if it were declared inside the {}. Similarly:
    Code:
    //i is out of scope
    for(int i = 0; i < 5; ++i)  //i is in scope
       std::cout << i;   //i is in scope
    //i is out of scope
    So basically, i is in scope as long as the code block it was declared in is still open. Since the code block in this example is a single line, i is in scope for that single line.

    **EDIT**
    Oops. Well, to answer the question :
    i is still in scope because, as you guessed, the code block it was defined inside of (the outer for loop) hasn't ended yet.
    Last edited by Hunter2; 03-03-2005 at 09:00 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    awesome hunter....if I could boost your rep again I would! haha your info has been beyond helpful. It should also be noted that this has been the longest thread I've started which I thought was kinda cool...I look forward to more posts and responses from you. Take it easy-Chap

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM
  5. How can I convert a vector into an array??
    By shanedudddy2 in forum C++ Programming
    Replies: 6
    Last Post: 01-22-2003, 12:15 PM