C++语言基础 例程 字符串流
来源:互联网 发布:做网站java还是php 编辑:程序博客网 时间:2024/06/17 02:20
贺老师的教学链接 本课讲解
例:“写”字符数组
#include<iostream>#include <strstream>using namespace std;struct student{ int num; char name[20]; float score;};int main( ){ student stud[3]= {1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90}; char c[50]; //用户定义的字符数组 ostrstream strout1(c,30); //建立输出字符串流,与数组c建立关联,缓冲区长30 for(int i=0; i<3; i++) //向字符数组c写3个学生的数据 strout1<<stud[i].num<<stud[i].name<<stud[i].score; strout1<<ends; //ends是C++的I/O操作符,插入一个′\\0′ cout<<"array c:"<<c<<endl; //显示字符数组c中的字符 ostrstream strout2(c,40); //这时,c将被重写 for(int i=0; i<3; i++) strout2<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<" "; strout2<<ends; //ends是C++的I/O操作符,插入一个′\\0′ cout<<"array c:"<<c<<endl; //显示字符数组c中的字符 return 0;}
以字符串流为中介交换数据
#include <strstream>#include<iostream>using namespace std;int main( ){ char c[50]="12 34 65 -23 -32 33 61 99 321 32"; int a[10],i,j,t; cout<<"array c:"<<c<<endl;//显示字符数组中的字符串 istrstream strin(c,sizeof(c)); //建立输入串流对象strin并与字符数组c关联 for(i=0; i<10; i++) strin>>a[i]; //从字符数组c读入10个整数赋给整型数组a cout<<"array a:"; for(i=0; i<10; i++) cout<<a[i]<<" "; //显示整型数组a各元素 cout<<endl; for(i=0; i<9; i++) //用起泡法对数组a排序 for(j=0; j<9-i; j++) if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } ostrstream strout(c,sizeof(c)); //建立输出串流对象strout并与字符数组c关联 for(i=0; i<10; i++) strout<<a[i]<<" "; //将10个整数存放在字符数组c strout<<ends; //加入′\\0′ cout<<"array c:"<<c<<endl; //显示字符数组c return 0;}
0 0
- C++语言基础 例程 字符串流
- C语言基础及例程
- C/C++格式化字符串流
- C/C++的字符串流
- C++基础::字符串流(stringstream)
- 关于字符串流的学习(c++)
- C++primer 字符串流操作 p258
- [日常学习][C++]字符串流 <sstream>
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- C++语法基础--标准IO库--字符串流
- C++语言基础 例程 标准输入流
- C++语言基础 例程 文本文件的读写
- C++语言基础 例程 二进制文件及其顺序读写
- C++语言基础 例程 案例:bmp文件格式剖析
- C++语言基础 例程 文件的随机读写
- C++语言基础 例程 字符串流
- C++语言基础 例程 二进制文件应用案例
- Convert pdf to image
- Own (and Refactor) the Build
- Android 进行单元测试难在哪-part1
- Pair Program and Feel the Flow
- Fragment MyFragment not attached to Activity
- ACM-计算几何 #Topcoder #SRM187-DIV2 PointInPolygon
- [Keil] Notes