C++语法基础--泛型算法(generic algorithm)--写入容器元素的算法fill(),fill_n,replace()
2013-07-12 04:33
573 查看
*fill
Assigns val to all the elements in the range [first,last)
原型:
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;
}
}
Example:
int main ()
{
vector<int> myvector (8); // myvector: 0 0 0 0 0 0 0 0
fill (myvector.begin(),myvector.begin()+4,1);
// myvector: 1 1 1 1 0 0 0 0
fill (myvector.begin()+3,myvector.end()-2,2); // myvector: 1 1 1 2 2 2 0 0
//myvector contains:
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
cout << ' ' << *it;// 1 1 1 2 2 2 0 0
}
return 0;
}
*fill_n
Assigns val to the first n elements of the sequence pointed by first.
原型:
template <class OutputIterator, class Size, class T>
void 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; // since C++11
}
Example:
int main ()
{
vector<int> myvector (8,1); // myvector: 1 1 1 1 1 1 1 1
std::fill_n (myvector.begin(),4,2); // myvector: 2 2 2 2 1 1 1 1
//myvector contains:
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
cout << ' ' << *it;//2 2 2 2 1 1 1 1
}
return 0;
}
Tips:
fill_n假定对指定数量的元素做写操作时是安全的,所以要确保容器有足够的空间。
*replace
Assigns new_value to all the elements in the range [first,last) that compare equal to old_value.
原型:
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value);
该算法等效于:
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
{
while (first!=last) {
if (*first == old_value) *first=new_value;
++first;
}
}
Example:
int main ()
{
int myints[] = { 1,1,1,3,2,2,1,3 };
vector<int> myvector (myints, myints+8); // 1,1,1,3,2,2,1,3
replace (myvector.begin(), myvector.end(), 3, 2);// 1,1,1,2,2,2,1,2
//myvector contains:
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
cout << ' ' << *it;// 1,1,1,2,2,2,1,2
}
return 0;
}
Assigns val to all the elements in the range [first,last)
原型:
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;
}
}
Example:
int main ()
{
vector<int> myvector (8); // myvector: 0 0 0 0 0 0 0 0
fill (myvector.begin(),myvector.begin()+4,1);
// myvector: 1 1 1 1 0 0 0 0
fill (myvector.begin()+3,myvector.end()-2,2); // myvector: 1 1 1 2 2 2 0 0
//myvector contains:
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
cout << ' ' << *it;// 1 1 1 2 2 2 0 0
}
return 0;
}
*fill_n
Assigns val to the first n elements of the sequence pointed by first.
原型:
template <class OutputIterator, class Size, class T>
void 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; // since C++11
}
Example:
int main ()
{
vector<int> myvector (8,1); // myvector: 1 1 1 1 1 1 1 1
std::fill_n (myvector.begin(),4,2); // myvector: 2 2 2 2 1 1 1 1
//myvector contains:
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
cout << ' ' << *it;//2 2 2 2 1 1 1 1
}
return 0;
}
Tips:
fill_n假定对指定数量的元素做写操作时是安全的,所以要确保容器有足够的空间。
*replace
Assigns new_value to all the elements in the range [first,last) that compare equal to old_value.
原型:
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value);
该算法等效于:
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
{
while (first!=last) {
if (*first == old_value) *first=new_value;
++first;
}
}
Example:
int main ()
{
int myints[] = { 1,1,1,3,2,2,1,3 };
vector<int> myvector (myints, myints+8); // 1,1,1,3,2,2,1,3
replace (myvector.begin(), myvector.end(), 3, 2);// 1,1,1,2,2,2,1,2
//myvector contains:
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
cout << ' ' << *it;// 1,1,1,2,2,2,1,2
}
return 0;
}
相关文章推荐
- C++语法基础--泛型算法(generic algorithm)--replace_copy(),unique_copy,copy()
- C++语法基础--泛型算法(generic algorithm)--对容器排序的算法sort(),stable_sort(),unique()
- C++语法基础--泛型算法(generic algorithm)--插入迭代器back_inserter(),front_insertor(),inserter()以及next()函数简介
- C++语法基础--关联容器--map(二)--查找并读取map中的元素count(),find()与 从map对象中删除元素erase()
- C++语法基础--泛型算法(generic algorithm)--transform
- C++学习笔记26——泛型算法之容器元素排序(sort unique)
- C++语法基础--泛型算法(generic algorithm)--find(),find_if()
- C++语法基础--泛型算法(generic algorithm)--反向迭代器,reverse_iterator::base()
- C++学习笔记25——泛型算法之写入容器
- C++语法基础--顺序容器(三)--关系操作符,容器大小的操作,访问元素,删除元素,查找元素
- C++语法基础--泛型算法(generic algorithm)--只读算法accumulate(),find_first_of(),count(),count_if()
- C++语法基础--泛型算法(generic algorithm)--iostream迭代器
- C++STL总结(附带容器迭代器泛型算法详情链接【未完成】)
- C++:泛型算法基础
- C++语法基础--关联容器--multimap,multiset
- C++语法基础--顺序容器(四)--赋值和swap
- C++ Primer 学习笔记:泛型算法入门 fill && back_inserter && copy && replace
- C++语法基础--关联容器--set
- C++ 学习笔记 2.1 : 容器和算法(顺序容器、关联容器、泛型算法)
- C++语法基础--list容器提供的的特有操作--merge(),remove(),reverse(),unique(),sort(),splice()