C++ //练习 10.37 给定一个包含10个元素的vector,将位置3到7之间的元素按逆序拷贝到一个list中。

C++ Primer(第5版) 练习 10.37

练习 10.37 给定一个包含10个元素的vector,将位置3到7之间的元素按逆序拷贝到一个list中。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex10.37.cpp
	> Author: 
	> Mail: 
	> Created Time: Thu 14 Mar 2024 03:38:34 PM CST
 ************************************************************************/

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

int main(){
    vector<int> number;
    int num;
    cout<<"Enter numbers: ";
    while(cin>>num){
        number.push_back(num);
        if(cin.get() == '\n'){
            break;
        }
    }

    cout<<"Vector numbers: ";
    for(auto n : number){
        cout<<n<<" ";
    }
    cout<<endl;

    list<int> lst;
    for(auto iter = number.crbegin() + 4; iter != number.crend() - 3; ++iter){
        lst.push_back(*iter);
    }
    cout<<"List numbers: ";
    for(auto l : lst){
        cout<<l<<" ";
    }
    cout<<endl;

    return 0;
}
运行结果显示如下
相关推荐
_w_z_j_几秒前
Linux----线程互斥与同步
linux·运维·开发语言
云栖梦泽2 分钟前
易语言网络编程基础:构建网络版应用
开发语言
Maỿbe11 分钟前
力扣hot图论部分
算法·leetcode·图论
雍凉明月夜13 分钟前
c++ 精学笔记记录Ⅲ
c++·笔记·学习
LYFlied19 分钟前
【每日算法】LeetCode 78. 子集
数据结构·算法·leetcode·面试·职场和发展
月明长歌23 分钟前
【码道初阶】【Leetcode606】二叉树转字符串:前序遍历 + 括号精简规则,一次递归搞定
java·数据结构·算法·leetcode·二叉树
子枫秋月24 分钟前
C++字符串操作与迭代器解析
数据结构·算法
鹿角片ljp24 分钟前
力扣234.回文链表-反转后半链表
算法·leetcode·链表
(●—●)橘子……25 分钟前
记力扣1471.数组中的k个最强值 练习理解
数据结构·python·学习·算法·leetcode
oioihoii28 分钟前
C++共享内存小白入门指南
java·c++·算法