記事ディレクトリ
コンテンツのこの部分はC言語に非常に似ており、一般的に使用されるC ++の部分であるため、すばやく確認できます。
1.ループ
1.1forループ
以下は、forループを使用して文字列にアクセスする例です。文字列を入力してから、出力を逆にします。
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout<<"Enter a word: ";
string word;
cin>>word;
for(int i = word.size()- 1;i >=0; i--)
{
cout<<word[i];
}
cout<<"\nBye.\n";
return 0;
}
1.2コンマ演算子
コンマ演算子は最低の優先順位です。コンマ演算子の値は右端の値です。
#include<iostream>
using namespace std;
int main()
{
int i = 0;
int j = 0;
i = 1,2;//等同于(i = 1),2;
j = (1,2);
cout<<i<<endl;
cout<<j<<endl;
return 0;
}
出力:
1.3関係演算子
関係演算子の優先順位は、算術演算子の優先順位よりも低くなります。
x + 3> y-2;(x + 3)>(y-2);と同等
1.4Cスタイルの文字列の比較
Cスタイルの文字列の配列名はアドレスを表すため、次の式でアドレスを比較します。
char word[] = "mate";
if(word == "mate")//比较的是地址,他们不相等。
Cスタイルの文字列はstrcmp()を使用してサイズを比較します。Cスタイルの文字列は末尾のヌル文字で定義されているため、次の2つの文字列は同じです。
char big[80] = "hello";
char little[10] = "hello";
- str1 == str2;の場合、strcmp(str1、str2)== 0;
- str1> str2の場合、strcmp(str1、str2)> 0;
- str1 <str2;の場合、strcmp(str1、str2)<0;
1.5文字列の比較
文字列は直接比較できます
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word = "?ate";
for(char ch = 'a';word != "mate";ch++)
{
cout<<word<<endl;
word[0] = ch;
}
cout<<word<<endl;
}
出力:
2.whileおよびdowhileループ
これらの2つのループはと同じ機能を持っているので、紹介しません。
2.1typedef和define
これらは両方ともエイリアス化できますが、defineは単なる置き換えであり、型を定義するときに予期しない結果が発生することがあります。Typedefはしません。
#define INT_point int*
INT_point pa,pb;//pa是指针,pb是普通整形变量。
3.範囲ベースのforループ(C ++ 11)
C ++ 11は、範囲ベースのforループという新しいループを追加しました。これにより、一般的な循環タスクが簡素化されます。たとえば、配列(またはベクトルや配列などのコンテナクラス)の各要素に対して同じ操作を実行します。
#include<iostream>
using namespace std;
int main()
{
double prices[5] = {
2.3, 4.3,6.4,10.3, 5.9};
for(double p:prices)
{
cout<<p<<endl;
}
//或者直接应auto
cout<<"使用auto:"<<endl;
for(auto p:prices)
{
cout<<p<<endl;
}
}
配列要素の値を変更する必要がある場合は、&pを使用する必要があります
#include<iostream>
using namespace std;
int main()
{
double prices[5] = {
2.3, 4.3,6.4,10.3, 5.9};
for(auto &p:prices)
{
p = p * 2;
}
for(double p:prices)
{
cout<<p<<endl;
}
}
4.ループとテキスト入力
4.1元のcin入力
このタイプの入力は、スペース、タブ、および改行を読み取りません。次の例は、「#」が読み取られるまで終了しません。
#include <iostream>
using namespace std;
int main()
{
char ch;
int count = 0;
cout<<"Enter characters;enter # quit:\n";
cin>>ch;
while(ch != '#')
{
cout<<ch;
++count;
cin>>ch;
}
cout<<endl<<count<<" characters read\n";
return 0;
}
出力
Enter characters;enter # quit:
da jia hao !###ldjkfa
dajiahao!
9 characters read
4.2スペースを読み取ることができるcin.get(char)
スペースなどの文字を読み取る必要がある場合は、cin.get(char)を使用できます。サンプルプログラムを次のように変更します。
#include <iostream>
using namespace std;
int main()
{
char ch;
int count = 0;
cout<<"Enter characters;enter # quit:\n";
cin.get(ch);
while(ch != '#')
{
cout<<ch;
++count;
cin.get(ch);
}
cout<<endl<<count<<" characters read\n";
return 0;
}
出力
Enter characters;enter # quit:
da jia hao !###lll
da jia hao !
12 characters read
5.論理演算子
論理演算子&& ||それらの優先度は関係演算子よりも低くなりますが、!はすべての関係演算子および算術演算子よりも高くなります。など:
x >5 && x < 10;
//等同于
(x > 5 && (x < 10))
混乱しやすい場合は、()を使用してください。
論理演算子は、プログラム内ではなく、および、またはで表すこともでき、C ++ではヘッダーファイルは必要ありません。C言語では、ヘッダーファイルiso646.hをインクルードする必要があります。
6.文字関数ライブラリcctype
C ++は、C言語の文字に関連する非常に便利な関数パッケージを継承し、そのヘッダーファイルはcctype(古いスタイルではctype.h)にあります。
7.switchステートメント
スイッチの判断は、整数、文字、または列挙のみを処理できます。例として列挙を使用します。
#include<iostream>
using namespace std;
int main()
{
enum {
red, orange, yellow, green, blue, violet,indigo};
cout <<"Enter color code (0 - 6): ";
int code;
cin >> code;
while(code >= red && code <= indigo)
{
switch(code)
{
case red : cout<<"red\n";break;
case orange : cout<<"orange\n";break;
case yellow : cout<<"yellow\n";break;
case green : cout<<"green\n";break;
case blue : cout<<"blue\n";break;
case violet : cout<<"violet\n";break;
case indigo : cout<<"indigo\n";break;
}
cout <<"Enter color code (0 - 6): ";
cin >>code;
}
cout << "Bye\n";
return 0;
}
インプットとアウトプットが第17章にあるときに一緒に勉強しましょう!
記事の参照:「C ++ Primer PlusSixthEdition」