C++無視しやすい詳細

4141 ワード

1データ型指定長を超えた割付
(1)符号なしデータ型
unsigned char ch1= 336;
unsigned char ch2 = -1;
上記の2つの賦値はunsigned charタイプの範囲を超えており、ほとんどのコンパイラはこのような状況を処理しています.賦値を許可し、警告を1つだけ与えますが、moduloを経た後の値です.
cout<(2)記号のある本については,具体的なコンパイラによって決める.
2浮動小数点型の有効数字
floatタイプは6桁の有効数字のみを提供し、doubleタイプは少なくとも10桁の有効数字を提供します.
3 definitionsとdeclarations(変数の定義と宣言)
主な違いは、ストレージスペースを割り当てることを定義し、ストレージスペースを割り当てないことを宣言し、名前が何なのか、タイプが何なのかを説明するだけです.定義と宣言の方法を集中的に見てみましょう.
extern int i;//宣言
int i;//定義#テイギ#
extern double pi=3.14//定義
4変数の作用範囲
宣言の開始から範囲の終了まで、ローカル変数はグローバル変数を上書きし、次のコードクリップを参照してください.
      int i = 100, sum = 0;

for (int i = 0; i != 10; ++i)

sum += i;

std::cout << i << " " << sum << std::endl

: i= 100 sum = 45



int sum = 0;

for (int i = 0; i != 10; ++i)

sum += i;
std::cout << "Sum from 0 to " << i << " is " << sum << std::endl;
, i (cout i)

5 const

const , 。 const ,

// file_1.cc
// defines and initializes a const that is accessible to other files
extern const int bufSize = fcn();
// file_2.cc
extern const int bufSize;
// uses bufSize from file_1
//uses bufSize defined in file_1
for (int index = 0; index != bufSize; ++index)
// ...
extern

6 vector<string>::const_iterator const vector<string>::iterator

for (vector<string>::const_iterator iter = text.begin();
iter != text.end(); ++ iter)
*iter = " "; // error: *iter is const

vector<string>::const_iterator iter , ,
vector<int> nums(10); // nums
is non
const
const vector<int>::iterator cit = nums.begin();
*cit = 1; // ok: cit can change its underlying element
++cit; // error: can't change the value of cit

const vector<int>::iterator cit , ,

7 typedef string *pstring
const pstring cstr; ?
,typedef string *pstring string , const pstring cstr <=> const string * cstr
const string , 。
, cstr const , string