文章目录
这部分内容和C语言很像,而且是常用的C++部分,所以可以快速的过一遍。
1. 循环
1.1 for循环
下面使用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.4 C-风格的字符串比较
C风格的字符串的数组名表示地址,因此下列表达式比较的是地址。
char word[] = "mate";
if(word == "mate")//比较的是地址,他们不相等。
C风格的字符串用strcmp()来比较大小。因为C风格字符串用结尾的空字符定义,所以下面两个字符串相等。
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 string类字符串比较
字符串可以直接比较
#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和do while循环
这两个循环和for一样的功能,就不介绍了。
2.1 typedef和define
这两个都可以设置别名,但是define只是替换,在定义类型时有时候会出现意想不到的结果。而typedef不会。
#define INT_point int*
INT_point pa,pb;//pa是指针,pb是普通整形变量。
3. 基于范围的for循环(C++11)
C++11新增了一种循环:基于范围(range-based)的for循环。这简化了一种常见的循环任务:对数组(或容器类,如vector和array)的每个元素执行相同的操作,比如。
#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))
容易搞混的话,就用()吧!
逻辑运算符在程序中还可以用and 、or和not表示,而且在C++中不需要任何头文件。在C语言则需要包括头文件iso646.h。
6. 字符函数库cctype
C++从C语言继承了一个与字符相关的、非常方便的函数软件包,它的头文件在cctype中(老式风格中为ctype.h)。
7. switch语句
switch的判断只能处理整数、char或者枚举量。用枚举量来举例:
#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 Plus第六版》