C++言語ベースルーチン文字ストリーム
2200 ワード
賀先生の授業リンクこの授業の説明
例:「書き込み」文字配列
文字ストリームを仲介してデータを交換する
例:「書き込み」文字配列
#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;
}