(1) copy函数:MSDN中的定义是这样子的,参数_First是源数组(或集合)对象的起始指针(或迭代器),参数_Last是源数组(或集合)对象的末尾指针(或迭代器),参数_DestBeg是目标数组(或集合)对象的起始指针(或迭代器)。
template<class InputIterator, class OutputIterator>
OutputIterator copy(
InputIterator _First,
InputIterator _Last,
OutputIterator _DestBeg
);示例代码如下:
// alg_copy.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>

int main() ...{
using namespace std;
vector <int> v1, v2;
vector <int>::iterator Iter1, Iter2;
int i;
for ( i = 0 ; i <= 5 ; i++ )
v1.push_back( 10 * i );
int ii;
for ( ii = 0 ; ii <= 10 ; ii++ )
v2.push_back( 3 * ii );
cout << "v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
cout << "v2 = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
// To copy the first 3 elements of v1 into the middle of v2
copy( v1.begin( ), v1.begin( ) + 3, v2.begin( ) + 4 );
cout << "v2 with v1 insert = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
// To shift the elements inserted into v2 two positions
// to the left
copy( v2.begin( )+4, v2.begin( ) + 7, v2.begin( ) + 2 );
cout << "v2 with shifted insert = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
}(2)find函数:MSDN中的定义是这样子的,参数_First是数组(或集合)对象的起始指针(或迭代器),参数_Last是数组(或集合)对象的末尾指针(或迭代器),参数_Val是数组(或集合)对象要查找的值。
template<class InputIterator, class Type>
InputIterator find(
InputIterator _First,
InputIterator _Last,
const Type& _Val
);示例代码如下:
// alg_find.cpp
// compile with: /EHsc
#include <list>
#include <algorithm>
#include <iostream>

int main() ...{
using namespace std;
list <int> L;
list <int>::iterator Iter;
list <int>::iterator result;
L.push_back( 40 );
L.push_back( 20 );
L.push_back( 10 );
L.push_back( 30 );
L.push_back( 10 );
cout << "L = ( " ;
for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
cout << *Iter << " ";
cout << ")" << endl;
result = find( L.begin( ), L.end( ), 10 );
if ( result == L.end( ) )
cout << "There is no 10 in list L.";
else ...{
cout << "There is a 10 in list L";
if ( ++result != L.end() )
cout << " and it is followed by a " << *result << ".";
}
cout << endl;
}(3)replace函数:MSDN中的定义是这样子的,参数_First是数组(或集合)对象的起始指针(或迭代器),参数_Last是数组(或集合)对象的末尾指针(或迭代器),参数_OldVal是数组(或集合)要被替代的原值,参数_NewVal是数组(或集合)替代的新值。
template<class ForwardIterator, class Type>
void replace(
ForwardIterator _First,
ForwardIterator _Last,
const Type& _OldVal,
const Type& _NewVal
);示例代码如下:
// alg_replace.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>

int main( ) ...{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;
int i;
for ( i = 0 ; i <= 9 ; i++ )
v1.push_back( i );
int ii;
for ( ii = 0 ; ii <= 3 ; ii++ )
v1.push_back( 7 );
random_shuffle (v1.begin( ), v1.end( ) );
cout << "The original vector v1 is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Replace elements with a value of 7 with a value of 700
replace (v1.begin( ), v1.end( ), 7 , 700);
cout << "The vector v1 with a value 700 replacing that of 7 is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
}(4)sort函数:MSDN中的定义是这样子的,参数_First是数组(或集合)对象的起始指针(或迭代器),参数_Last是数组(或集合)对象的末尾指针(或迭代器),指定了在一个范围内的值进行排序。
template<class RandomAccessIterator>
void sort(
RandomAccessIterator _First,
RandomAccessIterator _Last
);示例代码如下:
// alg_sort.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>( )
#include <iostream>
// Return whether first element is greater than the second
bool UDgreater ( int elem1, int elem2 )
...{
return elem1 > elem2;
}
int main( )
...{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;
int i;
for ( i = 0 ; i <= 5 ; i++ )
...{
v1.push_back( 2 * i );
}
int ii;
for ( ii = 0 ; ii <= 5 ; ii++ )
...{
v1.push_back( 2 * ii + 1 );
}
cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
sort( v1.begin( ), v1.end( ) );
cout << "Sorted vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// To sort in descending order. specify binary predicate
sort( v1.begin( ), v1.end( ), greater<int>( ) );
cout << "Resorted (greater) vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// A user-defined (UD) binary predicate can also be used
sort( v1.begin( ), v1.end( ), UDgreater );
cout << "Resorted (UDgreater) vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
}(5)random_shuffle函数:MSDN中的定义是这样子的,参数_First是数组(或集合)对象的起始指针(或迭代器),参数_Last是数组(或集合)对象的末尾指针(或迭代器),指定了在一个范围内随机打乱数据排列顺序。
template<class RandomAccessIterator>
void random_shuffle(
RandomAccessIterator _First,
RandomAccessIterator _Last
);示例代码如下:
// alg_random_shuffle.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>

int main( ) ...{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1, Iter2;
int i;
for ( i = 1 ; i <= 9 ; i++ )
v1.push_back( i );
random_shuffle( v1.begin( ), v1.end( ) );
cout << "The original version of vector v1 is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Shuffled once
random_shuffle( v1.begin( ), v1.end( ));
push_heap( v1.begin( ), v1.end( ) );
cout << "Vector v1 after one shuffle is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Shuffled again
random_shuffle( v1.begin( ), v1.end( ));
push_heap( v1.begin( ), v1.end( ) );
cout << "Vector v1 after another shuffle is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
}刚接触过STL,认识了几个函数的用法,使用起来也真的挺方便的。特别写出来记念。
本文介绍了STL中五个常用算法的使用方法,包括copy、find、replace、sort和random_shuffle函数,并提供了详细的示例代码。
1035

被折叠的 条评论
为什么被折叠?



