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;
}
运行结果显示如下
相关推荐
怪兽学LLM几秒前
LeetCode 438 找到字符串中所有字母异位词(Python 固定滑动窗口+字符计数解法)
python·算法·leetcode
满怀冰雪6 分钟前
第04篇-双指针算法-从有序数组到回文判断的高频解法
java·算法
CC数学建模6 分钟前
2026年江西省研究生数学建模竞赛1题:空间数据分析中的过拟合识别完整思路、代码、模型、文章,全网首发高质量分享!
python·算法·数学建模
leo__52014 分钟前
MATLAB实现牧羊人算法
开发语言·算法·matlab
视觉小萌新18 分钟前
C++利用libmicrohttpd制作交互网页端——C1
java·c++·交互
Gauss松鼠会20 分钟前
【GaussDB】GaussDB SMP特性调优详解
java·服务器·前端·数据库·sql·算法·gaussdb
Tisfy24 分钟前
LeetCode 3689.最大子数组总值 I:What The Medium
算法·leetcode·题解·贪心·模拟·脑筋急转弯
fpcc25 分钟前
C++编程实践—C++实现类似Qt的信号槽机制
c++·qt
葬送的代码人生25 分钟前
JavaScript 数组完全指南:从入门到实战
前端·javascript·算法
格发许可优化管理系统27 分钟前
Mentor许可证使用规定全解析
java·大数据·c语言·开发语言·c++