C++语法基础--泛型算法(generic algorithm)--插入迭代器back_inserter(),front_insertor(),inserter()以及next()函数简介
2013-07-12 15:33
281 查看
今天研究迭代器时发现原来for循环可以这样写:
int my_array[5] = {1, 2, 3, 4, 5};
for (int &x : my_array)
{
cout<<x;
}
百度了一下才知道:C++11支持以范围为基础的 for 循环。看来又要多花点时间去掌握C++11的新特性了。
1.标准库所定义的迭代器不依赖于特定的容器,事实上C++还提供了另外三种迭代器:
*插入迭代器
*iostream迭代器
*反向迭代器
2.C++提供三种插入器
*back_inserter:使用push_back实现插入的迭代器
Constructs a back-insert iterator that inserts new elements at the end of x.
原型:
template <class Container>
back_insert_iterator<Container> back_inserter (Container& x);
解析:
x :
Container on which the iterator will insert new elements.
Container should be a container class with member push_back defined.
return:
A back_insert_iterator that inserts elements at the end of container x.
Example:
int main()
{
int arr[]={1,2,3};
vector<int> vec(arr,arr+3);
fill_n(back_inserter(vec), 3, -1);
for (int n : vec)
{
cout << n << ' ';
}
}
//output://1 2 3 -1 -1 -1
*front_inserter:使用push_front实现插入
类似于back_inserter,这里就不举例了
*inserter: 使用insert实现插入
Constructs an insert iterator that inserts new elements int x at the position pointed by it.
原型:
template< class Container >
std::insert_iterator<Container> inserter( Container& c, typename Container::iterator i );
Example:
int main()
{
int arr[]={1,2,3,4,5};
list<int> lst(arr,arr+5);
fill_n(inserter(lst, next(lst.begin(),2)), 3, -1);
for (int n : lst)
{
cout << n << ' ';
}
}
Output:1 2 -1 -1 -1 3 4 5
3.next()
Returns an iterator pointing to the element that it would be pointing to if advanced n positions.
原型:
template <class ForwardIterator>
ForwardIterator next (ForwardIterator it,
typename iterator_traits<ForwardIterator>::difference_type n = 1);
解析:
it : an iterator
n : number of elements to advance
Return :The nth successor of iterator it.
Example:
int main()
{
int arr[]={1,2,3,4};
vector<int> v(arr,arr+4);
vector<int>::iterator it = v.begin();
vector<int>::iterator nx = std::next(it, 2);
cout << *it << ' ' << *nx << '\n';//1 3
}
int my_array[5] = {1, 2, 3, 4, 5};
for (int &x : my_array)
{
cout<<x;
}
百度了一下才知道:C++11支持以范围为基础的 for 循环。看来又要多花点时间去掌握C++11的新特性了。
1.标准库所定义的迭代器不依赖于特定的容器,事实上C++还提供了另外三种迭代器:
*插入迭代器
*iostream迭代器
*反向迭代器
2.C++提供三种插入器
*back_inserter:使用push_back实现插入的迭代器
Constructs a back-insert iterator that inserts new elements at the end of x.
原型:
template <class Container>
back_insert_iterator<Container> back_inserter (Container& x);
解析:
x :
Container on which the iterator will insert new elements.
Container should be a container class with member push_back defined.
return:
A back_insert_iterator that inserts elements at the end of container x.
Example:
int main()
{
int arr[]={1,2,3};
vector<int> vec(arr,arr+3);
fill_n(back_inserter(vec), 3, -1);
for (int n : vec)
{
cout << n << ' ';
}
}
//output://1 2 3 -1 -1 -1
*front_inserter:使用push_front实现插入
类似于back_inserter,这里就不举例了
*inserter: 使用insert实现插入
Constructs an insert iterator that inserts new elements int x at the position pointed by it.
原型:
template< class Container >
std::insert_iterator<Container> inserter( Container& c, typename Container::iterator i );
Example:
int main()
{
int arr[]={1,2,3,4,5};
list<int> lst(arr,arr+5);
fill_n(inserter(lst, next(lst.begin(),2)), 3, -1);
for (int n : lst)
{
cout << n << ' ';
}
}
Output:1 2 -1 -1 -1 3 4 5
3.next()
Returns an iterator pointing to the element that it would be pointing to if advanced n positions.
原型:
template <class ForwardIterator>
ForwardIterator next (ForwardIterator it,
typename iterator_traits<ForwardIterator>::difference_type n = 1);
解析:
it : an iterator
n : number of elements to advance
Return :The nth successor of iterator it.
Example:
int main()
{
int arr[]={1,2,3,4};
vector<int> v(arr,arr+4);
vector<int>::iterator it = v.begin();
vector<int>::iterator nx = std::next(it, 2);
cout << *it << ' ' << *nx << '\n';//1 3
}
相关文章推荐
- C++语法基础--泛型算法(generic algorithm)--反向迭代器,reverse_iterator::base()
- C++语法基础--泛型算法(generic algorithm)--iostream迭代器
- C++语法基础--泛型算法(generic algorithm)--写入容器元素的算法fill(),fill_n,replace()
- C++语法基础--泛型算法(generic algorithm)--对容器排序的算法sort(),stable_sort(),unique()
- STL之--插入迭代器(back_inserter,inserter,front_inserter的区别)
- C++学习笔记29——泛型算法之插入迭代器
- 插入迭代器 back_inserter、front_inserter、inserter
- C++语法基础--泛型算法(generic algorithm)--find(),find_if()
- C++语法基础--泛型算法(generic algorithm)--只读算法accumulate(),find_first_of(),count(),count_if()
- C++语法基础--泛型算法(generic algorithm)--transform
- C++语法基础--泛型算法(generic algorithm)--replace_copy(),unique_copy,copy()
- STL之--插入迭代器(back_inserter,inserter,front_inserter的区别)
- C++学习笔记:函数指针语法基础以及函数指针做函数参数的思想剖析
- C++ 插入迭代器,算法使用
- 【c++基础】虚函数的使用以及和成员函数的区别
- C++语法基础--动态绑定,派生类,虚函数,覆盖虚函数机制
- C++STL总结(附带容器迭代器泛型算法详情链接【未完成】)
- c++基础知识,有现成的函数不用,非的自己设计算法,这不是坑爹吗?
- 从零开始学C++之STL(十):迭代器适配器{(插入迭代器back_insert_iterator)、IO流迭代器(istream_iterator、ostream_iterator)}
- JavaScript基础(基本语法:变量、语句、数组、函数、对象、全局方法以及js与java不同,js与html关联)