vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second

Similar documents
Standard Template Library. Outline

EL2310 Scientific Programming

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

STL Containers, Part I

Templates and Vectors

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

STL: C++ Standard Library

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

Template based set of collection classes STL collection types (container types)

C++ Lab 04 - File I/O, Arrays and Vectors

Standard Template Library. Outline

SSE2034: System Software Experiment 3

Lectures 11,12. Online documentation & links

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

EL2310 Scientific Programming

Data Structures CSci 1200 Test 2 Questions

Vector. Vector Class. class Vector { public: typedef unsigned int size_type;

The Standard Template Library Classes

STL Standard Template Library

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

Chapter 17: Linked Lists

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

Standard Library Reference

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

by Pearson Education, Inc. All Rights Reserved. 2

Module 9. Templates & STL

STL Exception Handling Contract

CS197c: Programming in C++

The Standard Library. C Library Wrappers. The Standard Library. 1. General. Wolfgang Schreiner 2. Sequence Containers

Chapter 17: Linked Lists

Concepts for the C++0x Standard Library: Containers

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

Function Templates. Consider the following function:

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Abstract Data Types 1

Advanced Programming in C++ Container Design I

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Abstract Data Types 1

CS 103 Unit 12 Slides

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

Lecture on pointers, references, and arrays and vectors

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

2 is type double 3 auto i = 1 + 2; // evaluates to an 4 integer, so it is int. 1 auto d = 5.0; // 5.0 is a double literal, so it

Exercise 6.2 A generic container class

The Standard Library

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Practice question Answers

Array Elements as Function Parameters

const int length = myvector.size(); for(int i = 0; i < length; i++) //...Do something that doesn't modify the length of the vector...

Overview. Part II: Containers and Algorithms. Overview. Overview

CS11 Advanced C++ Spring 2018 Lecture 2

Programming with Haiku

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

Making std::vector constexpr

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

CPSC 427a: Object-Oriented Programming

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

Exam 3 Chapters 7 & 9

Arrays. Arizona State University 1

CS 103 Lab V for Vector

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Chapter 4: STL Sequence Containers

Chapter 5. The Standard Template Library.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Intermediate Programming, Spring 2017*

Chapter 7: Arrays Copyrig opy ht rig 2012 Pea e rson a Educa Educ ti a on, Inc I. nc

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Computer Science II Lecture 2 Strings, Vectors and Recursion

C++ Programming Fundamentals

Midterm Review. PIC 10B Spring 2018

Vector and Free Store (Vectors and Arrays)

CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Computational Physics

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values

contiguous_container proposal

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CSI33 Data Structures

C++ Final Exam 2017/2018

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Programming in C++ using STL. Rex Jaeschke

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Containers in C++ and Java

Bruce Merry. IOI Training Dec 2013

Computer Science II CSci 1200 Test 1 Overview and Practice

Standard Template Library

Practice Problems CS2620 Advanced Programming, Spring 2003

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Transcription:

C++ Vector Constructors explicit vector ( const Allocator& = Allocator() ); explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() ); template <class InputIterator> vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() ); vector ( const vector<t,allocator>& x ); Default constructor: constructs an empty vector, with no content and a size of zero. Repetitive sequence constructor: Initializes the vector with its content set to a repetition, n times, of copies of value. Iteration constructor: Iterates between first and last, setting a copy of each of the sequence of elements as the content of the container. Copy constructor: The vector is initialized to have the same contents (copies) and properties as vector x. vector<int> first; // empty vector of ints vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second vector<int> fourth (third); // a copy of third begin iterator begin(); const_iterator begin() const; Returns an iterator referring to the first element in the vector container. Notice that unlike member vector::front, which returns a reference to the first element, this function returns a random access iterator. C++ vector Page 1

end iterator end(); const_iterator end() const; Returns an iterator referring to the past-the-end element in the vector container. Notice that unlike member vector::back, which returns a reference to the element preceding this one, this function returns a random access iterator. size size_type size() const; Returns the number of elements in the vector container. This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity. Vectors automatically reallocate their storage space when needed or when requested with member resize. To retrieve the current storage capacity of a vector you can call to its member capacity. resize void resize ( size_type sz, T c = T() ); Resizes the vector to contain sz elements. If sz is smaller than the current vector size, the content is reduced to its first sz elements, the rest being dropped. If sz is greater than the current vector size, the content is expanded by inserting at the end as many copies of c as needed to reach a size of sz elements. This may cause a reallocation. capacity size_type capacity () const; Returns the size of the allocated storage space for the elements of the vector container. Notice that, in vectors, the capacity is not necessarily equal to the number of elements that conform the underlying vector content (this can be obtained with member vector::size), but the capacity of the actual allocated space, which is either equal or greater than the content size. C++ vector Page 2

