Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials
Register Blogs Search Today's Posts Mark Forums Read

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-31-2009, 10:34 AM
ZekeDragon's Avatar
Code Warrior
 
Join Date: Jul 2009
Location: Nowhere, Washington
Posts: 1,721
ZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to all
Send a message via AIM to ZekeDragon Send a message via MSN to ZekeDragon Send a message via Skype™ to ZekeDragon
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
Attached Files
File Type: zip vectorpart1.zip (590 Bytes, 12 views)

Last edited by ZekeDragon; 08-11-2009 at 08:51 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-31-2009, 11:30 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

Very nice. +rep.
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-09-2009, 01:37 PM
mindblaster's Avatar
Newbie
 
Join Date: Jul 2009
Posts: 17
mindblaster is an unknown quantity at this point
Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

nice lecture
__________________
You can read Free Articles
Discount Coupons & YesStyle Discount Coupons
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-09-2009, 06:45 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

Vectors are powerful, nicely written. +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-10-2009, 03:57 AM
MathX's Avatar
Guru
 
Join Date: Oct 2008
Location: Kosovo
Age: 19
Posts: 3,994
MathX has a spectacular aura aboutMathX has a spectacular aura about
Send a message via MSN to MathX
Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

Awesome! +rep
__________________
My Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-11-2009, 08:20 PM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

Very nice.

Quote:
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;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-11-2009, 08:31 PM
ZekeDragon's Avatar
Code Warrior
 
Join Date: Jul 2009
Location: Nowhere, Washington
Posts: 1,721
ZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to all
Send a message via AIM to ZekeDragon Send a message via MSN to ZekeDragon Send a message via Skype™ to ZekeDragon
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.
__________________
On Hiatus...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-11-2009, 08:48 PM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-11-2009, 08:54 PM
ZekeDragon's Avatar
Code Warrior
 
Join Date: Jul 2009
Location: Nowhere, Washington
Posts: 1,721
ZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to all
Send a message via AIM to ZekeDragon Send a message via MSN to ZekeDragon Send a message via Skype™ to ZekeDragon
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. >_<
__________________
On Hiatus...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-15-2009, 11:29 AM
Learning Programmer
 
Join Date: Aug 2009
Posts: 30
Sallyqq is an unknown quantity at this point
Re: On Learning the STL: Chapter 1 (Vectors) - Part 1

nice tutorial
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation - Part 2 - Client-Side Kernel News 0 04-28-2009 02:40 AM
Using ExportProvider to customize container behavior Part I Kernel News 0 12-25-2008 08:11 AM
Learning On A Project Kernel News 0 10-10-2008 07:10 AM
ROBOT ASSEMBLY-- C language hummer350 C and C++ 3 07-31-2008 11:03 PM
Adventures in F# - F# 101 Part 9 (Control Flow) Kernel News 0 05-05-2008 08:43 AM


All times are GMT -5. The time now is 10:29 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0