C++笔记-1-c与c++基础区别
文章目录
前言
自学笔记,没有历史知识铺垫(省略百度部分)C++笔记-1-c与c++基础区别
一、c++编译方式
gcc xx.cpp -lstdc++
原始方式编译g++ xx.cpp
推荐方式编译
二、c++文件扩展名
- cpp (主流使用格式)
- c
- cc
- cxx
三、c++头文件
c++ 允许使用标准c库方式,也支持新的引入头文件格式(不加
.h
)
#include <iostream>
引入C++中所有和I/O相关的类型、对象、函数
c++中c标准库的替代头文件写法
#include <stdio.h> <==> #include <cstdio>
#include <stdlib.h> <==> #include <cstdlib>
#include <string.h> <==> #include <cstring>
...
四、c++标准输入输出
cin
标准输入,c中scanf
替代品
cout
标准输出,c中printf
替代品
代码示例
#include <iostream>
int main()
{
char msg[1024];
std::cout << "请输入内容:\n" << std::endl;
std::cin >> msg;
std::cout << "输入结果:" << msg << std::endl;
}
// 编译输出结果
dony15$ g++ main.cpp
dony15$ ./a.out
请输入内容:
ceshi
输入结果:ceshi
五、c++命名空间
命名空间成员可以是全局变量、全局函数、类型、命名空间
命名空间多种写法
#include <iostream>
// 正常命名空间用法
namespace nsA
{
char a[1024];
void printfA()
{
std::cout << "nsA::a:" << a << "\n\r" << std::endl;
}
// 命名空间可以嵌套命名空间
namespace nsASub
{
void printfASub()
{
std::cout << "nsASub::a:" << a << "\n\r" << std::endl;
}
}
}
// 同一个命名空间可多次叠加定义
namespace nsA
{
struct a_s
{
int a;
char b[128];
};
void printfA2()
{
std::cout << "nsA::printfB:" << a << " \n" << std::endl;
}
}
// 匿名命名空间(仅当前文件可用,其他文件不可用,同c语言中的静态变量概念)
namespace
{
void printfB()
{
std::cout << "printfB \n" << std::endl;
}
}
// 定义全局变量
int abc;
int main()
{
std::cout << "请输入内容:\n" << std::endl;
// using namespace 声明命名空间后,可以直接使用,如cin、cout (注意,using多个命名空间存在相同变量或函数时,直接使用会报错,需要使用命名空间::变量)
using namespace std;
// 自定义命名空间::变量、函数等
cin >> nsA::a;
nsA::printfA();
using namespace nsA;
printfA();
printfA2();
// 嵌套命名空间
nsA::nsASub::printfASub();
// 引入命名空间指定方法
using nsA::printfA;
printfA();
// 命名空间别称
namespace nsA2 = nsA;
nsA2::printfA();
// 使用匿名命名空间方法
printfB();
// :: 使用全局变量(排除所有命名空间外的变量)
::abc=1;
}
// 编译输出结果
dony15$ ./a.out
请输入内容:
yya
nsA::a:yya
nsA::a:yya
nsA::printfB:yya
nsASub::a:yya
nsA::a:yya
nsA::a:yya
printfB
六、c++结构体
struct 和 union 声明时,都可以省略该关键字
结构体代码示例
#include <iostream>
struct Apple
{
int num;
char name[20];
void printf(void)
{
std::cout << "苹果名:" << name << ",数量:" << num << "\n"
<< std::endl;
}
};
int main()
{
// 声明时可省略struct前缀
Apple apple = {1, "绿苹果"};
apple.printf();
struct Apple apple2 = {2, "红苹果"};
apple2.printf();
}
// 编译输出结果
dony15$ ./a.out
苹果名:绿苹果,数量:1
苹果名:红苹果,数量:2
联合体代码示例
#include <iostream>
using namespace std;
int main()
{
// 联合体
union NUMS
{
// 4字节空间
unsigned int num;
// 4字节空间
unsigned char num_bit[4];
};
// c++ 匿名联合体
union
{
// 4字节空间
unsigned int num;
// 4字节空间
unsigned char num_bit[4];
};
NUMS nums;
nums.num = 0x12345678;
for (int i = 0; i < sizeof(nums.num_bit); i++)
{
// hex:16进制打印
// showbase:显示进制标识
cout << "大段模式:" << hex << showbase << (int)nums.num_bit[i] << endl;
}
}
七、c++枚举
#include <iostream>
using namespace std;
int main()
{
enum USER_TYPE
{
STD_USER,
VIP_1_USER,
VIP_2_USER
};
USER_TYPE user_type;
user_type = VIP_1_USER;
cout << "STD_USER:" << STD_USER << ",VIP_1_USER:" << VIP_1_USER << ",VIP_2_USER:" << VIP_2_USER << endl; // STD_USER:0,VIP_1_USER:1,VIP_2_USER:2
cout << "user_type:" << user_type << endl; // user_type:1
enum USER_TYPE2
{
STD_USER2 = 1,
VIP_1_USER2 = 5,
VIP_2_USER2 = 9
};
USER_TYPE2 user_type2;
user_type2 = VIP_1_USER2;
cout << "STD_USER2:" << STD_USER2 << ",VIP_1_USER2:" << VIP_1_USER2 << ",VIP_2_USER2:" << VIP_2_USER2 << endl; // STD_USER2:1,VIP_1_USER2:5,VIP_2_USER2:9
cout << "user_type2:" << user_type2 << endl; // user_type2:1
}
八、c++字符串
string字符串结构体
代码示例
#include <iostream>
using namespace std;
int main()
{
// c++字符串结构体
string tempName;
// 字符串拷贝
tempName = "zhangsan";
cout << "tempName:" << tempName << endl;
// 字符串拷贝
tempName = "lisi";
cout << "tempName:" << tempName << endl;
// 字符串拼接 +
string temp1 = tempName + "ABC";
cout << "temp1:" << temp1 << endl;
// 字符串拼接 +=
temp1 += tempName;
cout << "temp1 2:" << temp1 << endl;
// 字符串比较:== != > < >= <=
if (temp1 != tempName)
{
cout << "不相等" << endl;
}
// 下标访问
temp1[0] = 'L';
temp1[2] = 'S';
cout << "temp1 3:" << temp1 << endl;
}
// 编译输出结果
tempName:zhangsan
tempName:lisi
temp1:lisiABC
temp1 2:lisiABClisi
不相等
temp1 3:LiSiABClisi
九、c++字符串函数
获取字符串长度
size()
、length()
#include <iostream>
using namespace std;
int main()
{
string tempName;
tempName = "this is temp";
cout << tempName.size() << endl; // 12
cout << tempName.length() << endl; // 12
}
返回C风格的字符串(const char*)
c_str()
#include <iostream>
using namespace std;
int main()
{
string tempName;
tempName = "this is temp";
const char *tem = tempName.c_str();
cout << tem << endl; // this is temp
}
十、c++ bool类型
bool enable = true;
// 注意,非0时bool都是1 表示真
bool check()
{
return "abc";
}
十一、c++ 操作符别名
&& <==> and
|| <==> or
^ <==> xor
{ <==> <%
} <==> %>
...
十二、c++ 引用 Reference
类型 &引用名=变量名;
引用是指引用变量和被引用变量都指向同一个地址,与赋值不同
注意:引用时必须初始化,引用初始化后不可再改变
#include <iostream>
using namespace std;
int main()
{
int age = 15;
int &age2 = age;
cout << hex << &age << endl;
cout << hex << &age2 << endl;
}
// 执行结果
0x7ffee28661cc
0x7ffee28661cc
总结
本章主要为C++笔记-1-c与c++基础区别