#include <sstream>
1 std::istringstream
2 std::stringstream
1 std::istringstream
input
1 在一个字符串string里提取部分数据,这些数据以空格' '为间隔。
以空格' '为间隔,可以直接提取
#include <iostream>
#include <sstream> struct MyStruct
{
std::string str1, str2, str3;
double db;
int num;
char ch;
}; void main()
{
std::string mystring("china google microsoft 12.9 123 A");//创建一个字符串,用于提取
MyStruct struct1;//创建一个结构体,用于保存结果 std::istringstream input(mystring);//创建一个字符串扫描流
input >> struct1.str1 >> struct1.str2 >> struct1.str3 >> struct1.db >> struct1.num >> struct1.ch;//扫描 std::cout << struct1.str1 << std::endl;//打印
std::cout << struct1.str2 << std::endl;
std::cout << struct1.str3 << std::endl;
std::cout << struct1.db << std::endl;
std::cout << struct1.num << std::endl;
std::cout << struct1.ch << std::endl; system("pause");
}
2 在一个字符串string里提取部分数据,这些数据以'#'为间隔。
以'#'为间隔,要多处理一步,先把'#'查找和替换成空格' ',再提取
#include <iostream>
#include <sstream> struct MyStruct
{
std::string str1, str2, str3;
double db;
int num;
char ch;
}; void main()
{
char mystring[] = "china#google#microsoft#12.9#123#A";//创建一个字符串,用于提取 for (char *p = mystring; *p != '\0'; p++)//查找和替换#
{
if (*p == '#')
{
*p = ' ';
}
} MyStruct struct1;//创建一个结构体,用于保存结果 std::istringstream input(mystring);//创建一个字符串扫描流
input >> struct1.str1 >> struct1.str2 >> struct1.str3 >> struct1.db >> struct1.num >> struct1.ch;//扫描 std::cout << struct1.str1 << std::endl;//打印
std::cout << struct1.str2 << std::endl;
std::cout << struct1.str3 << std::endl;
std::cout << struct1.db << std::endl;
std::cout << struct1.num << std::endl;
std::cout << struct1.ch << std::endl; system("pause");
}
2 std::stringstream
使用&拼接字符串,执行两个system命令
#include <iostream>
#include <sstream> void main()
{
std::stringstream mystr;//字符串进行输入 char cmd1[] = { };//创建字符串
char cmd2[] = { };//创建字符串 std::cin.getline(cmd1, ).getline(cmd2, );//字符串赋值 mystr << cmd1 << '&' << cmd2;//输出到mystr std::string str = mystr.str();//转换 system(str.c_str());//执行 system("pause");
}
#include <sstream>的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include
问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- Mybatis常用总结:参数,返回,执行sql,include等
1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...
- jsp中的@include与jsp:include区别详解
1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...
- JSP中编译指令include与动作指令include的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...
- C/C++ 中的include
当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...
- 织梦多语言站点,{dede:include filename=''/}引入问题
织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...
- PHP 站点相对包含,路径的问题解决方法(include,require)
以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...
- 如何让include标签包裹的布局置于屏幕最下方?
如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性 a ...
随机推荐
- URL,URLConnection,HttPURLConnection的使用
URLConnection与HttPURLConnection都是抽象类,无法直接实例化对象.其对象主要通过URL的openconnection方法获得. 值得注意的是:1.openConnectio ...
- PowerDesigner15在生成SQL时报错Generation aborted due to errors detected during the verification of the mod
转载: http://blog.csdn.net/successful555/article/details/7582154 PowerDesigner中如何设置字符编码为GBK或者GB2312 ht ...
- 国内常用ntp服务器ip地址
ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)s1a.time.edu.cn 北京邮电大学s1b.time.edu.cn 清华大学s1c.time ...
- [转!]jQuey中的return false作用是什么
jQuey中的return false作用是什么: 在众多的语句中都有return false的使用,当然对于熟悉它的开发者来说,当然是知根知底,知道此语句的作用,当然也就知道在什么时候使用此语句,不 ...
- OAuth2.0认证介绍
OAuth2.0鉴权 返回 目录 [隐藏] 1 腾讯微博OAuth2.0认证介绍 2 获取accesstoken的两种方式 2.1 1.Authorization code grant 2.1.1 第 ...
- <!--转换office时需要此配置 --> <identity impersonate="true" />
1.需要对Office 进行操作时 ,添加权限 <!--转换office时需要此配置 --> <identity impersonate="true" /> ...
- 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件
一.控制ChartControl的Y轴范围 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件,具体代码如下: ...
- .NET程序性能优化基本要领
想了解更多关于新的编译器的信息,可以访问 .NET Compiler Platform ("Roslyn") 基本要领 在对.NET 进行性能调优以及开发具有良好响应性的应 ...
- 《编写可维护的JavaScript》之编程实践
最近读完<编写可维护的JavaScript>,让我受益匪浅,它指明了编码过程中,需要注意的方方面面,在团队协作中特别有用,可维护性是一个非常大的话题,这本书是一个不错的起点. 本书虽短,却 ...
- DropdownList的处理总结
创建一: List<SelectListItem> items = new List<SelectListItem>() { new SelectListItem(){Text ...