スポンサーサイト



この広告は30日以上更新がないブログに表示されます。

STL07

#include <set>
#include <iostream>

using namespace std;

int main()
{
 set<int> nums;  // 空のset
 // 要素を追加する
 nums.insert( 200 );
 nums.insert( 500 );
 nums.insert( 100 );
 nums.insert( 400 );
 nums.insert( 300 );
 nums.insert( 300 );
 // 要素を出力する
 set<int>::iterator it = nums.begin();
 while( it != nums.end() )
 {
  cout << *it << endl;
  ++it;
 }
 // 要素数を出力する
 cout << "要素数:" << (unsigned int)nums.size() << endl;
 // 要素の全削除
 nums.clear();
 // 全削除されているか確認
 if( nums.empty() )
 {
  cout << "空です" << endl;
 }
 return 0;
}

STL06

#include <list>
#include <iostream>

using namespace std;


int main()
{
 
 list<int> intlist;  // int型の双方向リスト
 int i;

 // 10個の要素を追加していく
 for( i = 0; i < 10; ++i )
 {
  intlist.push_back( i );
 }

 list<int>::iterator it = intlist.begin(); // イテレータ
 while( it != intlist.end() )  // listの末尾まで
 {
  cout << *it << endl;  // 要素を出力
  ++it;  // イテレータを1つ進める
 }

 return 0;
}

STL05

#include <list>

int main()
{
 std::list<int> intlist;  // int型の双方向リスト
 int i;

 // 10個の要素を追加していく
 for( i = 0; i < 10; ++i )
 {
  intlist.push_back( i );
 }

 // 10個の要素を削除していく
 for( i = 0; i < 10; ++i )
 {
  intlist.pop_back();
 }

 return 0;
}

STL04

#include <vector>
#include <iostream>

using namespace std;

int main()
{
 vector<int> array;   // int型の動的配列
 int i;
 for( i = 0; i < 10; ++i )
 {
  array.push_back( i );
 }
 vector<int>::iterator it = array.begin();
 ++it;
 it = array.erase( it );      // itの位置の要素を削除
 array.insert( it, 999 );     // itの位置に999を挿入
 for( it = array.begin(); it != array.end(); ++it )
 {
  cout << *it << endl;
 }
 return 0;
}

STL03

#include <vector>
#include <iostream>

using namespace std;

int main()
{
 vector<int> array;   // int型の動的配列
 int i;
 for( i = 0; i < 10; ++i )
 {
  array.push_back( i );
 }
 vector<int>::iterator it = array.begin();  // イテレータのインスタンス化
 while( it != array.end() )  // 末尾要素まで
 {
  cout << *it << endl;  // *演算子で間接参照
  ++it;                 // イテレータを1つ進める
 }
 return 0;
}

file03

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    FILE *fp;
    char filename[] = "sample.txt";
    int count;

    /* ファイルオープン */
    if ((fp = fopen(filename, "w")) == NULL) {
        fprintf(stderr, "ファイルのオープンに失敗しました.\n");
        return EXIT_FAILURE;
    }

    /* 書き込み */
    count = fprintf(fp, "Snoopy!\n");
 count += fprintf(fp, "%d + %d = %d\n",1,2,1+2);
    if ( count < 0 ) {
        fprintf(stderr, "ファイルの書込みに失敗しました.\n");
        fclose(fp);
        return EXIT_FAILURE;
    }

    fprintf(stdout, "%sへ%d文字出力しました.\n", filename, count);

    /* ファイルクローズ */
    fclose(fp);

    return EXIT_SUCCESS;
}

file02

#include <stdio.h>

int main(){
    //ファイル構造体へのポインタを宣言
    FILE *lf,*sf;
    char str[256];
    int c;
    //読み込みモードでファイルを開く
    lf = fopen("main.c","r");
    //エラー
    if(lf==NULL){
        printf("ファイルオープンエラー\n");
        return -1;
    }
    //書き込みモードでファイルを開く
    sf = fopen("test.txt","w");
    //エラー
    if(sf==NULL){
        printf("ファイルオープンエラー\n");
        return -1;
    }
    //fgetsがNULLになるまで繰り返す
    while(fgets(str,256,lf))
    {
        printf("%s", str);
        //str に格納されている文字列をsfに書き込む
        fputs(str,sf);
    }
    //sfクローズ
    fclose(sf);
    //lfクローズ
    fclose(lf);
    return 0;
}

file01

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
 FILE *fp; /* (1)ファイルポインタの宣言 */
 char s[256];
 
 /* (2)ファイルのオープン */
 /*  ここで、ファイルポインタを取得する */
 if ((fp = fopen("smpl.txt", "r")) == NULL) {
  printf("file open error!!\n");
  exit(EXIT_FAILURE); /* (3)エラーの場合は通常、異常終了する */
 }
 
 /* (4)ファイルの読み(書き)*/
 while (fgets(s, 256, fp) != NULL) {
 
  /* ここではfgets()により1行単位で読み出し */
  printf("%s", s);
 }
 fclose(fp); /* (5)ファイルのクローズ */
 
 return 0;
}

←prev next→