Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Give me an example for using vector in C

I just want to know how to use a vector in my C program.

urgent help me

2 Solutions
Hi Priyashree,

with use of the STL header files, you can achieve Maps, such as Vectors. Here's an example:

 #include <vector>
 vector<int> v(10);    // 10 elements of memory reserved (for now at least) - to store int objects
You can then use the following method calls to customize & use the vector:

  insert()                 // Insert one of more objects
  push_back()          // Add the object passed as an argument to the end of the vector
  erase()                 // Erase one or more elements
  clear()                  // Erase all elements

Hope that helps.
Andrew BeersTechnology LeadCommented:
to expand on his explination

vector<Type> aVec; Declare aVec as an empty vector for storing Type values
vector<Type> aVec(n); Declare aVec as a vector containing n default values of type Type
vector<Type> aVec(n, val); Declare aVec as a vector containing n copies of value val  
aVec[i] Access the character in aVec whose index is i (without checking that i is valid)
aVec.at(i) Access the character in aVec whose index is i (checking that i is valid)
aVec.size() Return the number of values in aVec
aVec.capacity() Return the number of values aVec can store
aVec.reserve(n) Change the capacity of aVec to n
aVec.push_back(value); Append value at aVec's end
aVec.pop_back(); Erase the last value in aVec
aVec.front() Return a reference to the first element of aVec
aVec.back() Return a reference to the last element of aVec
aVec = vec2 Make aVec a copy of vec2
aVec.clear() Erase all of the values in aVec

assume aVec is a declared vector.  There are more functions you can use and params that go inside then and which params are used where.

Hope that broadens your knowledge a bit more.

For an excellent, simple reference on the STLs, go to:


One thing that I found a little tricky at first was the indirection used by the iteration scheme.  To loop through a vector of objects and act on each one, you would do something like this:

class Thingy {
    void DoSomething();

typedef std::vector<Thingy *> ThingyVector;

// Let's assume we have a pointer to a ThingyVector, pv that is full of Thingys.
// If not, it could be made like this:
// ThingyVector *pv = new ThingyVector;
// for( int i = 0; i < 10; i++ ) {
//   pv->pushback( new Thingy );
// }

ThingyVector *pv;
ThingyVector::iterator pi;

for( pi = pv->begin(); pi != pv->end(); pi++ ) {

//  (*pi)  is now a pointer to a ThingyVector, so you could:


Remember, if you use all pointers as I have done, you are responsible for deleting the vector and the objects.  If the objects are used only from within the vector, you can implement a desctructor the destroys the member objects, thus:

ThingyVector::~ThingyVector() {
  ThingyVector::iterator pi;
  for( pi = pv->begin(); pi != pv->end(); pi++ ) {
    if( *pi ) delete (*pi);


Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now