lab9 - CSCI 101 Fundamentals of Computer Programming Lab 9...

This preview shows pages 1–3. Sign up to view the full content.

CSCI 101: Fundamentals of Computer Programming Lab 9: STL Vector Fall 2007 General Concepts Container A Container is an object that stores other objects (its elements). It can be a list of names (string type), a list of scores (float type), or a list of Employees (User defined class). A Container has methods for accessing its elements. Sequence A Sequence is a variable-sized Container whose elements are arranged in a strict linear order. It supports insertion and removal of elements, e.g. insert(), resize(), erase(). Vector Vector is the simplest of the STL container classes, and in many cases the most efficient. A vector is a Sequence which supports random access to elements. Vector supports insertion and removal of elements: At the end; At the beginning; In the middle; The number of elements in a vector may vary dynamically, but we do not need to worry about the memory management since it is automatic. Declare a vector to store integer values vector<int> V, V1; Operations on vector // Constructors vector<T> V; //Creates an empty vector of T's. vector<T> v(n); //Creates vector of n default values. vector<T> v(n, e); //Creates vector of n copies of e.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
vector<T> v(beg, end); //Creates vector with elements copied from range beg. .end. // Size associated functions int i; i = v.size(); //Returns number of elements in the list i = v.capacity(); //Max number of elements before reallocation i = v.max_size(); //Implementation max number of elements bool b; b = v.empty(); //True if empty v.reserve(n); //Sets capacity to n before reallocation // Altering elements v.insert(v.begin(), 3); // Insert 3 at the beginning v.push_back( 9 ); // Inserts a new element at the end v.pop_back() ; // Removes the last element v = v1; // Assigns v1 to
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/13/2008 for the course CSCI 101L taught by Professor Ghyam during the Fall '07 term at USC.

Page1 / 7

lab9 - CSCI 101 Fundamentals of Computer Programming Lab 9...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online