+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 13

Thread: On Learning the STL: Chapter 1 (Vectors) - Part 1

  1. #1
    Moderator ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,805
    Blog Entries
    41

    On Learning the STL: Chapter 1 (Vectors) - Part 1

    Vectors are a powerful STL container format that closely resembles an array, however is much more useable and intuitive. Vectors allow you to insert, remove, pop, push, change, and resize the array, making it very powerful indeed. It's what C standard arrays would be if they weren't terrible.

    So how do you use a Vector? Easy, just do this:
    Code:
    #include <vector>
    
    int main(){
        std::vector<int> myVector;
        return 0;
    }
    That simple code includes the vector header file, and declares a std::vector for use in your program. The "<int>" part of that vector is because a vector is a class template, which means it can be used to produce an array of any class object you want, from simple int's, pointers, std::strings or even other vectors! Let's start using a vector, and see what we can make!
    Code:
    #include <vector>
    #include <iterator>
    #include <iostream>
    #include <cstdlib>
    
    int main(int argc, char *argv[]) {
        using namespace std;
    
        int aval;
        if (argc == 2) {
            aval = atoi(argv[1]);
        } else {
            cout << "You must input one argument and only one argument!" << endl;
            return 0;
        }
        // Sanity check that input variable...
        if (aval < 1) {
            cout << "Please input a number greater than or equal to 1 to "
                 << "continue." << endl;
            return 0;
        }
    
        // Now let's create and initialize a vector.
        vector<int> myVector;
    
        for (int iii = 0; iii < aval; ++iii)
            myVector.push_back(iii + 1);
    
        // Now let's print out the value of that vector!
        for (int i = 0; (unsigned int)i < myVector.size(); ++i) 
            cout << myVector[i] << " ";
    
        cout << endl;
    
        cout << std::endl;
        return 0;
    }
    (You should be familiar with atoi and accepting command line statements) This code, when run, accepts a command line argument. Go ahead and build it, then, in the command line, enter "{PROGNAME} 25" and it will print out a string of numbers counting from one to 25.

    You may notice that there is no dynamic memory allocation necessary in this program. That is because the vector class template already has that functionality built-in to it, so there's no need to resize it ourselves, we just call "push_back" and vector will do that for us. push_back is a function that adds the value given to it as an argument to the end (or back) of the std::vector. There's a lot more functionality with vectors, so let's try another loop, this time removing each even number from the vector before printing it.
    Code:
    // Now let's create and initialize a vector.
        vector<int> myVector;
    
        for (int iii = 0; iii < aval; ++iii)
            myVector.push_back(iii + 1);
    
        // Remove even numbers, if any.
        for (vector<int>::iterator myIter = myVector.begin();
                                     myIter < myVector.end();
                                                  ++myIter) {
            if (*myIter % 2 == 0)
                myVector.erase(myIter);
        }
    
        // Now let's print out the value of that vector!
        for (int i = 0; (unsigned int)i < myVector.size(); ++i) 
            cout << myVector[i] << " ";
    What we did here was we simply added an additional for loop that initialized a vector iterator, which counted through the vector, and if the value with which the iterator was pointing to, if divided by 2, had a remainder of 0 (IE it was even), then that vector erased the number at the location of myIter.

    That's where the interesting part is, std::vectors also have the ability to erase certain values from within them, then dynamically resize themselves as if the erased value wasn't there at all (this would be very difficult with C arrays). The way this is done is the erase function is provided an iterator that points to the value in the vector array you want to erase, then the function deletes it and moves all of the values ahead of it back one. Go ahead and build that and try it!

    This is just part one on a chapter called "Vectors" for a series On Learning the STL. In the second part we'll take a closer look at iterators and just what they are, as well as how useful they can be, since as you can see, they're going to be used a lot! The attachment contains a copy of the complete code you can build from.

    - Zeke
    Bijgevoegde bestanden
    Last edited by ZekeDragon; 08-11-2009 at 04:51 PM.

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    13,155
    Blog Entries
    59

    Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

    Very nice. +rep.
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Newbie mindblaster is an unknown quantity at this point mindblaster's Avatar
    Join Date
    Jul 2009
    Posts
    17

    Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

    nice lecture

  4. #4
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

    Vectors are powerful, nicely written. +rep

  5. #5
    Guru MathX has a spectacular aura about MathX has a spectacular aura about MathX's Avatar
    Join Date
    Oct 2008
    Location
    Kosovo
    Age
    19
    Posts
    4,007

    Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

    Awesome! +rep

  6. #6
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,042

    Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

    Very nice.

    The way this is done is the erase function is provided an iterator that points to the value in the vector array you want to erase, then the function deletes it, moves all of the values ahead of it back one, then resizes the array. Go ahead and build that and try it!
    The thing to note with the erase function is if you do this operation often on big vectors it is going to produce an unwanted overhead on your program and you should use a linked structure. For small vectors this method is good if it is not used a lot.


    Regarding your code to output the vector. You can also use the [] operator to output items. Since the vector is a direct access container this code below would also work.

    Code:
    for (int i=0;i<v.size();i++) {
           cout << v[i] << " ";
    }
    cout << endl;

  7. #7
    Moderator ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,805
    Blog Entries
    41

    Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

    @chili5:

    True, there is an overhead when using the erase method, however for the size of an array I made it was negligible. The optimal way to do this would probably be to simply iterate through the vector and cout the value if (*myIter % 2) == 0 returns false.

    Quote Originally Posted by chili5 View Post
    Regarding your code to output the vector. You can also use the [] operator to output items. Since the vector is a direct access container this code below would also work.

    Code:
    for (int i=0;i<v.size();i++) {
           cout << v[i] << " ";
    }
    cout << endl;
    Good point, and it probably would have been preferable to use this syntax since we hadn't covered iterators in great detail. I'll go ahead and change it, but change it: "for (int i = 0; (unsigned int)i < v.size(); ++i)" since "i < v.size()" returns a compiler warning and I have allergies to compiler warnings.
    Should I get a userbar here?

  8. #8
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,042

    Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

    Great!

    i < v.size() shouldn't return a compile warning though.

    I think iterators should be a section all on there own. They can be a bit confusing.

  9. #9
    Moderator ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon is a splendid one to behold ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,805
    Blog Entries
    41

    Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

    Code:
    i < v.size()
    _____________________
    
    >> Warning: Comparison between signed and unsigned integer expressions.
    I deal with it a lot when using for loops. And check the next section, it was supposed to be mostly about iterators, instead I ended up covering constructors, memory management within vectors, and THEN iterators, and I didn't even get all of iterators yet. >_<
    Should I get a userbar here?

  10. #10
    Learning Programmer Sallyqq is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    30

    Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

    nice tutorial

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Validation - Part 2 - Client-Side
    By Kernel in forum News
    Replies: 0
    Last Post: 04-27-2009, 10:40 PM
  2. Replies: 0
    Last Post: 12-25-2008, 05:11 AM
  3. Learning On A Project
    By Kernel in forum News
    Replies: 0
    Last Post: 10-10-2008, 03:10 AM
  4. ROBOT ASSEMBLY-- C language
    By hummer350 in forum C and C++
    Replies: 3
    Last Post: 07-31-2008, 07:03 PM
  5. Replies: 0
    Last Post: 05-05-2008, 04:43 AM