2018년 3월 29일 목요일

Part 2: string,sizeof,goto,try-throw-catch...C++ 本を返却する前の細々しいもの整理(C/C++ で働かす Raspberry Pi 3)

C/C++ で働かす Raspberry Pi 3


Part 1:メモリの動的な確保解放, cmath, bitset..C++ 本を返却する前の細々しいもの整理(C/C++ で働かす Raspberry Pi 3)

8.stringストリング、文字列
vectorと似ている、大きく違う部分は文字列の最後にヌル文字(’¥0’)が入ります。
---------------
string str(10,'n');       -> nnnnnnnnnn
                                  -> str=string(10,'n'); でも同じ結果です。
str.empty()                 -> 空いたら 1 (true), 空いてなかったら 0 (false)
str.size()                     -> 文字数 -> 10
                                 -> str.length() も同じ意味です。
---------------

9.enum列挙型
使用者定義的型、整数とマッチする識別子を指定
---------------
enum games {MineCraft, StarCraft, Tetris};
games g=StarCraft;        ->  gの値は1です。
g=2;                             -> Error                              
          -> games g=StarCraft;  は const int g=1; と同じ意味です。
---------------

10.sizeof
オブジェクトのメモリ占有 ビット数を返します。

---------------

int num=10000;

cout << sizeof(num) << endl;   ->  4

---------------



11.try throw catch

---------------
 #include <iostream> 
 using namespace std; 
 int main(void) 
 { 
       int num; 
       cout << "Please input number(1-10)" << endl; 
       cin >> num; 
       try 
       { 
              if(num<1 || num>10) 
                     throw 1; 
       } 
       catch(int e) 
       { 
              string eVal; 
              switch(e
              { 
                     case 1: 
                            eVal="Input error!"; 
                            break; 
                     default: 
                            eVal="Undefined error!"; 
                            break; 

              } 
              cout << eVal << endl; 
              return e; 
        } 

        cout << "Good number: " << num << endl; 

        return 0; 
 }

catch(type arg)で型と変数名を自由に変更可能ので変更してみました。

 #include <iostream> 
 using namespace std; 
 int main(void) 
 { 
       int num; 
       cout << "Please input number(1-10)" << endl; 
       cin >> num; 
       try 
       { 
              if(num<1 || num>10) 
                     throw "Input error!"; 
       } 
       catch(char const* str) 
       { 
              cout << str << endl; 
              return 1; 
        } 

        cout << "Good number: " << num << endl; 

        return 0; 
 }

 catch(char const* str)  部分を  catch(string str)にすると
terminate called after throwing an instance of 'char const*'
エラーが発生しました。 ( Dev-C++ 5.11)
---------------

12.goto
プログラム実行の流れを変えます。
---------------
 #include <iostream>
 using namespace std;
 int main(void)
 {
       cout << "HA" << endl;     -> "HA" 出力
       cout << "HA" << endl;     -> "HA" 出力
       cout << "HA" << endl;     -> "HA" 出力 
       goto skip;                        ->実行の流れを skip: の位置へ移動させます。
       cout << "HO" << endl;     -> "HO"は出力されません。
       skip: cout << "HA" << endl;  -> "HA" 出力 
       cout << "!!!" << endl;     -> "!!!" 出力 

       for(int i=0;i<10;i++)
       {
              if(i==3) goto skip2;  ->実行の流れを skip2: の位置へ移動させます。
                 3は出力されません。
              if(i==9) break;          -> 9の場合ループを終了、8まで出力されます。
              cout << i << endl;
              skip2:;                   
       }
       return 0;
 }
HA
HA
HA
HA
!!!
0
1
2
4
5
6
7
8

---------------

Part 2 。。。終わり

댓글 없음:

댓글 쓰기

1、2、QWER! in OSAKA (大阪 2025/4/10(木) Yogibo META VALLEY)

https://www.creativeman.co.jp/event/qwer-osaka/ QWER - CREATIVEMAN PRODUCTIONS 主催:LIVET 企画:ソニー・ミュージックソリューションズ 招聘:クリエイティブマンプロダクション 制作協力:クリエイテ...