C++:判断拷贝元素的顺序

问题

一个vector中保存1到9,将其拷贝到三个其他容器中。使用inserter,
back_inserter和front_inserter将元素添加到三个容器中,
对每种inserter估计输出序列是怎样的,运行程序验证你的估计是否正确.

C++.
image-2120

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*************************************************************************
    > File Name: test.10.28.cpp
    > Author: puruidong
    > Mail: 1@w1520.com
    > Created Time: 2014年06月20日 星期五 23时28分11秒
 ************************************************************************/

#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<deque>
#include<forward_list>
using namespace std;

/*************************************************
 
    一个vector中保存1到9,将其拷贝到三个其他容器中。使用inserter,
    back_inserter和front_inserter将元素添加到三个容器中,
    对每种inserter估计输出序列是怎样的,运行程序验证你的估计是否正确.


 **********************************************************/

int main()
{
    vector<int> vec ={1,2,3,4,5,6,7,8,9};
   forward_list<int> fwlt ;
    list<int> lst;
    deque<int> dqe;
    cout << "使用front_inserter添加数据到forward_list,将是倒序的." << endl;
    copy(vec.begin(),vec.end(),front_inserter(fwlt));
    cout << "forward_list的内容:" << endl;
    for(const auto &fw:fwlt)
    {
        cout << fw << endl;
    }
    cout << "使用inserter添加数据到list,顺序不变." << endl;
    copy(vec.begin(),vec.end(),inserter(lst,lst.begin()));
    cout << "list的内容:" << endl;
    for(const auto &ls : lst)
    {
        cout << ls << endl;
    }
    cout << "使用back_inserter添加数据到deque不会改变顺序" << endl;
    copy(vec.begin(),vec.end(),back_inserter(dqe));
    cout << "deque的内容:" << endl;
    for(const auto &dq : dqe)
    {
        cout << dq << endl;
    }
    return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据