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. Benchmarking Apache Kafka: 2 Million Writes Per Second (On Three Cheap Machines)

    I wrote a blog post about how LinkedIn uses Apache Kafka as a central publish-subscribe log for inte ...

  2. netstat Recv-Q和Send-Q

    通过netstat -anp可以查看机器的当前连接状态:   Active Internet connections (servers and established) Proto Recv-Q Se ...

  3. axios请求本地的json文件在打包部署到子目录域名下,路径找不到

    前言: 因为要同时部署两个项目,有一个是部署到域名下面的子目录下,如:https://xxx.com/siot-admin vue 项目中使用axios请求了本地项目的static文件夹下的json文 ...

  4. Koa 中 ejs 模板的使用

    ejs的基本使用 安装 koa-views 和 ejs npm install --save koa-views/cnpm install --save koa-views npm install e ...

  5. MAC oh-my-zsh

    效果图 step1 : 安装zsh    brew install zsh step2: sudo vim  /etc/shells 添加 /usr/local/bin/zsh  step3:安装oh ...

  6. YAML详解

    1   YAML简介 YAML,即YAML Ain’t Markup Language的缩写,YAML 是一种简洁的非标记语言.YAML以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁 ...

  7. 关于H5页面中生成图片的两种方式!

    前言: 我们在做项目过程中,经常会遇到自定义生成一张图片并可以长按保存.长按保存图片在微信等浏览器中默认就有,那么对于生成图片的有哪些方式呢? 方法一: 利用canvas绘制图形,然后生成图片 代码如 ...

  8. jQuery 事件对象的属性

    jQuery 在遵循 W3C 规范的情况下,对事件对象的常用属性进行了封装,使得事件处理在各大浏览器下都可以正常运行而不需要进行浏览器类型判断. (1) event.type 该方法的作用是可以获取到 ...

  9. 老婆大人 split,slice,splice,replace的用法

    split()方法用于把一个字符串分割成字符串数组 str.split("字符串/正则表达式从该参数制定额地方分割str",可选,可指定返回数组的最大长度,如果没设置参数,整个字符 ...

  10. Spring-framework

    1.spring注解驱动开发 官方文档 @Configuration 告诉spring这是一个配置类,配置类=配置文件 @Bean 给容器中注入一个bean,类型为返回值类型,id默认用方法名作为id ...