初识c++
1:c++关键字
asm | do | if | return | try | continue |
---|---|---|---|---|---|
auto | double | inline | short | typedef | for |
bool | dynamic_cast | int | signed | typeid | public |
break | else | long | sizeof | throw | typename |
case | enum | mutable | static | union | wchar_t |
catch | eplicit | anmespace | static_cast | unsigned | default |
char | export | new | struct | using | friend |
class | extern | switch | virtual | register | operator |
const | false | private | template | void | true |
const_cast | go to | protected | this | volatile | while |
delete | float | reinterpret_cast |
2:命名空间
2.1 命名空间的定义
#include<iostream>
namespace N1 {
int a = 10;
int b = 20;
int Add(int left, int right) {
return left + right;
}
}
namespace N2 {
int a = 10;
int b = 20;
int Add(int left, int right) {
return left + right;
}
namespace N3 {
int c = 30;
int d = 40;
int Sub(int left, int right) {
return left - right;
}
}
}
int main() {
using namespace N2;
printf("%d ", N1::a);
printf("%d ", b);
printf("%d ", N2::N3::c);
printf("%d ", Add(10, 20));
printf("%d ", N2::N3::Sub(30, 20));
system("pause");
return 0;
}
3:c++输入输出
#include<iostream>
using namespace std;
int main() {
int a;
char b;
double c;
cin >> a >> b >> c;
cout << a << " " << b << " " << c << endl;
system("pause");
return 0;
}
4:缺省参数、
缺省参数是声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该
默认值,否则使用指定的实参
//全缺省参数
#include<iostream>
using namespace std;
void TestFunc(int a = 10) {
cout << a << endl;
}
int main() {
TestFunc();
TestFunc(20);
system("pause");
return 0;
}
//半缺省参数
#include<iostream>
using namespace std;
void TestFunc(int a, int b = 10) {
cout << a <<" "<< b << endl;
}
int main() {
TestFunc(20);
system("pause");
return 0;
}
5):函数重载
函数重载:是函数的一种特殊情况,C++允许在同一作用域中声明几个功能类似的同名函数,这些同名函数的
形参列表(参数个数 或 类型 或 顺序)必须不同,常用来处理实现功能类似数据类型不同的问题
#include<iostream>
using namespace std;
void print(int i) {
cout << i << endl;
}
void print(string str) {
cout << str << endl;
}
int main() {
print(1);
print("hello world");
system("pause");
return 0;
}
6)引用
6.1:引用特性
#include<iostream>
using namespace std;
void TestFunc() {
int a = 10;
int& ra = a;
ra = 100;
cout << a << " " << ra << endl;//100 100
}
int main() {
TestFunc();
system("pause");
return 0;
}
void testfunc() {
int a = 10;
int& ra = a;
int& rra = a;
cout << a << " " << ra << " " << rra << endl;//10 10 10
cout << &a << " " << &ra << " " << &rra << endl;//00AFFB04 00AFFB04 00AFFB04
}
int main() {
testfunc();
system("pause");
return 0;
}
6.2 常引用
void TestFunc() {
const int a = 10;
const int& ra = a;
const int& b = 10;
double d = 3.14;
const int& rd = d;
cout << a << " " << ra << " " << b << " " << d <<" "<<rd<< endl;
//10 10 10 3.14 3
}
int main() {
TestFunc();
system("pause");
return 0;
}
6.3 使用场景
//做参数
void swap(int& left, int& right) {
int t = left;
left = right;
right = t;
}
void TestFunc() {
int a = 10;
int b = 20;
swap(a, b);
cout << a << " " << b << endl;//20 10
}
int main() {
TestFunc();
system("pause");
return 0;
}
//做返回值
int& Add(int a, int b) {
int c = a + b;
return c;
}
int main() {
int& ret = Add(1, 2);
Add(3, 4);
cout << ret << endl;
system("pause");
return 0;
}
6.4 传值,传引用效率比较
#include<time.h>
struct a {
int a[10000];
};
void testfunc1(a a) {
}
void testfunc2(a& a) {
}
void testrefandvalue() {
a a;
//以值作为函数参数
size_t begin1 = clock();
for (size_t i = 0;i < 10000;i++) {
testfunc1(a);
}
size_t end1 = clock();
//以引用作为函数参数
size_t begin2 = clock();
for (size_t i = 0;i < 10000;i++) {
testfunc2(a);
}
size_t end2 = clock();
//分别计算两个函数运行结束后的时间
cout << "testfunc1(int*)-time" << end1 - begin1 << endl;
cout << "testfunc2(int&)-time" << end2 - begin2 << endl;
}
int main() {
for (int i = 0;i < 10;i++) {
testrefandvalue();
}
system("pause");
return 0;
}
值和引用作为返回值类型的比较
#include<time.h>
struct A {
int a[10000];
};
A a;
A TestFunc1() {
return a;
}
A& TestFunc2() {
return a;
}
void TestReturnByRefOrvalue() {
//以值作为函数的返回类型
size_t begin1 = clock();
for (size_t i = 0;i < 10000;i++) {
TestFunc1();
}
size_t end1 = clock();
//以引用作为函数的返回类型
size_t begin2 = clock();
for (size_t i = 0;i < 10000;i++) {
TestFunc2();
}
size_t end2 = clock();
cout << "TestFunc1 time" << end1 - begin1 << endl;
cout << "TestFunc2 time" << end2 - begin2 << endl;
}
int main() {
for (int i = 0;i < 10;i++) {
TestReturnByRefOrvalue();
}
system("pause");
return 0;
}
6.5 内联函数
test.h
#pragma once
class Test {
public:
Test(int a,int b):a(a),b(b){}
int max();
private:
int a;
int b;
};
test.c
#include"Test.h"
inline int Test:: max() {
return a > b ? a : b;
}
Main.c
#include"Test.h"
#include<iostream>
using namespace std;
inline int Test::max()
{
return a > b ? a : b;
}
int main() {
Test a(3, 5);
cout << a.max() << endl;
system("pause");
return 0;
}