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;
}
运行结果显示如下
相关推荐
jojo_zjx2 分钟前
GESP 24年12月2级 数位和
c++
自由的好好干活2 分钟前
PCI9x5x驱动移植支持PCI9054在win7下使用3
c++·驱动开发
techdashen3 分钟前
Rust OnceCell 深度解析:延迟初始化的优雅解决方案
开发语言·oracle·rust
踢足球09295 分钟前
寒假打卡:2026-01-24
数据结构·算法
少控科技10 分钟前
QT新手日记033
开发语言·qt
亲爱的非洲野猪25 分钟前
动态规划进阶:多维DP深度解析
算法·动态规划
王九思35 分钟前
Java 内存分析工具 MAT
java·开发语言·安全
AlenTech41 分钟前
197. 上升的温度 - 力扣(LeetCode)
算法·leetcode·职场和发展
superman超哥1 小时前
Serde 的零成本抽象设计:深入理解 Rust 序列化框架的哲学
开发语言·rust·开发工具·编程语言·rust序列化
夕除1 小时前
java--2
java·开发语言