記事のディレクトリ
ループの範囲に基づいて、
double prices[5] = {4.99, 10.99, 6.87, 9.58, 11.49};
for(double x : prices)
cout<<x<<endl;
論理演算子の短絡
&&
それが決定された場合、左の式は、false
それは、決定表現/実行権ではありません
constへのconstポインタとポインタ
*
ポインタの名前との直接接触は、constのへのポインタであります
int i = 10;
int j = 10;
// 变量值不可修改
const int ci = i;
ci = 1; //error
// 1. 指针指向的内存不可修改
const int* pi1 = &i;
*pi1 = 1; // error
pi1 = &j;
// 2. 等价于 1
int const* pi2 = &i;
*pi2 = 1; // error
pi2 = &j;
// 3. 指针不可修改
int* const pi3 = &i;
*pi3 = 1;
pi3 = &j; // error
// 4. 没有这种语法
const* int pi4 = &i; // error
// const int*类型的值不能用于初始化int*变量
int* pi4 = pi1; // error
コマンドライン引数(模造C)
コード:
// option.exe
#include<stdio.h>
int main(int argc, char* argv[])
{
printf("%d arguments in all.\n", argc);
for(int i = 0; i < argc; i++)
printf("%d: %s\n", i+1, argv[i]);
return 0;
}
コマンドライン:
...>g++ option.cpp -o option.exe
...>option -i infile -o outfile
コマンドライン出力:
5 arguments in all.
1: option
2: -i
3: infile
4: -o
5: outfile
最初のパラメータは、コマンドライン上のプログラム名であるプログラム(またはフルパス)へのパスであります
C ++は自動的に間隔を置いた、空白/連続文字列をスプライスします
それは書くことができます。
cout << "Hello World!\n"
"Still water runs deep.\n"
"Can you can a can like a canner can a can?";
疑似乱数の生成
あなたは含める必要があり<ctime>
および<random>
2つのヘッダ。
#include <Windows.h> // for Sleep()
#include <iostream>
#include <ctime>
#include <random>
#pragma (lib, "Kernel32.lib")
using namespace std;
int main()
{
for(int i = 0; i < 10; ++i)
{
srand(time(0));
cout << "# " << i+1 << " : " << rand() << endl;
Sleep(1000);
}
return 0;
}
# 1 : 16695
# 2 : 16699
# 3 : 16702
# 4 : 16705
# 5 : 16708
# 6 : 16712
# 7 : 16715
# 8 : 16718
# 9 : 16721
# 10 : 16725
これらの「乱数」とのギャップに注目することは素晴らしいではありません。これがためですrand()
に従ってsrand()
生成する「乱数」の値が渡され、そうでない場合はSleep(1000)
、その後も、同じ10個の数字が表示されます。