锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
class deque {
public:
// typedefs:
typedef iterator;
typedef const_iterator;
typedef Allocator<T>::pointer pointer;
typedef Allocator<T>::reference reference;
typedef Allocator<T>::const_reference const_reference;
typedef size_type;
typedef difference_type;
typedef T value_type;
typedef reverse_iterator;
typedef const_reverse_iterator;
// allocation/deallocation:
deque();
deque(size_type n, const T& value = T());
deque(const deque<T, Allocator>& x);
template <class InputIterator>
deque(InputIterator first, InputIterator last);
~deque();
deque<T, Allocator>& operator=(const deque<T, Allocator>& x);
void swap(deque<T, Allocator>& x);
// accessors:
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator rend();
size_type size() const;
size_type max_size() const;
bool empty() const;
reference operator[](size_type n);
const_reference operator[](size_type n) const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
// insert/erase:
void push_front(const T& x);
void push_back(const T& x);
iterator insert(iterator position, const T& x = T());
void insert (iterator position, size_type n, const T& x);
template <class InputIterator>
void insert (iterator position, InputIterator first, InputIterator last);
void pop_front();
void pop_back();
void erase(iterator position);
void erase(iterator first, iterator last);
};
]]>
class Allocator = allocator> class list
{
public:
// typedefs:
typedef iterator
typedef const_iterator
typedef Allocator<T>::pointer pointer
typedef Allocator<T>::reference reference
typedef Allocator<T>::const_reference const_reference
typedef size_type
typedef difference_type
typedef T value_type
typedef reverse_iterator
typedef const_reverse_iterator;
// allocation/deallocation:
list()
list(size_type n, const T& value = T())
template <class InputIterator>
list(InputIterator first, InputIterator last)
list(const list<T, Allocator>& x)
~list()
list<T, Allocator>& operator=(const list<T, Allocator>& x)
void swap(list<T, Allocator>& x);
// accessors:
iterator begin()
const_iterator begin() const
iterator end()
const_iterator end() const
reverse_iterator rbegin()
const_reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator rend();
bool empty() const;
size_type size() const;
size_type max_size() const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
// insert/erase:
void push_front(const T& x);
void push_back(const T& x);
iterator insert(iterator position, const T& x = T());
void insert(iterator position, size_type n, const T& x);
template <class InputIterator>
void insert(iterator position, InputIterator first, InputIterator last);
void pop_front();
void pop_back();
void erase(iterator position);
void erase(iterator first, iterator last);
// special mutative operations on list:
void splice(iterator position, list<T, Allocator>& x);
void splice(iterator position, list<T, Allocator>& x, iterator i);
void splice(iterator position, list<T, Allocator>& x,
iterator first, iterator last);
void remove(const T& value);
template <class Predicate> void remove_if(Predicate pred);
void unique();
template <class BinaryPredicate> void unique(BinaryPredicate binary_pr
void merge(list<T, Allocator>& x);
template <class Compare> void merge(list<T, Allocator>& x, Compare com
void reverse();
void sort();
template <class Compare> void sort(Compare comp);
};
]]>
insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency.
template <class T, template <class U> class Allocator = allocator>
class vector {
public:
// typedefs:
typedef iterator;
typedef const_iterator;
typedef Allocator::pointer pointer;
typedef Allocator::reference reference;
typedef Allocator::const_reference const_reference;
typedef size_type;
typedef difference_type;
typedef T value_type;
typedef reverse_iterator;
typedef const_reverse_iterator;
// allocation/deallocation:
vector();
vector(size_type n, const T& value = T());
vector(const vector& x);
template vector(InputIterator first, InputIterator last);
~vector();
vector& operator=(const vector& x);
void reserve(size_type n);
void swap(vector& x);
// accessors:
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator rend();
size_type size() const;
size_type max_size() const;
size_type capacity() const;
bool empty() const;
reference operator[](size_type n);
const_reference operator[](size_type n) const
reference front();
const_reference front() const;
reference back();
const_reference back() const;
// insert/erase:
void push_back(const T& x);
iterator insert(iterator position, const T& x = T());
void insert(iterator position, size_type n, const T& x);
template
void insert(iterator position, InputIterator first, InputIterator last);
void pop_back();
void erase(iterator position);
void erase(iterator first, iterator last);
};
]]>
bool includes(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2, Compare comp)
鍚堝茍錛氬悎騫禰first1,last1)涓嶽first2,last2)涓や釜闆嗗悎銆?br>OutputIterator set_union(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
姹備氦闆嗭細姹俒first1,last1)涓嶽first2,last2)涓や釜闆嗗悎鐨勪氦闆嗐?
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
姹傚樊錛氭眰鍦╗first1,last1)涓紝鑰屼笉鍦╗first2,last2)鐨勫厓绱犻泦鍚堛?
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
姹傚縐板樊錛氫袱涓泦鍚堜箣鍜岄泦 鍑忓幓 涓や釜闆嗗悎涔嬩氦闆嗐?
OutputIterator set_symmetric difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator set_symmetric difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;
void init(vector<int>& v, int a, int b)
{
v.clear();
for(int i = a; i <= b; i++){
v.push_back(i);
}
}
int main()
{
vector<int>v1, v2;
vector<int>::iterator itr;
cout << "Includes operator:" << endl;
init(v1,1,5);
init(v2,1,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "v2 is in v1 ? " << (includes(v1.begin(),v1.end(),v2.begin(),v2.end()) ? "yes" : "no") << endl;
cout << "v1 is in v2 ? " << (includes(v2.begin(),v2.end(),v1.begin(),v1.end()) ? "yes" : "no") << endl;
cout << endl;
init(v1,1,5);
init(v2,3,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_union operator on v1,v2: ";
set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
init(v1,1,5);
init(v2,3,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_intersecion operator on v1,v2: ";
set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
init(v1,1,5);
init(v2,3,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_difference operator, v1 to v2: ";
set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_difference operator, v2 to v1: ";
set_difference(v2.begin(),v2.end(),v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
init(v1,1,5);
init(v2,3,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_symmetric_difference operator, v1 to v2: ";
set_symmetric_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_symmetric_difference operator, v2 to v1: ";
set_symmetric_difference(v2.begin(),v2.end(),v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
return 0;
}
Vector v1: 1 2 3 4 5
Vector v2: 1 2 3 4 5 6 7 8
v2 is in v1 ? no
v1 is in v2 ? yes
Vector v1: 1 2 3 4 5
Vector v2: 3 4 5 6 7 8
set_union operator on v1,v2: 1 2 3 4 5 6 7 8
Vector v1: 1 2 3 4 5
Vector v2: 3 4 5 6 7 8
set_intersecion operator on v1,v2: 3 4 5
Vector v1: 1 2 3 4 5
Vector v2: 3 4 5 6 7 8
set_difference operator, v1 to v2: 1 2
set_difference operator, v2 to v1: 6 7 8
Vector v1: 1 2 3 4 5
Vector v2: 3 4 5 6 7 8
set_symmetric_difference operator, v1 to v2: 1 2 6 7 8
set_symmetric_difference operator, v2 to v1: 1 2 6 7 8
]]>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
inplace_merge鍚堝茍涓涓腑鍓嶅悗涓ゆ鍒嗗埆鏈夊簭鐨勫簭鍒楋紝鍗砙first,middle)涓嶽middle,last)鍒嗗埆鏈夊簭錛屽悎騫惰繖涓や釜瀛愬簭鍒楋紝騫舵妸緇撴灉淇濆瓨鍦╗first,last錛変腑銆?br>inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
#include<algorithm>
#include<iostream>
#include<iterator>
#include<vector>
using namespace std;
int main()
{
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>::iterator vecitr;
for(int i = 1; i <= 10; i++){
v1.push_back(i%7);
v2.push_back(i%4);
}
sort(v1.begin(),v1.end());
cout << "Fisrt vecter: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
sort(v2.begin(),v2.end());
cout << "Second vecter: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
cout << "Merge this two vecter: ";
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
for(int i = 1; i <= 10; i++){
v3.push_back(i%7);
}
sort(v3.begin(),v3.end());
int x = v3.size();
for(int i = 1; i <= 10; i++){
v3.push_back(i%4);
}
vecitr = v3.begin();
advance(vecitr,x);
sort(vecitr,v3.end());
cout << "Third vecter: ";
copy(v3.begin(),v3.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
cout << "inplace_Merge third vecter: ";
inplace_merge(v3.begin(),vecitr,v3.end());
copy(v3.begin(),v3.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
return 0;
}
Second vecter: 0 0 1 1 1 2 2 2 3 3
Merge this two vecter: 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 4 5 6
Third vecter: 0 1 1 2 2 3 3 4 5 6 0 0 1 1 1 2 2 2 3 3
inplace_Merge third vecter: 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 4 5 6
]]>
ForwardIterator lower_bound (ForwardIterator first,ForwardIterator last, const T& value)
ForwardIterator lower_bound (ForwardIterator first,ForwardIterator last, const T& value, Compare comp)
//鏌ユ壘鏈榪滅殑涓涓猧terator i錛?浣垮緱[first,i)閲岄潰鐨勪換鎰忎竴涓猧terator j錛屾湁 *j < value or comp(*j,value)==true.
ForwardIterator upper_bound (ForwardIterator first,ForwardIterator last, const T& value)
ForwardIterator upper_bound (ForwardIterator first,ForwardIterator last, const T& value, Compare comp)
//鏌ユ壘鏈榪滅殑涓涓猧terator i錛?浣垮緱[first,i)閲岄潰鐨勪換鎰忎竴涓猧terator j錛屾湁 !(*j < value) or comp(*j,value)==false.
pair equal_range(ForwardIterator first, ForwardIterator last, const T& value)
pair equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp)
//鏌ユ壘鏈澶х殑subrange[i,j)錛屼嬌寰椾換鎰忎竴涓猧terator in [i,j)婊¤凍!(*k<value) && !(value<*k) or comp(*k,value)==false && comp(value,*k)==false.
bool binary_search (ForwardIterator first, ForwardIterator last, const T& value)
bool binary_search (ForwardIterator first, ForwardIterator last, const T& value, Compare comp)
// 鏌ユ壘鏄惁鍦╗first,last)涓瓨鍦╥terator i,婊¤凍 !(*i<value) && !(value<*i) or comp(*i,value)==false && comp(value,*i)==false
// 瀛樺湪鍒欒繑鍥瀟rue,鍚﹀垯榪斿洖false.
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
vector<int>v;
vector<int>::iterator itr;
pair< vector<int>::iterator, vector<int>::iterator >vecpair;
for(int i = 1; i <= 20; i++){
v.push_back(i%6);
}
sort(v.begin(),v.end());
cout << "array: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl;
//lower_bound
cout << "lower_bound function, value = 3:" << endl;
itr = lower_bound(v.begin(),v.end(),3);
cout << " [first, itr) = ";
copy(v.begin(),itr,ostream_iterator<int>(cout," "));
cout << endl;
cout << " [itr, last) = ";
copy(itr,v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
// upper_bound
cout << "upper_bound function, value = 3:" << endl;
itr = upper_bound(v.begin(),v.end(),3);
cout << " [first, itr) = ";
copy(v.begin(),itr,ostream_iterator<int>(cout," "));
cout << endl;
cout << " [itr, last) = ";
copy(itr,v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
// equal_range
cout << "equal_range function, value = 3:" << endl;
vecpair = equal_range(v.begin(),v.end(),3);
cout << " [vecpair->first, vecpair->second) = ";
copy(vecpair.first, vecpair.second, ostream_iterator<int>(cout," "));
cout << endl << endl;
//binary_search, value = 3
cout << "binary_search function, value = 3:" << endl;
cout << "3 is " << (binary_search(v.begin(),v.end(),3) ? "":"not ") << "in array." << endl;
cout << endl;
//binary_search, value = 6
cout << "binary_search function, value = 6:" << endl;
cout << "6 is " << (binary_search(v.begin(),v.end(),6) ? "":"not ") << "in array." << endl;
cout << endl;
return 0;
}
0 0 0 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 5
lower_bound function, value = 3:
[first, itr) = 0 0 0 1 1 1 1 2 2 2 2
[itr, last) = 3 3 3 4 4 4 5 5 5
upper_bound function, value = 3:
[first, itr) = 0 0 0 1 1 1 1 2 2 2 2 3 3 3
[itr, last) = 4 4 4 5 5 5
equal_range function, value = 3:
[vecpair->first, vecpair->second) = 3 3 3
binary_search function, value = 3:
3 is in array.
binary_search function, value = 6:
6 is not in array.
]]>