浅谈算法back_inserter,front_inserter,inserter

//23:44 2010/6/6
……………………………浅谈算法back_inserter,front_inserter,inserter……………………………
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int ia[]={1,2,3,4,100,5,100};
    vector<int> ivec(ia,ia+7);
    list<int> ilst;

    //replace_copy(ivec.begin(),ivec.end(),inserter(ilst,ilst.begin()),100,0);

    //replace_copy(ivec.begin(),ivec.end(),back_inserter(ilst),100,0);
    //replace_copy(ivec.begin(),ivec.end(),front_inserter(ilst),100,0);

   
    cout<<"list: "<<endl;
    for(list<int>::iterator iter=ilst.begin();iter!=ilst.end();++iter)
    cout<<*iter<<" ";
    cout<<endl;

    return 0;
    }
注:以上代码的三个//后面的代码分别表示不同的功能,调试的时候可以依次去掉,观察结果的不同。

上面这一段代码是《C++ Primer》第四版中习题11.14的解答题答案。
以上三个结果执行结果依次是    1  2  3  4  0  5  0
                              1  2  3  4  0  5  0
                              0  5  0  4  3  2  1
这段代码主要在于帮助理解inserter(),back_inserter(),front_inserter()三个函数实现的不同功

能。
要来理解三个函数就要先知道replace_copy()是做什么的,replace_copy 算法将范围 [first, last)

中所有元素都复制到结果,那么中间的inserter(),back_inserter(),front_inserter()这三个分别

有什么区别呢,看上面的运行结果,大家很容易就看出来了吧,第三个也就是front_inserter()是反向

输出的,为什么呢,应该这样子了理解:
front_inserter()是实现总是在输出序列的前端实现插入,就是说第一个输出的是1,第二个是2,但是

这个2不是在1的后面,而是前面,于是就是2  1,以此类推就不难理解为什么显示结果是0  5  0  4 

3  2  1了。
back_inserter()是实现总是在输出序列的末端实现插入,所以也不难理解输出结果是1  2  3  4  0 

5  0了。
OK,接下来时inserter(),大家看到上面的程序运行结果第一个和第二个貌似没有区别,为了更好更形象

的理解,我把上面的代码稍微改动几个地方

#include<iostream>
#include<list>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int ia[]={1,2,3,4,100,5,100};
    vector<int> ivec(ia,ia+7);


    list<int> ilst(7,0);

    replace_copy(ivec.begin(),ivec.end(),inserter(ilst,ilst.begin()),100,0);
    //replace_copy(ivec.begin(),ivec.end(),inserter(ilst,ilst.begin()),100,0);
    //replace_copy(ivec.begin(),ivec.end(),inserter(ilst,++ilst.end()),100,0);
   

    cout<<"list: "<<endl;
    for(list<int>::iterator iter=ilst.begin();iter!=ilst.end();++iter)
    cout<<*iter<<" ";
    cout<<endl;


    return 0;
    }


注:本段代码中初始化了ilst为七个0的容器。

执行结果依次是 1  2  3  4  0  5  0  0  0  0  0  0  0  0
               0  1  2  3  4  0  5  0  0  0  0  0  0  0
               0  0  0  0  0  0  0  1  2  3  4  0  5  0

这下应该很容易的掌握了back_inserter,front_inserter,inserter着三个算法的区别了吧。

 


//fromlinge@gmail.com 
//fromlinge的C++交流群 8278237

