Vectors in C++
Vectors in C++
v0.9
Why to use
vector
A vector is an array like container that improves on the C++ array types. In
particular it is not necessary to know how big you want the vector to be when you
declare it, you can add new elements to the end of a vector using the push_back
function. (In fact the insert function allows you insert new elements at any position
of the vector, but this is a very inefficient operation − − if you need to do this often
consider using a list instead).
Vector elements are placed in contiguous storage so that they can be accessed and
traversed using iterators
1. vector<int> v1;
2. vector<string> v2;
3. vector<ClassObject> v3;
Declaration and initialization
#include <iostream.h>
#include <vector.h>
void main()
{
vector<int> v1;
vector<int> v2(10);
vector<int> v3(10,7);
cout < < "v1.size() returns " < < v1.size() < <
endl; cout < < "v2.size() returns " < < v2.size()
< < endl; cout < < "v3.size() returns " < <
v3.size() < < endl;
}
Checking whether vector is empty or not
VectorName.empty();
Returns a Boolean value;
Accessing Elements of a Vector
You can access a vector's elements using operator[]. Thus, if you wanted to print out
all the elements in a vector you could use code like vector<int> v;
Example:
vector<int> v;
vector<int>::iterator intIterator;
v.insert(v.end() - 2, 5);
Begin() and
end()
Such iterators are constructed and returned by the functions begin() and end().
You can compare two iterators (of the same type) using == and !=, increment
using
++ and dereference using *.
DO REMEMBER
For iteration itr1
1. itr1 will return the position
2. *itr1 will return value at that position (dereferencing)
Iteration – an
example
//continued
#include <iostream.h> {
#include <vector.h> *i =
void main() j; j+
{ +; i+
vector<int> v(10); +;
first is ``less'' than the }
second int j = 1; //
vector<int>::iterator i; Squa
// Fill the vector v with integers 1 to re
10. i = v.begin(); each
while (i != v.end()) elem
ent
of v.
for
(i=v.b
egin()
Comparison of
vectors
You can compare two vectors using == and <. == will return true only if both vectors
have the same number of elements and all elements are equal.
void main()
{
vector<int> v1;
vector<int>
v2;
for (int i=0; i<4; i++) v1.push_back(i+1);
for (int i=0; i<3; i++)
v2.push_back(i+1); cout << "v1: ";
for (int i=0; i<v1.size(); i++) cout <<
v1[i] << ' ';
cout << endl;
cout << "v2:
";
for (int i=0; i<v2.size(); i++) cout << v2[i] << '
Searching - std::find()
std::find() - Finds the element in the given range of numbers.
- returns an iterator to the first element in the range [first,last) that compares equal to val.
- If no such element is found, the function returns last.
Format
InputIterator find (InputIterator first, InputIterator last, const T& val)
Arguments
val :
Value to be search in the range
Return Value :
An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.
Searching - std::find()
Searching – std::find_if
Searching – std::find_if
Getting collection of items
Getting collection of items
Getting collection of items – copy_if
Copies the elements in the range [first,last) for which pred returns true
Getting collection of items – copy_if