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;
}
运行结果显示如下
相关推荐
精英的英20 分钟前
记一次 Qt5 Language Server 开发
开发语言·vscode·qt
烧酒同学30 分钟前
【C++】记录size of std::vector的巧妙坑
开发语言·c++·图形渲染
周周哈哈哈43 分钟前
线程创建、执行、退出、回收
java·开发语言
gb42152871 小时前
python中Web应用服务器
开发语言·前端·python
地平线开发者1 小时前
量化为什么会有损失:从量化误差到精度下降
算法
手写码匠1 小时前
华为云Flexus+DeepSeek征文|Dify 多 Agent 会话管理与上下文工程实战:让智能体“记住该记住的,忘掉该忘掉的“
人工智能·深度学习·算法·aigc
2401_862880821 小时前
数据结构 --- 栈
c语言·数据结构·算法
萧瑟余晖2 小时前
Java深入解析篇三十六之分布式系统详解
java·开发语言·分布式
致Great2 小时前
Qwen3.8-27B 接入 DSH 全流程实测:写代码、跑长任务,都没问题
算法
ZJU_统一阿萨姆2 小时前
【推理优化进阶】调度器的数学内核:排队论、SLO 与在线决策
开发语言·人工智能·语言模型·系统架构·vllm