std::vector

Posted: October 17, 2011 in C/C++, Programming
Tags: , ,

vector, in C++ can act as a ArrayList,  similar to Java or C#. vector itself is included as a part of STL. In this article i just introduce some basic usage of vector, the rest can read here.

Here’s the instantiation of vector:

std::vector<AnObject*> objectList;

To add a member, we can use push_back()

objectList.push_back(anObject);

vector’s size it very simple to be obtained:

objectList.size();

The fun part of using vector when we’re accessing its member, it’s same as we use array :D

objectList[a]->aMethod();

To delete a member, it is little different, we should use iterator:

objectList.erase(objectList.begin() + a);

When a is the index of a member we want to delete and objectList.begin() is a method to obtain the initial iterator.

Comments
  1. picogenkaku says:

    please provide some examples. :) thanks ^^

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s