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;
}
运行结果显示如下
相关推荐
艾莉丝努力练剑1 小时前
【C++:异常】C++ 异常处理完全指南:从理论到实践,深入理解栈展开与最佳实践
java·开发语言·c++·安全·c++11
快乐zbc8 小时前
【C++ 基础】:给定一个指针 p,你能判断它是否指向合法的对象吗?
c++
岁忧8 小时前
GoLang五种字符串拼接方式详解
开发语言·爬虫·golang
tyatyatya8 小时前
MATLAB基础数据类型教程:数值型/字符型/逻辑型/结构体/元胞数组全解析
开发语言·matlab
sulikey8 小时前
C++类和对象(下):初始化列表、static、友元、内部类等核心特性详解
c++·static·初始化列表·友元·匿名对象·内部类·编译器优化
心无旁骛~8 小时前
python多进程和多线程问题
开发语言·python
星云数灵8 小时前
使用Anaconda管理Python环境:安装与验证Pandas、NumPy、Matplotlib
开发语言·python·数据分析·pandas·教程·环境配置·anaconda
kaikaile19958 小时前
基于遗传算法的车辆路径问题(VRP)解决方案MATLAB实现
开发语言·人工智能·matlab
四问四不知9 小时前
Rust语言进阶(结构体)
开发语言·后端·rust
q***9949 小时前
index.php 和 php
开发语言·php