본문 바로가기

3.구현/C or C++

Vectors in STL

Vectors in STL

Its a dynamic array that the stardard does not specify.

Header file : <vector>

Declaretion:

namespace std {

            template <class T, class Allocator = allocator<T> > class vector;

}

Vector opertions

Create, Copy, and Destory Operations

Operation

Effect

vector<Elem> c

Creats an empty vector without any elements

vector<Elem> c1(c2)

Create a copy of another vector of the same type (all elements are copied)

vector<Elem> c(n)

Creates a vector with n elements that are created by the default constructor

vector<Elem> c(n, elem)

Creates a vector initialized with n copies of element elem

vector<Elem> c(beg, end)

Creates a vector initialized with the elements of the range [beg, end)

c.~vector<Elem>()

Destroy all elements and free the memory

Nonmodifying Operations

Operation

Effect

c.size()

Return the actual number of elements

c.empty()

Returns whether the  container is empty (equivalent to size()==0, but might be faster)

c.max_size()

Returns the maximum number of elements possible

cpacity()

Returns the maximum possiblenumber of elements without reallocation

reserve()

Enlarges capacity, if not enough yes

c1 == c2

Return whether c1 is equal to c2

c1 != c2

Return whether c1 is not equal to c2 (equivalent to !(c1 == c2))

c1 < c2

Return whether c1 is less than c2

c1 > c2

Return whether c1 is greater than c2 (equivalent to c2 < c1)

c1 <= c2

Return whether c1 is less that or equal to c1 (equivalent !(c2<c1))

c1 >= c2

Return whether c1 is greater than or equal to c2 (equivalent to !(c1<c2))

Assignments

Operation

Effect

c1 = c2

Assigns all elements of c2 to c1

c.assign(n, elem)

Assigns n copies of element elem

c.assign(beg, end)

Assigns theelements of the range [beg, end)

c.swap(c2)

Swaps the data of c1 and c2

swpa(c1, c2)

Same (as global function)

std::list<Elem> l;

std::vector<Elem> coll;

coll.assign(l.begin(), l.end());

Element Access

Operation

Effect

c.at(idx)

Return the element with index idx (throws range error exception if idx is out of range)

c[idx]

Returns the element with index idx(no range checking)

c.front()

Returns the first element (no check whether a first element exists)

c.back()

Return the last element (no check whether a last element exists)

std::vector<Elem> coll;  // empty!

coll[5] = elem;  // RUNTIME ERROR è undefined behavior

std::cout << coll.front();// RUNTIME ERROR è undefined behavior

So, you must ensure that it is valid and not empty.

std::vector<Elem> coll; // empty

if(coll.size() > 5) {

            coll[5] = elem;  // OK

}

if (!coll.empty()) {

            cout << coll.front();  // OK

}

coll.at(5) = elem; // throws out_of_range exception

Iterator Functions

Operation

Effect

c.begin()

Returns a random access iterator for the first element

c.end()

Returns a random access iterator for the position after the last element

c.rbegin()

Return a reverse iterator for the first element of a reverse interation

c.rend()

Return a reverse iterator for the position after the last element of a reverse iteration

Inserting and Removing Elements

Operation

Effect

c.insert(pos, elem)

Insert at iterator position pos a copy of elem and return s the position of the new element

c.insert(pos, n, elem)

Inserts at iterator position os n coies of elem (returns nothing)

c.insert(pos, beg, end)

Inserts at iterator position pos a copy of all elements of the range [beg, end) (returns nothing)

c.push_back(elem)

Appends a copy of elem at the end

c.pop_back()

Removes the last element (does not return it)

c.erase(pos)

Removies the element at iterator position pos and returns theposition of the next element

c.erase(beg, end)

Removes all elements of the range [beg, end) and returns the position of the next element

c.resize(num)

Changes the numbe of elements to num (if size() grows, new elements are created by their default constructor)

c.resize(num, elem)

Changes the number of elements to num (if size() grows ,new elements are copies of elem)

c.clear()

Removes all elements (makes the container empty)

std::vector<Elem> coll;

// remove all elements with value val

coll.erase(remove(coll.begin(), coll.end(), val), coll.end());

To remove only  the first elements that has a certain value

std::vector<Elem> coll;

//remove first element with value val

std::vector<Elem>::iterator pos;

pos = find(coll.begin(), coll.end(), val);

if (pos != coll.end()) {

            coll.erase(pos);

}

Exception Handling

the C++ standard library guarantees the following:

1.       If an element gets insertedwith push_back() and an exception occurs, this function has no effect.

2.       insert() either succeeds or has no effect if the copy operations (copy constructor and assignment operator) of the elements do not throw.

3.       pop_back() does not throw any exceptions.

4.       erase() and clear do not rhrow if the copy operations (copy constructor and assignment operator) of the elements do not throw.

5.       swap() does not throw.

6.       If elements are used that naver throw exceptions on copy operations (copy constructor and assignment operator), every operation is either successful or has no effect. Such elements might be “plain old data” (POD).

Examples

반응형

'3.구현 > C or C++' 카테고리의 다른 글

Metaprogramming  (0) 2007.02.16
[패턴] Command  (0) 2007.02.16
블록 메모리 복사 성능시험  (0) 2007.02.16
메모리 복사 성능시험 (memcpy)  (0) 2007.02.16
디버거 - 로그 윈도우 2.5 (Win32 디버그 지원)  (2) 2006.11.23