c++开始学习1

慕课-c++远征之起航篇

1、c++与c的不同

 数据类型:共同:基本:(int、char、float、double),构造:(数组、struct、union、emum),指针类型,空类型(void)。

      异:c++多了bool型

 初始化数值:同:int x = 1;

       异:c++多了 int x(1)?

 输入输出不同:c:scanf,print

        c++:cin,cout 

 定义位置:c:函数内开头

        c++:随用随定义

2、c++语句介绍

 1)#include <iostream> 该库定义了三个标准流对象,cin、cout、cerr。一般用法 cin >> x(输入x是什么);cout << "xxx"(或者变量x)<< endl;

  cout << oct(dec、hex,进制) << x << endl;以什么进制输出

  cout <<  boolalpha << y << endl; 以bool值true or false 输出y

 2)# include <stdlib.h> 常用的函数库

 3)using namespace std; 使用std命名空间

     namespace xx {}定义某命名空间( using namespace * 释放某命名空间,使用)

   不同命名空间可以使用相同变量名,用xx::x 区分

 4)system(”pause“)系统命令,按任意键继续

3、注意事项

 1)使用双引号

 2)开头三行一般为:#include <iostream>

          #include <stdlib.h>

          using namespace std;

4、例子,找出数组中的最大值

#include <iostream>
#include <cstdlib>
using namespace std;

int getValue(int *arr, int count)
{
int temp = arr[0];
for(int i = 0; i < count; i++)
{
if(arr[i] > temp)
{
temp = arr[i];
}
}
return temp;
}

int main()
{
cout << "请输入一个数组:" << endl;
int arr1[4] = {11,9,5,4};
//cin >> arr1;   ??数组怎样输入、输出
//cout << arr1 << endl;
cout << getValue(arr1,4) << endl;

}

 

posted @ 2017-06-16 23:41  可可西米  阅读(180)  评论(0编辑  收藏  举报