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>的更多相关文章

  1. 浅谈JSP中include指令与include动作标识的区别

    JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...

  2. Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include

    问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...

  3. error RC1015: cannot open include file 'afxres.h' 解决办法

    在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...

  4. Mybatis常用总结:参数,返回,执行sql,include等

    1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...

  5. jsp中的@include与jsp:include区别详解

    1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...

  6. JSP中编译指令include与动作指令include的区别

    include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...

  7. C/C++ 中的include

    当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...

  8. 织梦多语言站点,{dede:include filename=''/}引入问题

    织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...

  9. PHP 站点相对包含,路径的问题解决方法(include,require)

    以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...

  10. 如何让include标签包裹的布局置于屏幕最下方?

    如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性  a ...

随机推荐

  1. 无意发现vim里插入模式可以借助Alt键输入一些特殊字符

    无意发现vim里插入模式可以借助Alt键输入一些特殊字符.如: Alt+w: ÷ Alt+:: » Alt+f  :  æ Alt+ . :  ® Alt+ ? :  ¯...

  2. C++利用指针突破私有成员访问限制

    C++ 面向对象的一大特性就是封装,使用不同的访问控制符来控制外接对其的访问权限.比如: 1 class A 2 { 3 public: 4 A(): i(10){} 5 void print(){ ...

  3. 四大图像库:OpenCV/FreeImage/CImg/CxImage

    1.对OpenCV 的印象:功能十分的强大,而且支持目前先进的图像处理技术,体系十分完善,操作手册很详细,手册首先给大家补计算机视觉的知识,几乎涵盖了近10年内的主流算法: 然后将图像格式和矩阵运算, ...

  4. 当浏览器不支持placeholder,所执行的函数

    $(function(){ //判断浏览器是否支持placeholder属性 supportPlaceholder='placeholder'in document.createElement('in ...

  5. poj2387-Til the Cows Come Home dijkstra获得水的问题

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29539   Accepted ...

  6. iOS 三维变换

    1:平移 一个4*4的单位矩阵乘以一个P(x,y,z,1)的行向量,则表示此矩阵向x轴移动了x的单位,向Y轴移动了y个单位,向Z轴移动了z个单位,最后获得移动后的目标矩阵是 [ 1, 0, 0, 0  ...

  7. 6T GPT 移动硬盘在linux下的挂载

    实验室拿来了一个6T的移动硬盘,拿到后没有分区就直接用了,在Windows上用的好好的,插到上Linux后起初不会挂载,折腾了一会,成功挂载,很简单. 运行fdisk –l后,显示如下: 很明显,sd ...

  8. To restore the database on a new host-将数据库恢复至一个新的主机上

    To restore the database on a new host:1. Ensure that the backups of the target database are accessib ...

  9. Oracle的汉字转拼音首字母的函数

    CREATE OR REPLACE FUNCTION F_PINYIN(P_NAME IN VARCHAR2) RETURN VARCHAR2 AS V_COMPARE VARCHAR2(100); ...

  10. JSON 解析第三方框架

    常见的 JSON 解析第三方框架 JSONKit(最快) SBJson TouchJSON 以上三个框架的性能依次降低! 介绍 JSONKit 第三方框架的目的 JSON 的解析并不是表面上那么简单 ...