C++ 定义数字
需要用到数字时,通过使用原始的数据类型,如 int、short、long、float 和 double 等等。
这些数字的类型在 C++ 快速入门计划——第一天 中数字的数据类型表格中有提到。
#include <iostream>
using namespace std;
int main ()
{
//定义
short s; //16位长度整型
int i; //整型
long l; //32位长度整型
float f; //浮点型
double d; //双浮点型
//赋值
s = 10;
i = 1000;
l = 1000000;
f = 230.47;
d = 30949.374;
//输出
cout << "short s :" << s << endl;
cout << "int i :" << i << endl;
cout << "long l :" << l << endl;
cout << "float f :" << f << endl;
cout << "double d :" << d << endl;
return 0;
}
C++ 数学运算
在 C++ 中,除了可以创建各种函数,还包含了各种有用的函数,这些函数写在标准 C 和 C++ 库中,叫做内置函数。 C++ 中一些有用的内置的数学函数可以对各种数字进行计算,但是前提需要先引用库文件——数学头文件 。
函数 | 描述 |
---|---|
double cos(double); | 该函数返回弧度角(double 型)的余弦 |
double sin(double); | 该函数返回弧度角(double 型)的正弦 |
double tan(double); | 该函数返回弧度角(double 型)的正切 |
double log(double); | 该函数返回参数的自然对数。 |
double pow(double, double); | 假设第一个参数为 x,第二个参数为 y,则该函数返回 x 的 y 次方 |
double hypot(double, double); | 该函数返回两个参数的平方总和的平方根 |
double sqrt(double); | 该函数返回参数的平方根。 |
int abs(int); | 该函数返回整数的绝对值 |
double fabs(double); | 该函数返回任意一个浮点数的绝对值 |
double floor(double); | 该函数返回一个小于或等于传入参数的最大整数 |
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int i = -200;
float f = 230.47;
double d = 200.374;
//数学运算
cout << "sin(d) :" << sin(d) << endl;
cout << "abs(i) :" << abs(i) << endl;
cout << "floor(d) :" << floor(d) << endl;
cout << "sqrt(f) :" << sqrt(f) << endl;
cout << "pow( d, 2) :" << pow(d, 2) << endl;
return 0;
}
C++ 随机数
在许多情况下,C++编程里面也需要生成随机数。关于随机数生成器,有两个相关的函数。一个是 rand(),该函数只返回一个伪随机数。生成随机数之前必须先调用 srand() 函数。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
int i,j;
srand( (unsigned)time( NULL ) );// time() 函数获取系统时间的秒数
for( i = 0; i < 10; i++ ) //10个随机数
{
// 生成实际的随机数
j= rand();
cout <<"随机数: " << j << endl;
}
return 0;
}
C++ 数组
C++ 支持数组数据结构,可以存储一个固定大小的相同类型元素的顺序集合。数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量。
数组的声明并不是声明一个个单独的变量,比如 number0、number1、…、number99,而是声明一个数组变量,比如 numbers,然后使用 numbers[0]、numbers[1]、…、numbers[99] 来代表一个个单独的变量。数组中的特定元素可以通过索引访问,且所有的数组都是由连续的内存位置组成,最低的地址对应第一个元素,最高的地址对应最后一个元素。
声明数组:需要指定元素的类型和元素的数量
type arrayName [ arraySize ];
//double balance[10]; 例子
上面的arraySize是元素的数量必须大于0的整数,type是任意一个有效的C++数据类型,arrayName是一个任意命名的数组名。
初始化数组:
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
大括号 { } 之间的值的数目不能大于我们在数组声明时在方括号 [ ] 中指定的元素数目
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
如果您省略掉了数组的大小,数组的大小则为初始化时元素的个数。
访问数组元素
数组元素可以通过数组名称加索引进行访问。元素的索引是放在方括号内,跟在数组名称的后边
//声明数组、数组赋值、访问数组
double salary = balance[9]; //将数组第10个元素的值赋给salary变量。
#include <iostream>
#include <iomanip>
using namespace std;
using std::setw;
int main ()
{
int n[10]; //内含10个整数的数组
//初始化数组元素
for ( int i = 0; i < 10; i++ )
{
n[ i ] = i + 100; //设置元素 i 为 i + 100
}
cout << "Element" << setw( 13 ) << "Value" << endl;
// 输出数组中每个元素的值
for ( int j = 0; j < 10; j++ )
{
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
}
return 0;
}
C++ 中数组详解
多维数组:C++ 支持多维数组。多维数组最简单的形式是二维数组。
type name[size1][size2]...[sizeN]; //三维数组
type arrayName [ x ][ y ]; //二维数组
//int threedim[5][10][4];
//int a[3][4]
上面创建了一个三维 5 . 10 . 4 整型数组和一个二维3 . 4整型数组
int a[3][4] = {
{0, 1, 2, 3} ,
{4, 5, 6, 7} ,
{8, 9, 10, 11}
};
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; //与上面是相同的
实例:
#include <iostream>
using namespace std;
int main ()
{
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; //访问二维数组元素
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ )
{
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}
指向数组的指针:通过指定不带索引的数组名称来生成一个指向数组中第一个元素的指针。
留空
传递数组给函数:通过指定不带索引的数组名称来给函数传递一个指向数组的指针,C++ 传数组给一个函数,数组类型自动转换为指针类型,因而传的实际是地址,若传递一个一维数组作为参数,则有三种声明方式。
void myFunction(int *param) {} //形式参数是一个指针
void myFunction(int param[10]){} //形式参数是一个已定义大小的数组:
void myFunction(int param[]){} //形式参数是一个未定义大小的数组:
#include <iostream>
using namespace std;
// 函数声明
double getAverage(int arr[], int size);
int main()
{
// 带有 5 个元素的整型数组
int balance[5] = { 1000, 2, 3, 17, 50 };
double avg;
// 传递一个指向数组的指针作为参数
avg = getAverage(balance, 5);
// 输出返回值
cout << "平均值是:" << avg << endl;
return 0;
}
//函数定义
double getAverage(int arr[], int size)
{
int i, sum = 0;
double avg;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
avg = double(sum) / size;
return avg;
}
就函数而言,数组的长度是无关紧要的,因为 C++ 不会对形式参数执行边界检查
从函数返回数组:C++ 虽然不允许返回一个完整的数组作为函数的参数。但是,可以通过指定不带索引的数组名来返回一个指向数组的指针。所以要从函数返回一个一维数组,就必须声明一个返回指针的函数。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// 要生成和返回随机数的函数(声明一个返回指针的函数)
int * getRandom( )
{
static int r[10];
srand( (unsigned)time( NULL ) );
for (int i = 0; i < 10; ++i)
{
r[i] = rand();
cout << r[i] << endl;
}
return r;
}
//主函数调用
int main ()
{
//一个指向整数的指针
int *p;
p = getRandom();
for ( int i = 0; i < 10; i++ )
{
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}
return 0;
}
C++字符串
C风格字符串:实际上是使用 null 字符 ‘\0’ 终止的一维字符数组,是一个以 null 结尾的字符串,包含了组成字符串的字符。
#include <iostream>
using namespace std;
int main()
{
char greeting[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }; //第一种
char winter[] = "winter"; //第二种
cout << "Greeting message: ";
cout << greeting << endl;
cout << "winter message: ";
cout << winter << endl;
return 0;
}
C++ 引入的 string 类类型:
常见的有这些
函数 | 说明 |
---|---|
append() | 在字符串的末尾添加字符 |
find() | 在字符串中查找字符串 |
insert() | 插入字符 |
length() | 返回字符串的长度 |
replace() | 替换字符串 |
substr() | 返回某个子字符串 |
#include <iostream>
#include <string>
using namespace std;
int main()
{
//定义一个string类对象
string http = "C++ is the best language in the world? ";
//打印字符串长度
cout << http.length() << endl;
//拼接
http.append("yes!");
cout << http << endl; //打印结果为:C++ is the best language in the world? yes!
//删除
int pos = http.find("in the world?"); //查找in the world?在字符串中的位置
cout << pos << endl;
http.replace(pos, 13, ""); //从位置pos开始,之后的13个字符替换为空,即删除
cout << http << endl;
//找子串runoob
int first = http.find_first_of("?"); //从头开始寻找字符的位置
int last = http.find_last_of("!"); //从尾开始寻找字符的位置
cout << http.substr(first + 1, last - first - 1) << endl; //提取子串并打印
return 0;
}