C++ //练习 10.29 编写程序,使用流迭代器读取一个文本文件,存入一个vector中的string里。

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

练习 10.29 编写程序,使用流迭代器读取一个文本文件,存入一个vector中的string里。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex10.29.cpp
	> Author: 
	> Mail: 
	> Created Time: Mon 04 Mar 2024 10:43:46 AM CST
 ************************************************************************/

#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<iterator>
#include<algorithm>
#include<functional>
using namespace std;

int main(){
    vector<string> str;
    ifstream in ("10.29.txt");
    istream_iterator<string> str_it(in);
    istream_iterator<string> eof;

    while(str_it != eof){
        str.push_back(*str_it++);
    }

    for(const auto s : str){
        cout<<s<<" ";
    }
    cout<<endl;

    return 0;
}
运行结果显示如下
相关推荐
_wyt00138 分钟前
完全背包问题详解
c++·背包dp
小钻风33661 小时前
Spring Boot 文件上传详解:深入理解 MultipartFile 的使用与原理
java·开发语言
qizayaoshuap2 小时前
# ❌ 井字棋 — 鸿蒙ArkTS Minimax AI算法与博弈系统设计
人工智能·算法·华为·harmonyos
qq_452396232 小时前
第二篇:《Go 开发环境搭建:SDK、IDE、Module 与 Hello World》
开发语言·ide·golang
天青色等烟雨..2 小时前
全流程ArcGISPro空间分析、三维建模、可视化及Python融合应用技术
开发语言·python
丰锋ff2 小时前
1.2 Qt常用基础类型
开发语言·qt
皓月斯语2 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
atunet2 小时前
树状结构在查询优化中的作用与实现细节7
算法
徐凤年_3 小时前
rog_map参数理解
算法
春日见3 小时前
算法与数据结构----哈希表
数据结构·人工智能·算法·机器学习·自动驾驶·哈希算法·散列表