【cpp】chap 8
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的更多相关文章
- 【CPP】字符串和格式化输入输出
		前导:数组(array),字符串转换说明符%s,定义符号常量,,strlen()获取字符串长度,. [字符串] 没有专门的字符串类型,是吧他存储在字符型数组中,数组最后一个字符为空字符'\0',c用他 ... 
- 【CPP】数据和C
		%f是浮点型的占位符,%f.2表示显示到小数点后两位,.2称为修饰词 变量可以在程序执行过程中变化和指定,而常量不可以. [数据类型关键字]int long short unsigned char ... 
- 【CPP】概览
		[使用C语言的七个步骤]1:定义程序目标 2:设计程序 3:编写代码 4:编译 5:运行 6:测试和调试 7:维护和修改 [程序细节] :#include 指示和头文件 include&l ... 
- 【cpp】Vector
		这vector 很有用 // compile with: /EHsc #include <vector> #include <iostream> int main() { us ... 
- opencv读取图像输入到tensorflow模型中进行运算【cpp】
		void TransformMatToTensor(const cv::Mat &image, Tensor &input_tensor, int input_width, int i ... 
- 【cpp】new delete
		double *M = new double[2*num]; double *T = new double[2 * num]; double *activeM = new double[2 * num ... 
- 【转】linux configure报错configure: error: C++ preprocessor “/lib/cpp” fails sanity 的解决办法
		/lib/cpp fails sanity check的解决 在某些软件的时候,运行./configure 会报错,错误提示为: configure: error: C++ preprocessor ... 
- 【转】Qt 资源图片删除后,错误 needed by `debug/qrc_image.cpp'. Stop. 的终极解决办法
		@2019-06-13 [小记] Qt项目做完了把资源文件夹下已经不用的图片文件删掉,运行时报错(编译不报错):No rule to make target `images/图片文件名', neede ... 
- Python高手之路【三】python基础之函数
		基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ... 
随机推荐
- .net 调用java service 代理类方法
			通过Svcutil.exe 工具生成代理类调用 1.找到如下地址“C:\Windows\System32\cmd.exe” 命令行工具,右键以管理员身份运行(视系统是否为win7 而定) 2 ... 
- ubuntu创建idea桌面快捷方式
			This method can be used to create a launcher for any application, not just IntelliJ IDEA. For any la ... 
- ZooKeeper Dynamic Reconfiguration (dynamicConfigFile) ZooKeeper动态配置
			有人翻译的地址:https://www.cnblogs.com/dupang/p/5649843.html ZooKeeper Dynamic Reconfiguration Overview Cha ... 
- 为什么要使用TypeScript开发Web应用程序
			TypeScript仍然相对较新,但已经赢得了很多信徒.继续阅读,看看这种很酷的语言的一些最好的功能. 定义TypeScript TypeScript是由Microsoft Corporation开发 ... 
- grep -v、-e、-E
			在Linux的grep命令中如何使用OR,AND,NOT操作符呢? 其实,在grep命令中,有OR和NOT操作符的等价选项,但是并没有grep AND这种操作符.不过呢,可以使用patterns来模拟 ... 
- ansible 与 Jinja2的结合
			1.文件架构 [root@master template]# tree . ├── jinj2_test.yml ├── meta ├── tasks ├── templates │ └── te ... 
- iOS开发基础-九宫格坐标(6)
			继续对iOS开发基础-九宫格坐标(5)中的代码进行优化. 优化思路:把字典转模型部分的数据处理操作也拿到模型类中去实现,即将 ViewController 类实现中 apps 方法搬到 WJQAppI ... 
- 基于 WebGL 3D 的 HTML5 档案馆可视化管理系统
			前言 档案管理系统是通过建立统一的标准以规范整个文件管理,包括规范各业务系统的文件管理的完整的档案资源信息共享服务平台,主要实现档案流水化采集功能.为企事业单位的档案现代化管理,提供完整的解决方案,档 ... 
- python部署lvs
			lvs-dr-rr import paramiko vip = '192.168.254.250' ds = '192.168.254.17' rs1 = '192.168.254.37' rs2 = ... 
- hdu4746 Mophues (莫比乌斯进阶)
			参考博客:https://blog.csdn.net/acdreamers/article/details/12871643 题意:满足1<=x<=n,1<=y<=m,并且gc ... 
