スポンサーサイト



この広告は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;
}

STL02

#include <vector>
#include <iostream>

using namespace std;

int main()
{
 vector<int> array1;  // int型の動的配列
 vector<int> array2;  // int型の動的配列
 int i;
 for( i = 0; i < 10; ++i )
 {
  array1.push_back( i );
  array2.push_back( 9 - i );
 }
 if( array1 == array2 )  // ==演算子で比較
 {
  cout << "一致しました" << endl;
 }
 array2.clear();                 // clear()で全ての要素を削除する
 cout << array1.size() << endl;  // size()で要素数取得
 if( array2.empty() )            // empty()で空かどうか調べる
 {
  cout << "array2は空です" << endl;
 }
 for( i = 0; i < 10; ++i )
 {
  array1.pop_back();          // pop_back()で末尾の要素削除
 }
 return 0;
}

STL01

#include <vector>
#include <iostream>

using namespace std;

int main()
{
 vector<int> array;  // int型の動的配列
 int i;
 // 10個の要素を追加していく
 for( i = 0; i < 20; ++i )
 {
  array.push_back( i );
 }
 // 10個の要素を出力
 for( i = 0; i < 20; ++i )
 {
  cout << array[i] << endl;
 }
 return 0;
}

calc02

#include <stdio.h>

void main()
{
 int a=4,b=3;
 // 演算処理
 printf("%d+%d=%d\n",a,b,a+b);
 printf("%d-%d=%d\n",a,b,a-b);
 printf("%d*%d=%d\n",a,b,a*b);
 printf("%d/%d=%d\n",a,b,a/b);
 printf("%d/%dの余り:%d\n"a,b,a%b);
}

calc01

#include <stdio.h>

main()
{
 printf("5+5は%dです。\n",5+5);
 printf("5−5は%dです。\n",5-5);
 printf("5×5は%dです。\n",5*5);
 printf("5÷5は%dです。\n",5/5);
 printf("5÷3の余りは%dです。\n",5%3);
}

csv02

#include <stdio.h>

int main(void)
{
  FILE *fp;
  char *fname = "comma.csv";
  char s[100];
  int ret, n1, n2;
  float f1, f2;

  fp = fopen( fname, "r" );
  if( fp == NULL ){
    printf( "%sファイルが開けません\n", fname );
    return -1;
  }

  while( ( ret = fscanf( fp, "%[^,],%d,%d,%f,%f", s, &n1, &n2, &f1, &f2 ) ) != EOF ){
    printf( "%s %d %d %f %f", s, n1, n2, f1, f2 );
  }

  fclose( fp );
  return 0;
}

csv01

#include <stdio.h>

int main(void)
{
  FILE *fp;
  char *fname = "comma.csv";
  char *s1 = "test01";
  char *s2 = "test02";
  char *s3 = "test03";
  int n1 = 10;
  int n2 = 20;
  int n3 = 30;
  float f1 = 1.0;
  float f2 = 2.0;
  float f3 = 3.0;

  fp = fopen( fname, "w" );
  if( fp == NULL ){
    printf( "%sファイルが開けません\n", fname );
    return -1;
  }

  fprintf( fp, "%s,%d,%d,%f,%f\n", s1, n1, n1+1, f1, f1+0.1 );
  fprintf( fp, "%s,%d,%d,%f,%f\n", s2, n2, n2+1, f2, f2+0.1 );
  fprintf( fp, "%s,%d,%d,%f,%f",    s3, n3, n3+1, f3, f3+0.1 );

  fclose( fp );

  printf( "%sファイル書き込みが終わりました\n", fname );
  return 0;
}

ststic01

#include <stdio.h>

void main()
{
 int i;
 for(i=0;i<3;i++){
  static int s=i;
  printf("i=%d s=%d\n",i,s);
 }
}

array02

#include <stdio.h>

void main()
{
 int ar[]={5,2,3,2,1};
 int i;
 for(i=0;i<5;i++){
  printf("ar[%d]=%d\n",i,ar[i]);
 }
}

array01

#include <stdio.h>

void main()
{
 int i;
 int a[3];

 a[0]=10;
 a[1]=20;
 a[2]=30;
 for(i=0;i<3;i++){
  printf("a[%d]=%d\n",i,a[i]);
 }
}

Cpp04

#include <iostream>

namespace sample
{
 int num;    // sample::num
}

int main()
{
 using namespace std;  // このスコープ内ではstd::が省略できる
 int num;    // 単なるnum
 
 num = 123;          // 単なるnumに代入
 sample::num = 300;  // sample::numに代入
 
 cout << num << endl;
 cout << sample::num << endl;

 return 0;
}

Cpp03

#include <iostream>

namespace sample
{
 int num;    // sample::num
}

int main()
{
 int num;    // 単なるnum
 
 num = 123;          // 単なるnumに代入
 sample::num = 300;  // sample::numに代入
 
 std::cout << num << std::endl;
 std::cout << sample::num << std::endl;

 return 0;
}

Cpp02

#include <iostream>

using namespace std;

int main()
{
 int num;
 
 cin >> num;
 cout << num<<endl;

 return 0;
}

←prev next