1.输出“输入的内容”

// basic file operations
#include <iostream>
#include <fstream>
#include <string>
using namespace std; std::istream& func(std::istream &is)
{
std::string buf;
while (is >> buf)
std::cout << "output = " << buf << std::endl;
is.clear();
return is;
} int main() {
std::istream &is=cin;
func(is);
}

2.istringstream

#include <iostream>
#include <sstream>
using std::istream; istream& func(istream &is)
{
std::string buf;
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
} int main()
{
std::istringstream iss("hello");
func(iss); system("pause");
return ;
}

3.ReadFileToVec——将每一行储存成为vector元素

#include <fstream>
#include <string>
#include <vector>
#include <iostream> using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl; void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (std::getline(ifs, buf))
vec.push_back(buf);
}
} int main()
{
vector<string> vec;
ReadFileToVec("cww.txt", vec);
for (const auto &str : vec)
cout << str << endl;
system("pause");
return ;
}

将每个单词存储到vector元素:

#include <fstream>
#include <string>
#include <vector>
#include <iostream> using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl; void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (ifs >> buf)
vec.push_back(buf);
}
} int main()
{
vector<string> vec;
ReadFileToVec("cww.txt", vec);
for (const auto &str : vec)
cout << str << endl;
system("pause");
return ;
}

4.除非有必须使用其他容器的理由,否则就用Vector或List。

5.

【cpp】chap 8的更多相关文章

  1. 【CPP】字符串和格式化输入输出

    前导:数组(array),字符串转换说明符%s,定义符号常量,,strlen()获取字符串长度,. [字符串] 没有专门的字符串类型,是吧他存储在字符型数组中,数组最后一个字符为空字符'\0',c用他 ...

  2. 【CPP】数据和C

    %f是浮点型的占位符,%f.2表示显示到小数点后两位,.2称为修饰词 变量可以在程序执行过程中变化和指定,而常量不可以. [数据类型关键字]int long short unsigned char  ...

  3. 【CPP】概览

    [使用C语言的七个步骤]1:定义程序目标  2:设计程序  3:编写代码  4:编译  5:运行  6:测试和调试  7:维护和修改 [程序细节] :#include 指示和头文件 include&l ...

  4. 【cpp】Vector

    这vector 很有用 // compile with: /EHsc #include <vector> #include <iostream> int main() { us ...

  5. opencv读取图像输入到tensorflow模型中进行运算【cpp】

    void TransformMatToTensor(const cv::Mat &image, Tensor &input_tensor, int input_width, int i ...

  6. 【cpp】new delete

    double *M = new double[2*num]; double *T = new double[2 * num]; double *activeM = new double[2 * num ...

  7. 【转】linux configure报错configure: error: C++ preprocessor “/lib/cpp” fails sanity 的解决办法

    /lib/cpp fails sanity check的解决 在某些软件的时候,运行./configure 会报错,错误提示为: configure: error: C++ preprocessor ...

  8. 【转】Qt 资源图片删除后,错误 needed by `debug/qrc_image.cpp'. Stop. 的终极解决办法

    @2019-06-13 [小记] Qt项目做完了把资源文件夹下已经不用的图片文件删掉,运行时报错(编译不报错):No rule to make target `images/图片文件名', neede ...

  9. Python高手之路【三】python基础之函数

    基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...

随机推荐

  1. SQL MID() 函数

    MID() 函数 MID 函数用于从文本字段中提取字符. SQL MID() 语法 SELECT MID(column_name,start[,length]) FROM table_name 参数 ...

  2. (四)Exploring Your Cluster

    The REST API Now that we have our node (and cluster) up and running, the next step is to understand ...

  3. Java Scanner 类

    下面是创建 Scanner 对象的基本语法: Scanner s = new Scanner(System.in); Scanner -是java类库的一个基础类,一个可以使用正则表达式来解析基本类型 ...

  4. 程序员修神之路--🤠分布式高并发下Actor模型如此优秀🤠

    写在开始 一般来说有两种策略用来在并发线程中进行通信:共享数据和消息传递.使用共享数据方式的并发编程面临的最大的一个问题就是数据条件竞争.处理各种锁的问题是让人十分头痛的一件事. 传统多数流行的语言并 ...

  5. 让linux启动更快的方法

    导读 进行 Linux 内核与固件开发的时候,往往需要多次的重启,会浪费大把的时间. 在所有我拥有或使用过的电脑中,启动最快的那台是 20 世纪 80 年代的电脑.在你把手从电源键移到键盘上的时候,B ...

  6. PS快速调出天蓝色清新外景

    原片: 一.调整光比 曝光 黑白灰. 二.调整色温(新手可用白平衡工具.左上角第3个)调整饱和度(自然饱和度和蓝原色) 三.互补色的运用(高光偏黄 加的蓝色 暗部发蓝青色 加的橙黄色) 四.调整好照片 ...

  7. 解决import模块后提示无此模块的问题

    最近在工作中发现一个奇怪的问题: 明明已经装上了,但是还提示找不到该模块,没办法,我又去site-package文件下面看了: 发现Linux下自带的python2.7里面装上了该模块(我在root用 ...

  8. C#中使用JavaScriptSerializer类实现序列化与反序列化

    1.添加引用 JavaScriptSerializer类的使用需要引用System.Web.Extensions.dll文件,根据路径:C:\Program Files (x86)\Reference ...

  9. C#中字符串的字面值(转义序列)

    在程序开发中,经常会碰到在字符串中字面值中使用转义序列,下面表格收集了下转义序列的完整列表,以便大家查看引用: 转义序列列表 转义序列 产生的字符 字符的Unicode值 \' 单引号 0x0027 ...

  10. BEX5下新建任务到待办任务

    List<OrgUnit> list = new ArrayList<OrgUnit>(); // 1.查询出执行者 String sql = "select t.f ...