cout ==>输出类对象,用来输出的,可以自动识别类型,所以不需要加格式符号
<< 插入符(输出符号)
endl 换行===>\n
- #include
- //#预处理
- //include 包含 相应的头文件
- //iostream 输入输出流 i--input o--out
-
- using namespace std;
- //using :使用命名空间的关键字
- //namespace :命名空间的关键字
- //std:标准的命名空间
-
- //程序入口
- int main()
- { //程序的开始
-
-
- cout << "Hello World!" << endl;
- cout << "今天是我学习C++的第一天,好开心呀" << "好油" << endl;
- return 0;
-
- } //程序的结束
cin 输入类的对象, 用来输入的,给变量初始化,它可以自动识别数据类型,所以不需要加格式符,不需要加&
>> 提取符(输入符)
- #include
-
- using namespace std;
-
- int main()
- {
- cout << "请输入你的年纪:" << endl;
- int age;
- cin >> age;
- cout << "age = " << age;
-
- return 0;
- }
基本数据类型:char 、short、int 、long、float、double、bool、string
构造数据类型:数组、指针、结构体、共用体、枚举、类
- #include
- #include
-
- using namespace std;
-
- int main()
- {
- //字符型 每个字符都有对应的ASCII码值
- char a = 'A';
- cout << "a = " << a << endl; //结果 a = A
- cout << "a = " << (int)a << endl;
-
- //整型 进制的问题 有无前缀
- //二进制 0b
- //八进制 0
- //十进制 无
- //十六进制 0x
- int b = 100, b2 = 0b100, b3 = 0100, b4 = 0x100;
- cout << "b = " << b << endl; //结果100
- cout << "b = " << dec << b << endl; //dec是以10进制方式 100
- cout << "b = " << oct << b << endl; //oct是以8进制
- cout << "b = " << hex << b << endl; //hex是以16进制
- cout << "b = " << b << endl; //如果改变了输出进制规则,则后面都以这种方式输出
- cout << "b = " << dec << b << endl;
-
-
- //浮点型
- double d1 = 12.3456789, d2 = 1.23456789;
- cout << "d1 = " << d1 << endl; //保留有效数字6位,存在四舍五入
- //如果自己想保留有效数字几位,需要用到一个方法 其头文件
- cout << setprecision(4) << d1 << endl;
-
- //保留小数点后几位
- cout << setprecision(4) << fixed << d1 << endl;
-
- //bool类型 true == 1 false == 0
- bool t = true, t2 = false;
- cout << t << endl;
- cout << boolalpha << t << endl;
- cout << t2 << endl;
- //bool可以参与运算
- cout << t+t2 << endl;
-
- return 0;
- }
- #include
-
-
- using namespace std;
-
-
- int main()
- {
- string str1; // int a
- string str2 = "abcdef"; //初始化
- cout << "str2 = " << str2 << endl;
-
-
- string str3 = str2; //把str2初始化str3
- cout << "str3 = " << str3 << endl;
-
-
- string str4(str3); // ==> string str4 = str3; 调用有参构造函数
- cout << "str4 = " << str4 << endl;
-
-
- string str5(str2,3); //把str2下标为3地方开始给str5初始化
- cout << "str5 = " << str5 << endl;
-
-
- str1 = str5; //赋值
-
-
- string str6(6, 'm');
- cout << "str6 = " << str6 << endl;
- return 0;
- }
size() 大小---元素的个数
empty() 判断是否为空
capacity() 容量大小
- if(!str6.empty())
- {
- cout << str6.size() << endl;
- cout << str6.capacity() << endl;
- }
1> c语言风格的字符串可以直接转换成c++字符串
2> c++字符串不可以直接转换成c风格的字符串
- string str;
- char a[20] = "hello kitty";
- str = a;
- cout << str << endl;
-
-
- string strr = "hello world";
- //a = strr;
- //strcpy(a, strr);
- strcpy(a, strr.c_str()); //把C++中的字符串转换成c
- cout << a << endl; //hello world
1> 下标 ---不判断是否越界
2> at() ---判断是否越界
- #include
-
- using namespace std;
-
- int main()
- {
- string str = "hello";
-
-
- cout << str[4] << endl;
- cout << str[8] << endl;
-
-
- cout << str.at(4) << endl;
- cout << str.at(8) << endl;
-
-
- return 0;
- }
由于c++有这种数据类型的变量,所以可以像其他基本数据类型一样用关系运算符比较。
- string str1 = "ab";
- string str2 = "ABCDEFGTTTTT";
-
- if(str1 != str2)
- {
- if(str1 > str2)
- {
- cout << "str1 > str2" << endl;
- }
- else
- {
- cout << "str1 < str2" << endl;
- }
- }
- else
- {
- cout << "sr1 == str2" << endl;
- }
- #include
- #include
//包含数组的头文件 容器 -
- using namespace std;
-
- int main()
- {
-
- //一维数组
- // int a[5];
- // for(int i = 0; i<5; i++)
- // {
- // cin >> a[i];
- // }
- // for(int i=0; i<5; i++)
- // {
- // cout << a[i];
- // }
-
-
- // array
a; // == int a[5] 容器 - // array
::iterator iter; //迭代器 ==指针 - // //a.begin()第一个元素的地址
- // //a.end()最后一个元素的下一个地址
- // for(iter = a.begin(); iter != a.end(); iter++)
- // {
- // cin >> *iter;
- // }
- // for(iter = a.begin(); iter != a.end(); iter++)
- // {
- // cout << *iter << " ";
- // }
- // cout << endl;
-
-
- //二维数组 由多个数据类型相同的一维数组组成的数组
- // int a[3][2];
- // for(int i=0;i<3;i++)
- // {
- // for(int j=0;j<2;j++)
- // {
- // cin >> a[i][j];
- // }
- // }
- // for(int i=0;i<3;i++)
- // {
- // for(int j=0;j<2;j++)
- // {
- // cout << a[i][j] << " ";
- // }
- // cout << endl;
- // }
-
-
- array< array<int, 2> , 3> a;
- array< array<int, 2> , 3>::iterator iter1;
- array<int, 2>::iterator iter2;
- for(iter1 = a.begin(); iter1 != a.end(); iter1++)
- {
- for(iter2 = (*iter1).begin(); iter2 != (*iter1).end(); iter2++)
- {
- cin >> *iter2;
- }
- }
- for(iter1 = a.begin(); iter1 != a.end(); iter1++)
- {
- for(iter2 = (*iter1).begin(); iter2 != (*iter1).end(); iter2++)
- {
- cout << *iter2 << " ";
- }
- cout << endl;
- }
- return 0;
- }
1> 多人协同合作时,可能会出现命名污染(命名冲突)
2> 命名空间就相当于给一个取个姓 (张 三)
- #include
-
-
- //方法3,直接将整个命名空间拿过来,后面的程序就可以不需要加命名空间名以及限定符
- //using namespace std;
-
-
- using std::cout;
-
-
- int main()
- {
- //方法1:写上命名空间名字以及作用域限定符
- //std::cout << "Hello World!" << std::endl;
-
- //方法2:使用命名空间里的某个名字,则后面程序中就不需要加命名空间名以及限定符,但是
- //没有表明的名字,则还要循序方法1
- cout << "Hello World!" << std::endl;
- cout << "...." << std::endl;
-
- return 0;
- }
格式: namespace 命名空间名 {
数据类型 名字1;
数据类型 名字2;
数据类型 名字3;
。。。。 数据类型 名字n;
}
#include using namespace std; namespace Zhangsan { int cuihua; int age; } namespace Lisi //可以嵌套 { int cuihua; int age; namespace xiaosan { int age; //作用域不同 可以一级一级找 } } namespace Zhangsan //可以在同一个作用域下定义相同的命名空间 { int b; //int age //但是要注意,合并,就不能有相同类型的变量名 } using Zhangsan::age; //方法2 int main() { Zhangsan::cuihua = 19; age = 56; Lisi::xiaosan::age = 23; return 0; }
1> 作用:防止命名冲突,命名污染问题
2> std的命名空间的使用,有三种:独立使用、声明某一个名字、声明整个命名空间
3> 自定义命名空间:namespace 空间名{ }
4> 命名空间能够嵌套定义,同一作用域下可以定义多个同名的命名空间