Presentation is loading. Please wait.

Presentation is loading. Please wait.

Vectors. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically.

Similar presentations


Presentation on theme: "Vectors. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically."— Presentation transcript:

1 Vectors

2 Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically resized Constant time insertion or removal at the end Linear time ins/rem elsewhere

3 Example #include using namespace std; … vector v; assert(v.size == 0); v.insert(v.begin(), 3); assert(v.size() == 1);

4 Default constructor #include using namespace std; … /* create an empty vector v of specified type */ vector v;

5 Copy constructor #include using namespace std; … /* create an vector v1 which is a copy of v (types must be the same) */ vector v1(v);

6 Sized constructor #include using namespace std; … /* create an vector v which of specified size, using the default constructor */ vector v1(size);

7 Initialized constructor #include using namespace std; … /* create an vector v which of specified size, using the supplied value */ vector v1(size, value);

8 Range constructor #include using namespace std; … /* create an vector v of the specified size, us values all values from begin to end */ vector v(begin, end);

9 Removing elements v.clear();// deletes all elements v.erase(pos);// deletes element at pos

10 Inserting elements // insert val at pos v.insert(pos, val); // insert n copies of val at pos v.insert(pos, n, val); // a copy of v.beg.. v.end-1 is inserted // at pos v.insert(pos, beg, end);

11 Operations for all containers Default constructor /* Initialize the object to an empty state */

12 Parameterized constructors /* Various specialized ways to specify the initial contents of the container */

13 Copy constructors /* A container of the same type must be passed by value. */

14 destructor /* Executes when the container goes out of scope */

15 c.empty() /* Returns true if c is empty, false otherwise. */

16 c.size() /* Returns the number of elements in c. */

17 c.max_size() /* Returns the number of elements that can be inserted into c. */

18 c.swap(c1) /* Swaps the elements c and c1 */

19 c.begin() /* Returns an interator to the first element in c. */

20 c.end() /* Returns an interator succeeding the last element in c. */

21 c.rbegin() /* (reverse) Returns an interator pointing to the last element in c. */

22 c.rend() /* (reverse) Returns an interator pointing one past the first element in c */

23 c.insert(pos, val) /* Inserts val into pos (pos is an iterator) */

24 c.erase(begin, end) /* Deletes elements from begin to end-1 */

25 c.clear() /* Deletes all elements */

26 Operators /* =, ==, !=,, >=x */


Download ppt "Vectors. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically."

Similar presentations


Ads by Google