(7)STL算法之交换赋值
3、互换
STL的大部分容器都提供了成员函数swap(),用于两个不同容器对象之间的元素交换,同时,STL算法也提供了算法swap()。个人认为尽量使用容器自己的swap()。
(1)swap()
// 声明及定义
template <class T>
void swap (T& a, T& b);
template <class T>
void swap (T& a, T& b)
{
T c(std::move(a));
a=std::move(b);
b=std::move(c);
}
template <class T, size_t N>
void swap (T (&a)[N], T (&b)[N])
{
for (size_t i = 0; i<N; ++i)
swap (a[i],b[i]);
}
用法示例
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int x = 10, y = 20; // x=10 y=20
swap(x, y); // x=20 y=10
vector<int> v1(4, x), v2(6, y); // 4个20 6个10
swap(v1, v2); // 6个10 4个20
cout << "vector v1:";
for (auto it = v1.begin(); it != v1.end(); ++it)
cout << *it << " ";
cout << endl;
cout << "vector v2:";
for (int val : v2)
cout << val << " ";
cout << endl;
return 0;
}
(2) swap_ranges()
这个函数可以交换两个容器中的部分元素
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2);
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2)
{
while (first1!=last1)
{
swap (*first1, *first2);
++first1;
++first2;
}
return first2;
}
用法示例:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v1(5, 10);
vector<int> v2(3, 33);
// 用容器一中四个元素交换 容器二从 begin开始的四个元素,
//如果容器二中没有这么大空间,会出问题,比如将上边改为 vector<int> v2(3, 33);
swap_ranges(v1.begin(), v1.begin() + 4, v2.begin());
cout << "vector v1:";
for (auto it = v1.begin(); it != v1.end(); ++it)
cout << *it << " ";
cout << '\n';
cout << "vector v2:";
for (auto it = v2.begin(); it != v2.end(); ++it)
cout << *it << " ";
cout << '\n';
return 0;
}
4、赋值
可以使用 fill()、fill_n()、generate()、generate_n() 4个函数来给容器赋值。
fill()、fill_n()用于给指定区间每个元素赋予相同的数值,
(1)fill()
// fill() 声明及定义
template <class ForwardIterator, class T>
void fill (ForwardIterator first, ForwardIterator last, const T& val);
template <class ForwardIterator, class T>
void fill (ForwardIterator first, ForwardIterator last, const T& val)
{
while (first != last)
{
*first = val;
++first;
}
}
用法示例:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v(8); // v: 0 0 0 0 0 0 0 0
fill(v.begin(), v.begin() + 4, 5); // v: 5 5 5 5 0 0 0 0
fill(v.begin() + 3, v.end() - 2, 8);// v: 5 5 5 8 8 8 0 0
cout << "vector v:";
for (auto it = v.begin(); it != v.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
(2)fill_n()
template <class OutputIterator, class Size, class T>
OutputIterator fill_n (OutputIterator first, Size n, const T& val);
template <class OutputIterator, class Size, class T>
OutputIterator fill_n (OutputIterator first, Size n, const T& val)
{
while (n>0)
{
*first = val;
++first;
--n;
}
return first;
}
用法示例:
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v(8, 10); // v: 10 10 10 10 10 10 10 10
std::fill_n(v.begin(), 4, 20); // v: 20 20 20 20 10 10 10 10
std::fill_n(v.begin() + 3, 3, 33); // v: 20 20 20 33 33 33 10 10
return 0;
}
(3)generate()、generate_n()
template <class ForwardIterator, class Generator>
void generate (ForwardIterator first, ForwardIterator last, Generator gen);
template <class ForwardIterator, class Generator>
void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
{
while (first != last)
{
*first = gen();
++first;
}
}
template <class OutputIterator, class Size, class Generator>
OutputIterator generate_n (OutputIterator first, Size n, Generator gen);
template <class OutputIterator, class Size, class Generator>
void generate_n ( OutputIterator first, Size n, Generator gen )
{
while (n>0)
{
*first = gen();
++first;
--n;
}
}
用法示例:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int Fibonacci(void)
{
static int r;
static int f1 = 0;
static int f2 = 1;
r = f1 + f2;
f1 = f2;
f2 = r;
return f1;
}
void main()
{
vector<int> v2(5, 0), v3(6, 0);
generate(v2.begin(), v2.end(), rand);
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ", ")); //打印
cout << endl;
generate_n(v2.begin(), 3, rand); //从 begin() 赋值 3个随机数
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ", "));
cout << endl;
// 自定义规则
generate_n(v3.begin(), 6, Fibonacci);
copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, ", "));
cout << endl;
}