empty bool empty () const; Returns whether the vector container is empty, i.e. whether its size is 0. operator[] reference operator[] ( size_type n ); const_reference operator[] ( size_type n ) const; Returns a reference to the element at position n in the vector container. cout << "myvector contains:"; for (i=0; i<sz; i++) cout << " " << myvector[i]; front reference front ( ); const_reference front ( ) const; Returns a reference to the first element in the vector container. back reference back ( ); const_reference back ( ) const; Returns a reference to the last element in the vector container. C++ vector Page 3

assign template <class InputIterator> void assign ( InputIterator first, InputIterator last ); void assign ( size_type n, const T& u ); Assigns new content to the vector object, dropping all the elements contained in the vector before the call and replacing them by those specified by the parameters: In the first version (with iterators), the new contents are a copy of the elements in the sequence between first and last (in the range [first,last)). In the second version, the new content is the repetition n times of copies of element u. vector<int> first; vector<int> second; vector<int> third; first.assign (7,100); // a repetition 7 times of value 100 vector<int>::iterator it; it=first.begin()+1; second.assign (it,first.end()-1); // the 5 central values of first int myints[] = {1776,7,4}; third.assign (myints,myints+3); // assigning from array. cout << "Size of first: " << int (first.size()) << endl; cout << "Size of second: " << int (second.size()) << endl; cout << "Size of third: " << int (third.size()) << endl; Size of first: 7 Size of second: 5 Size of third: 3 C++ vector Page 4

push_back void push_back ( const T& x ); Adds a new element at the end of the vector, after its current last element. The content of this new element is initialized to a copy of x. vector<int> myvector; int myint; cout << "Please enter some integers (enter 0 to end):\n"; do { cin >> myint; myvector.push_back (myint); } while (myint); cout << "myvector stores " << (int) myvector.size() << " numbers.\n"; pop_back void pop_back ( ); Removes the last element in the vector, effectively reducing the vector size by one and invalidating all iterators and references to it. vector<int> myvector; int sum (0); myvector.push_back (100); myvector.push_back (200); myvector.push_back (300); while (!myvector.empty()) { sum+=myvector.back(); myvector.pop_back(); } cout << "The elements of myvector summed " << sum << endl; The elements of myvector summed 600 C++ vector Page 5

insert iterator insert ( iterator position, const T& x ); void insert ( iterator position, size_type n, const T& x ); template <class InputIterator> void insert ( iterator position, InputIterator first, InputIterator last ); The vector is extended by inserting new elements before the element at position. vector<int> myvector (3,100); vector<int>::iterator it; it = myvector.begin(); it = myvector.insert ( it, 200 ); myvector.insert (it,2,300); // "it" no longer valid, get a new one: it = myvector.begin(); vector<int> anothervector (2,400); myvector.insert (it+2,anothervector.begin(),anothervector.end()); int myarray [] = { 501,502,503 }; myvector.insert (myvector.begin(), myarray, myarray+3); cout << "myvector contains:"; for (it=myvector.begin(); it<myvector.end(); it++) cout << " " << *it; cout << endl; myvector contains: 501 502 503 300 300 400 400 200 100 100 100 C++ vector Page 6

erase iterator erase ( iterator position ); iterator erase ( iterator first, iterator last ); Removes from the vector container either a single element (position) or a range of elements ([first,last)). unsigned int i; vector<unsigned int> myvector; // set some values (from 1 to 10) for (i=1; i<=10; i++) myvector.push_back(i); // erase the 6th element myvector.erase (myvector.begin()+5); // erase the first 3 elements: myvector.erase (myvector.begin(),myvector.begin()+3); cout << "myvector contains:"; for (i=0; i<myvector.size(); i++) cout << " " << myvector[i]; cout << endl; myvector contains: 4 5 7 8 9 10 C++ vector Page 7

swap void swap ( vector<t,allocator>& vec ); Exchanges the content of the vector by the content of vec, which is another vector of the same type. Sizes may differ. After the call to this member function, the elements in this container are those which were in vec before the call, and the elements of vec are those which were in this. All iterators, references and pointers remain valid for the swapped vectors. unsigned int i; vector<int> first (3,100); // three ints with a value of 100 vector<int> second (5,200); // five ints with a value of 200 first.swap(second); cout << "first contains:"; for (i=0; i<first.size(); i++) cout << " " << first[i]; cout << "\nsecond contains:"; for (i=0; i<second.size(); i++) cout << " " << second[i]; cout << endl; first contains: 200 200 200 200 200 second contains: 100 100 100 C++ vector Page 8

clear void clear ( ); All the elements of the vector are dropped: their destructors are called, and then they are removed from the vector container, leaving the container with a size of 0. unsigned int i; vector<int> myvector; myvector.push_back (100); myvector.push_back (200); myvector.push_back (300); cout << "myvector contains:"; for (i=0; i<myvector.size(); i++) cout << " " << myvector[i]; myvector.clear(); myvector.push_back (1101); myvector.push_back (2202); cout << "\nmyvector contains:"; for (i=0; i<myvector.size(); i++) cout << " " << myvector[i]; cout << endl; myvector contains: 100 200 300 myvector contains: 1101 2202 C++ vector Page 9