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;
}
运行结果显示如下
相关推荐
草莓熊Lotso36 分钟前
Qt 进阶核心:UI 开发 + 项目解析 + 内存管理实战(从 Hello World 到对象树)
运维·开发语言·c++·人工智能·qt·ui·智能手机
2501_9418656341 分钟前
从事件驱动到异步架构的互联网工程语法构建与多语言实践分享
java·开发语言·jvm
前端 贾公子4 小时前
v-if 与 v-for 的优先级对比
开发语言·前端·javascript
嗯嗯=5 小时前
python学习篇
开发语言·python·学习
TonyLee0177 小时前
LLVM安装(ubuntu22)
c++
Swift社区7 小时前
LeetCode 465 最优账单平衡
算法·leetcode·职场和发展
不会c嘎嘎7 小时前
QT中的常用控件 (二)
开发语言·qt
聆风吟º7 小时前
【数据结构手札】空间复杂度详解:概念 | 习题
java·数据结构·算法
weixin_445054727 小时前
力扣热题51
c++·python·算法·leetcode
是一个Bug8 小时前
50道核心JVM面试题
java·开发语言·面试