c++学習ノートの言語基礎

10976 ワード

 

プログラムこうぞう
コメント
c++は、2つの注釈をサポートします.
  • 行の注釈、形式は
  • のようです
    //line comment
  • 個の注釈、例えば
  • /* block comment can occupy
    multiple lines */

    ヘッダファイル
    cppファイルの先頭には通常#includeという命令があり、プリプロセッサ(preprocessor)と呼ばれ、iostreamライブラリに導入する関数として機能し、その他には#inlcude,#include,etcがある.
    ネーミングスペース
    プログラムにはusing namespace std;がよく見られます.stdライブラリのすべての要素が現在のファイルに表示されるようにします.例えば、プログラムにおいて印刷機能を用い、上記の文を用いた後、直接cout< , , , ,std::cout< , ::がドメインアクセス であってもよい.
    main
    も なプログラム を てみましょう.
    //my first program in c++
    #include
    using namespace std;
    
    int main() {
        cout<

    main はc++プログラムが するエントリ であり、プログラムのどこにいてもプログラムが すると されます.
    とタイプ
    ひょうじ
    c++では、ラベルはアルファベット、 、 で され、 は ではなく、さらに なのは、ラベルが と に であることです. された は、 c++ されたキーワードと することはできません. は c++ されたキーワードです( なc++コンパイラには、 の キーワードも まれる があります).
    alignas, alignof, and, and_eq, asm, auto, bitand, bitor, 
    bool, break, case, catch, char, char16_t, char32_t, class, 
    compl, const, constexpr, const_cast, continue, decltype, 
    default, delete, do, double, dynamic_cast, else, enum, 
    explicit, export, extern, false, float, for, friend, goto, 
    if, inline, int, long, mutable, namespace, new, noexcept, 
    not, not_eq, nullptr, operator, or, or_eq, private, 
    protected, public, register, reinterpret_cast, return, 
    short, signed, sizeof, static, static_assert, static_cast, 
    struct, switch, template, this, thread_local, throw, true, 
    try, typedef, typeid, typename, union, unsigned, using, 
    virtual, void, volatile, wchar_t, while, xor, xor_eq
    

    データ
    は、c++のすべての タイプです.
    グループ
    タイプ
    サイズ/
    もじがた
    char
    1バイト、 なくとも8ビット
     
    char16_t
    charより きく、 なくとも16ビット
     
    char32_t
    char 16より きい_t, なくとも32ビット
     
    wchar_t
    セットを すことができます

    signed char
    charと じく なくとも8
     
    signed short int
    charより きく、 なくとも16ビット
     
    signed int
    shortより きく、 なくとも16ビット
     
    signed long int
    intより きく、 なくとも32ビット
     
    signed long long int
    longより きく、 なくとも64ビット
    なし
    unsigned char
    それに する き サイズと じ
     
    unsigned short int

     
    unsigned int

     
    unsigned long int

     
    unsigned long long int


    float
     
     
    double
    がfloatより きい
     
    long double
    がdoubleより きい
    ブールがた
    bool
     
    void
    void
    ストレージなし
    のポインタ
    decltype(nullptr)
     
    の の めフォント は して かなくてもよい.
    タイプでは、charタイプが1バイトの なサイズを する を き(1バイト)、 のタイプは、 くのコンパイラおよびマシンで のサイズを できないため、 されたサイズがありません. のコンパイラは、 も もすべてのプラットフォームに に できるように、 されたタイプのサイズを します.
    タイプのサイズはビット で され、タイプのビット が きいほど できる が くなり、 に する も きくなります.
    サイズ
    な の
    コメント
    8ビット
    256
    =28
    16ビット
    65 536
    =216
    32ビット
    4 294 967 296
    =232 (~4 billion)
    64ビット
    18 446 744 073 709 551 616
    =264 (~18 billion billion)
    int a;
    int b;
    int c;

    または
    int a, b, c;


    c++は、3つの の をサポートします.
    //c-like initialization
    int x = 0;
    
    //constructor initialization
    int x(0);
    
    //uniform initialization
    int x{0};

    タイプ autoとdecltype
    しい が されると、コンパイラは によって のタイプを することができます.
    int foo = 0;
    auto bar = foo; //the same as: int bar = foo;

    が されていない はdecltypeで できます.
    int foo = 0;
    decltype(foo) bar; //the same as: int bar;

    stringタイプ
    stringタイプはc++の タイプであり、stringタイプの は シーケンスを することができる. タイプとは なり、stringタイプを するには、ヘッダファイルを する がある.
    #include
    #include
    using namespace std;
    
    int main(){
        string mystring;
        mystring = "hello world";
        cout<

    stringタイプの は、3つの をサポートします.
    string mystring = "hello";
    string mystring("hello");
    string mystring{"hello"};



    は、 、 、 、 、ブール 、ポインタ 、ユーザーカスタム に けられます.
    :
    10 、8 、16 75の :
    75      //decimal
    0113    //octal
    0x4b    //hexadecimal

    デフォルトでは、 の のタイプはintであるが、 の を えることによって なる タイプを することができ、 は と を しない.

    を します.
    u or U
    unsigned
    l or L
    long
    ll or LL
    long long
    :
    75      //int
    75u     //unsigned int
    75l     //long
    75ul    //unsigned long
    75lu    //unsigned long

    :
    :
    3.14159    // 3.14159
    6.02e23    // 6.02 x 10^23
    1.6e-19    // 1.6 x 10^-19
    3.0        // 3.0 

    のデフォルトタイプはdoubleであり、 に を することで の タイプを することができます.

    を します.
    f or F
    float
    l or L
    long double
    :
    3.14159L   // long double
    6.02e23f   // float 

    および :
    :
    'm';
    "hello";

    をエスケープコードで します.
    エスケープコード

    り し
    \r
    リターンマッチ
    \t
    すいへいタブ
    \v
    すいちょくタブ
    \b
    チェックアウト
    \f
    ページをかえる
    \a
    ベルを らす
    \’

    \”

    \?
    よろしくという \\
    ぎゃくスラッシュ
    :
    "Left \t Right"
    "one 
    two
    three"

    の は、スペースまたは で できます.
    "this forms" " a single"    " string "
    "of characters"
    
    //    
    
    "this forms a single string of characters"

    1つの い は、 スラッシュで の に できます.
    x = "string expressed in \
    two lines"
    
    //    
    
    x = "string expressed in two lines"

    なる を して、 なる タイプを します.

    を します.
    u
    char16_t
    U
    char32_t
    L
    wchar_t
    ここの は と を していることに してください.
    じように、 なる を して、 なる タイプを できます.

    を します.
    u8
    プログラム にutf-8で される
    R
    の を す
    の では、 コンテンツは、 R"sequence()sequence"によって られ、ここでのsequenceは、 られた を して の ( のシーケンスを む)であってもよい.
    :
    R"(string with \backslash)"
    R"&%$(string with \backslash)&%$"

    の2つの はいずれも"string with \\backslash"に しく、プレフィックスRはu,Lまたはu 8にバインドされてもよい.
    その のタイプの :
  • ブールタイプの :true,false
  • のポインタタイプの :nullptr
  • :
    bool foo = true;
    bool bar = false;
    int* p = nullptr;

    タイプ
    #include
    using namespace std;
    
    //      
    const double pi = 3.14159;
    const char newline = '
    '; int main(){ double r = 5.0; double circle; circle = 2 * pi * r; cout<

    プリプロセッサ (#define)
    に を ける のメカニズムとして、#define identifier replacementの があります.
    コード のidentifierが れる はいずれもreplacementに き えられ、replacementは の ( まで)であってもよく、この き えはプリプロセッサによってプログラムコンパイル に されるため、このような き えはタイプまたは チェックを わない.
    :
    #include
    using namespace std;
    
    #define PI 3.14159
    #define NEWLINE '
    ' int main(){ double r = 5.0; double circle; circle = 2 * PI * r; cout<

    オペレータ
    り てオペレータ(=)
    1:
    y = 2 + (x = 5);
    
    //    
    
    x = 5;
    y = 2 + x;

    2:
    x = y = z = 5;

    オペレータ(+,-,*,/,%)
    :
    x = 11 % 3; //2

    ブレンド り てオペレータ(+=,-=,*=,/=,%=,>>=,<<=,&=,^=,|=)
    :
    y += x; //    y = y + x;
    x -= 5; //    x = x - 5;
    x /= y; //    x = x / y;
    price *= units + 1; //    price = price * (units + 1);

    および オペレータ(++、-)
    :
    x = 3;
    y = ++x; // x = 4, y = 4
    
    x = 3;
    y = x++; // x = 4, y = 3

    オペレータ(=,!=,>,=,<=)
    :
    a = 2; b = 3; c = 6;
    a == 5; //false
    a * b >= c; //true
    b + 4 > a * c; //false
    (b = 2) == a; //true

    オペレータ(!,&,|)
    ショート :
  • a&&b,aはfalse, はfalse,bは されない.
  • a‖b,aビットtrue, true,bは されない.

  • き3 Dオペレータ(?)
    :
    result = codition ? result1 : result2;
    
    //1. condition   true, result   result1.
    //2. condition   false, result   result2.

    ストップオペレータ(,)
    :
    a = (b = 3, b + 2);
    
    //  : a = 5, b = 3.

    ビットオペレータ(&,|,^,~,<>)
    :
    4 << 1; //8
    4 >> 1; //2

    タイプ の
    :
    int i;
    float f = 3.14;
    
    i = (int) f; //c-like style
    i = int (f); //function sytle

    sizeof
    sizeof は、 タイプまたは でバイト を すパラメータを け れます.
    x = sizeof(char); //x    1,   char        

    :sizeofの り はコンパイル なので、プログラムが される に します.
    オペレータ
    cplusplus

    c++は、 シーケンスを できるストリームオブジェクトを します.
    フローオブジェクト

    cin
    ひょうじゅんにゅうりょくりゅう
    cout
    フロー
    cerr
    エラー ストリーム
    clog
    ログ ストリーム
    cout
    :
    cout << "hello" << " world
    "; string str = "hello"; cout << str << " world" << endl; //endl , , // 。

    cin
    :
    cin >> a >> b;
    
    //    
    
    cin >> a;
    cin >> b;

    cinと
    の の 、stringの わりに、getlineライブラリのcin を して、スペースまたは を できます.
    :
    // cin with strings
    #include 
    #include 
    using namespace std;
    
    int main ()
    {
      string mystr;
      cout << "What's your name? ";
      getline (cin, mystr);
      cout << "Hello " << mystr << ".
    "; cout << "What is your favorite team? "; getline (cin, mystr); cout << "I like " << mystr << " too!
    "; return 0; }

    stringstream
    を の タイプに に するには、sstreamライブラリのstringstream を します.
    :// stringstreams #include #include #include using namespace std; int main () { string mystr; float price=0; int quantity=0; cout << "Enter price: "; getline (cin,mystr); stringstream(mystr) >> price; cout << "Enter quantity: "; getline (cin,mystr); stringstream(mystr) >> quantity; cout << "Total price: " << price*quantity << endl; return 0; }