1. 可以使用容器的构造函数来初始化容器,例如: ```c++ int arr[] = {1, 2, 3, 4, 5}; vector<int> v(arr, arr + 5); ``` 这里使用了 vector 的构造函数,第一个参数表示起始地址,第二个参数表示结束地址。 2. 迭代器是一个用来遍历容器中元素的对象。前插型迭代器 front_insert_iterator 和后插型迭代器 back_insert_iterator 用来在容器的前端和后端插入元素。使用前插型迭代器和后插型迭代器需要满足容器支持插入操作,例如 vector、deque、list 等。定义方法如下: ```c++ #include <iterator> #include <vector> #include <iostream> using namespace std; int main() { vector<int> v; front_insert_iterator<vector<int>> fii(v); back_insert_iterator<vector<int>> bii(v); for (int i = 0; i < 5; i++) { *fii = i; *bii = i; } for (auto x : v) { cout << x << " "; } cout << endl; return 0; } ``` 3. for_each 算法可以用来访问容器中的元素,需要传入一个函数对象作为参数。函数对象可以是一个函数指针、函数对象、lambda 表达式等。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; void print(int x) { cout << x << " "; } int main() { vector<int> v = {1, 2, 3, 4, 5}; for_each(v.begin(), v.end(), print); cout << endl; return 0; } ``` 4. copy_if 算法用于将满足条件的元素拷贝到另一个容器中,需要传入源容器的起始和结束位置、目标容器的起始位置、以及一个谓词函数作为参数。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; bool is_odd(int x) { return x % 2 == 1; } int main() { vector<int> v = {1, 2, 3, 4, 5}; vector<int> v2; copy_if(v.begin(), v.end(), back_inserter(v2), is_odd); for (auto x : v2) { cout << x << " "; } cout << endl; return 0; } ``` 5. 自定义类型可以重载比较运算符(<、==、> 等)和流插入运算符(<<),以便在容器中进行排序、输出等操作。重载的目的是为了支持自定义类型的比较和输出。例如: ```c++ #include <iostream> #include <string> using namespace std; class Student { public: string name; int score; bool operator < (const Student& other) const { return score < other.score; } bool operator == (const Student& other) const { return score == other.score; } bool operator > (const Student& other) const { return score > other.score; } friend ostream& operator << (ostream& os, const Student& s) { os << s.name << ": " << s.score; return os; } }; int main() { Student s1 = {"Tom", 80}; Student s2 = {"Jerry", 90}; cout << (s1 < s2) << endl; cout << (s1 == s2) << endl; cout << (s1 > s2) << endl; cout << s1 << endl; return 0; } ``` 6. 自定义类型可以使用 max_element、min_element、max、min 等算法进行操作,前提是要满足比较运算符的定义。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; class Student { public: string name; int score; bool operator < (const Student& other) const { return score < other.score; } bool operator == (const Student& other) const { return score == other.score; } bool operator > (const Student& other) const { return score > other.score; } }; int main() { vector<Student> v = {{"Tom", 80}, {"Jerry", 90}, {"Alice", 70}}; auto it = max_element(v.begin(), v.end()); cout << *it << endl; return 0; } ``` 7. generate 算法可以用来生成容器中的元素,需要传入一个函数对象作为参数。函数对象可以是一个函数指针、函数对象、lambda 表达式等。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; int fib(int n) { if (n == 0 || n == 1) { return 1; } return fib(n - 1) + fib(n - 2); } int main() { vector<int> v(10); generate(v.begin(), v.end(), []() {return rand() % 100;}); for (auto x : v) { cout << x << " "; } cout << endl; generate(v.begin(), v.end(), fib); for (auto x : v) { cout << x << " "; } cout << endl; return 0; } ``` 8. remove 和 remove_if 算法用于将满足条件的元素移动到容器的末尾,并返回移动后的末尾位置。需要使用 erase 算法来真正删除元素。例如: ```c++ #include <vector> #include <algorithm> #include <iostream> using namespace std; bool is_odd(int x) { return x % 2 == 1; } int main() { vector<int> v = {1, 2, 3, 4, 5}; auto it = remove(v.begin(), v.end(), 3); v.erase(it, v.end()); for (auto x : v) { cout << x << " "; } cout << endl; vector<int> v2 = {1, 2, 3, 4, 5}; auto it2 = remove_if(v2.begin(), v2.end(), is_odd); v2.erase(it2, v2.end()); for (auto x : v2) { cout << x << " "; } cout << endl; return 0; } ``` 9. 有序序列的并集、交集、差集等运算可以使用 set 的集合运算操作来实现。例如: ```c++ #include <set> #include <algorithm> #include <iostream> using namespace std; int main() { set<int> s1 = {1, 2, 3, 4}; set<int> s2 = {3, 4, 5, 6}; set<int> s3; set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3, s3.begin())); for (auto x : s3) { cout << x << " "; } cout << endl; set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3, s3.begin())); for (auto x : s3) { cout << x << " "; } cout << endl; set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3, s3.begin())); for (auto x : s3) { cout << x << " "; } cout << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值