Debug:F10 单步执行 F11 进入函数体内部
引用&是标识符的别名
可用于作形参
void swap(int&a,int&b)
{int t=a;
a=b;
b=t;
}
含有可变参数的函数 initializer_list
内联函数 inline(函数体不可以有循环和switch,不能有异常接口,必须调用前声明)
建议编译器直接用inline函数体里的函数而不经过函数调用返回的开销
constexpr函数:所有参数都是常数时返回值一定是常数,只有一个return
带默认参数值的函数:有默认参数的形参放最右,实参初始化形参按从左到右
函数重载:形参类型不同或形参个数不同
C++系统函数
#编程要点
1 c++和python不同要用“”,;
2 c++ 使用cin>> cout<< 需要前面加 include
3 c++ 输出结果看不到,末尾加system(“pause”)就可以看到结果了
数组
对象数组
#pragma once //头文件定义
#ifndef _POINT_H//一个c文件包含同一个h文件多次,如果不加#ifndef宏定义,会出现变量重复定义的错误
#define _POINT_H如果x没有被宏定义过,定义x,并编译程序段
class Point
{
public:
Point();
Point(int x, int y);
~Point();
void move(int newX, int newY);
int getX() const { return x; };
int getY() const { return y; };
static void showCount();
private:
int x, y;
};
#endif //如果x已经定义过了则编译程序段2的语句,“忽视”程序段 1
定义头文件中执行文件/用函数重载
#include "stdafx.h"
#include <iostream>
#include"Point.h"
using namespace std;
Point::Point() : x(0), y(0) //默认初始化均为0
{
cout << "Default Constructor called." << endl;
}
Point::Point(int x, int y) : x(x), y(y) //方便写法:意思是用x初始化x,y初始化y
{
cout << "Constructor called." << endl;
}
Point::~Point()
{
cout << "Destructor called" << endl;//析构
}
void Point::move(int newX, int newY)
{
cout << "moving the point to (" << newX << "," << newY << ")" << endl;
x = newX;
y = newY;
}
定义主程序
#include "stdafx.h"
#include "Point.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Entering main" << endl;
Point a[2];
for (int i = 0; i<2; i++)
a[i].move(i + 10, i + 20);
cout << "existing main" << endl;
system("pause");
return 0;
}
基于范围的for循环
注意stdafx.h头文件一定要放在最前面不然会报错:未申明的标识符之类的
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int array[3] = { 1,2,3 };
for (int&e:array)//引用e
{
e += 2;
cout << e << endl;
}
system("pause");
return 0;
}
地址运算
定义指针ptr,ptr指向的对象是int型i,把i的地址给ptr
static int i;
static int* ptr = &i;
*ptr = 3;//指针运算,ptr存放的地址单元的数值为3
引用传值
void swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}
实现数组转置的函数
int main()
{
int array[3][3] = {{ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }};
cout << "the origin array number is "<<endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << array[i][j];
}
}
for (int i = 0; i < 3; i++)
{
int j = 0;
while (j < 3)
{
if (j > i)
{
swap(array[i][j],array[j][i]);
j = j + 1;
}
else
{
j = j + 1;
}
}
}
cout << "转置以后\n" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << array[i][j];
}
}
system("pause");
return 0;
}
bug:
关于没有与这些操作数匹配的“<<”'运算符问题
解决方案:#include <string.h>改成#include
bug:一开始报了38个错误,包括修改了以上问题以后还是报C2061语法错误:标识符“string”
解决方案:在.h头文件里也需要写include以及using namespace std;
#include<string>
#pragma once
#ifndef _Employee_H
#define _Employee_H
using namespace std;
代码Employee.h
#include<string>
#pragma once
#ifndef _Employee_H
#define _Employee_H
using namespace std;//一定要include<string>以及using namespace且include<string>放在开头
class Employee//类的格式要记住,和python不同
{
public:
Employee(string name, string address, string city, int zipcode);//构造类
~Employee();//析构类
void change_name(string& newname,string& name);//引用 交换更新名字
void display(string name, string address, string city, int zipcode);
private:
string name;
string address;
string city;
int zipcode;
string newname;
};//记得分号
#endif
代码mainfile.cpp
#include "stdafx.h"
#include"Employee.h"
#include <iostream>
#include<string>
using namespace std;
int main()
{
int zipcode;
string city;
string address;
string name;
cout << "your name:";
cin >> name;
cout << "your address:" << endl;
cin >> address;
cout << "your city:" << endl;
cin >> city;
cout << "your zipcode:" << endl;
cin >> zipcode;
Employee employee(name, address, city, zipcode);
string newname;
cout << "your new name:";
cin >> newname;
employee.change_name(newname, name);
employee.display(name, city, address, zipcode);
system("pause");
return 0;
}
代码6.6.cpp
#include "stdafx.h"
#include"Employee.h"
#include<iostream>
#include<string>
using namespace std;
Employee::Employee(string name,string address,string city,int zipcode):name(name),address(address),city(city),zipcode(zipcode)
{
cout << "Constructed called." << endl;
}
Employee::~Employee()
{
cout << "Destructed called." << endl;
}
void Employee::change_name(string& newname,string& name)
{
string temp = newname;
name = temp;
}
void Employee::display(string name, string address, string city, int zipcode)
{
cout << "\t" << name <<"\t"<< address <<"\t" << city << "\t" << zipcode << endl;
}
对象指针:
#include "stdafx.h"
#include<iostream>
using namespace std;
class Point {
public:
Point(int x=0,int y=0):x(x),y(y){}
int getX() const { return x; }
int getY() const { return y; }
private:
int x, y;
};
int main()
{
Point a(4, 5);
Point *p1 = &a;
cout << p1->getX() << endl;//p1->方法名的方式调用类里面的函数
cout << a.getX() << endl;
system("pause");
return 0;
}
动态内存分配:
动态申请操作内存操作符 new
new 类型名T (初始化形参列表)
释放内存操作符 delete
delete 指针p
释放指针p所指向的内存,p必须是new操作的返回值
#include "stdafx.h"
#include<iostream>
using namespace std;
class Point
{
public:
Point() :x(0), y(0)
{
cout << "default constructor called" << endl;
}
Point(int x, int y) :x(x), y(y)
{
cout << "constructor called" << endl;
}
~Point()
{
cout << "destructor called" << endl;
}
int getX() const { return x; }
int getY() const { return y; }
void move(int newX, int newY)
{
x = newX;
y = newY;
}
private:
int x, y;
};
int main()
{
cout << "step 1" << endl;
Point *ptr1 = new Point;
cout << ptr1->getX()<<endl;
delete ptr1;
cout << "step 2" << endl;
ptr1 = new Point(1, 2);
cout << ptr1->getX() << endl;
delete ptr1;
system("pause");
return 0;
}
vector 应用举例
vector arr(5)
建立大小为5的int数组
与普通数组具有相同形式:
vector对象名 [ 下标表达式 ]
vector数组对象名不表示数组首地址
获得数组长度
用size函数
数组对象名.size()
#include "stdafx.h"
#include <vector>
#include<iostream>
using namespace std;
double average(const vector<double> & arr)
{
double sum = 0;
for (unsigned i = 0; i < arr.size(); i++)
{
sum += arr[i];
}
return sum / arr.size();
}
int main()
{
unsigned n;
cout << "n=?" << endl;
cin >> n;
vector<double>arr(n);//创建数组对象
cout << "please input n real numbers:" << endl;
for (unsigned i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "Average = " << average(arr);
system("pause");
return 0;
}
auto
#include "stdafx.h"
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<int> v = { 1,2,3 };
for (auto i = v.begin(); i != v.end(); i++)
{
cout << *i << endl;//这里的i应该是一个指针
}
for (auto e : v)
{
cout << e << endl;
}
system("pause");
return 0;
}
template 模板
template<typename T>
T abs(T x)
{
return x<0?-x:x
}
int main()
{
int n = -5;
double d = -5.5;
cout << abs(n) << endl;
cout << abs(d) << endl;
}
第九章习题
9.1 //学会使用函数模板
#include"stdafx.h"
#include <iostream>
using namespace std;
template <typename T>
inline T getSum(T const& arr, int const& b)
{
for (int i = 1; i < b; i++)
{
arr[0] = arr[0] + arr[i];
}
return arr;
}
int main()
{
int n, m;
cin >> n >> m;
int* arr_int = new int[n];
double* arr_double = new double[m];
for (int i = 0; i < n; ++i)
cin >> arr_int[i];
for (int i = 0; i < m; ++i)
cin >> arr_double[i];
cout << getSum(arr_int, n)[0] << endl;
cout << getSum(arr_double, m)[0] << endl;
system("pause");
return 0;
}
//折半查找
#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename T>
inline int binSearch(T arr[], int n, T key)
{
int first = 0;
int last = n;
while(first <= last)
{
int mid = (first + last) / 2;
if (key == arr[mid])
{
return mid;
}
else if (key < arr[mid])
{
last = mid -1;
}
else
{
first = mid + 1;
}
}
return -1;
}
int main()
{
int n, m;
int key1;
double key2;
cin >> n >> m >> key1 >> key2;
int* arr_int = new int[n];
double* arr_double = new double[m];
for (int i = 0; i < n; ++i)
cin >> arr_int[i];
for (int i = 0; i < m; ++i)
cin >> arr_double[i];
cout << binSearch(arr_int, n, key1);
cout << binSearch(arr_double, m, key2);
system("pause");
return 0;
}
10.1 迭代器
输入流迭代器
istream_iterator<T>
输出流迭代器
ostream_iterator<T>
//10_2.cpp
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;
//求平方的函数
double square(double x) {
return x * x;
}
int main() {
//从标准输入读入若干个实数,分别将它们的平方输出
transform(istream_iterator<double>(cin), istream_iterator<double>(),
ostream_iterator<double>(cout, "\t"), square);//transform函数:输入迭代器入口,输入迭代器出口,输出迭代器,函数
cout << endl;
return 0;
}
//10_3.cpp
#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
//将来自输入迭代器的n个T类型的数值排序,将结果通过输出迭代器result输出
template <class T, class InputIterator, class OutputIterator>
void mySort(InputIterator first, InputIterator last, OutputIterator result) {
//通过输入迭代器将输入数据存入向量容器s中
vector<T> s;
for (;first != last; ++first)
s.push_back(*first);
//对s进行排序,sort函数的参数必须是随机访问迭代器
sort(s.begin(), s.end());
copy(s.begin(), s.end(), result); //将s序列通过输出迭代器输出
}
int main() {
//将s数组的内容排序后输出
double a[5] = { 1.2, 2.4, 0.8, 3.3, 3.2 };
mySort<double>(a, a + 5, ostream_iterator<double>(cout, " "));
cout << endl;
//从标准输入读入若干个整数,将排序后的结果输出
mySort<int>(istream_iterator<int>(cin), istream_iterator<int>(), ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}
/*
迭代器辅助函数
advance(p, n)
对p执行n次自增操作
distance(first, last)
计算两个迭代器first和last的距离,即对first执行多少次“++”操作后能够使得first == last
容器功能
begin()、end():获得首尾迭代器
clear()将容器清空
empty判断容器是否为空
size()得到容器元素个数
s1.swap(s2):将s1与s2内容交换
debug:
C2065:“ostream_iterator”:未声明的标识符
需要加 #include
//10_4.cpp
#include <iostream>
#include <list>
#include <deque>
//输出指定的顺序容器的元素
template <class T>
void printContainer(const char* msg, const T& s) {
cout << msg << ": ";
copy(s.begin(), s.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
int main() {
//从标准输入读入10个整数,将它们分别从s的头部加入
deque<int> s;
for (int i = 0; i < 10; i++) {
int x;
cin >> x;
s.push_front(x);
}
printContainer("deque at first", s);
//用s容器的内容的逆序构造列表容器l
list<int> l(s.rbegin(), s.rend());
printContainer("list at first", l);
//将列表容器l的每相邻两个元素顺序颠倒
list<int>::iterator iter = l.begin();
while (iter != l.end()) {
int v = *iter;
iter = l.erase(iter);
l.insert(++iter, v);
}
printContainer("list at last", l);
//用列表容器l的内容给s赋值,将s输出
s.assign(l.begin(), l.end());
printContainer("deque at last", s);
return 0;
}
/*
运行结果如下:
0 9 8 6 4 3 2 1 5 4
deque at first: 4 5 1 2 3 4 6 8 9 0
list at first: 0 9 8 6 4 3 2 1 5 4
list at last: 9 0 6 8 3 4 1 2 4 5
deque at last: 9 0 6 8 3 4 1 2 4 5
/*
#include <iostream>
#include <numeric> //包含数值算法头文件
using namespace std;
//定义一个普通函数
int mult(int x, int y) { return x * y; };
int main() {
int a[] = { 1, 2, 3, 4, 5 };
const int N = sizeof(a) / sizeof(int);
cout << "The result by multipling all elements in a is "
<< accumulate(a, a + N, 1, mult)//前两个参数是范围,第三个是初值,第四个是函数
<< endl;
return 0;
//10_14.cpp
#include <iostream>
#include <numeric> //包含数值算法头文件
using namespace std;
class MultClass{ //定义MultClass类
public:
//重载操作符operator()//对*进行重载
int operator() (int x, int y) const { return x * y; }
};
int main() {
int a[] = { 1, 2, 3, 4, 5 };
const int N = sizeof(a) / sizeof(int);
cout << "The result by multipling all elements in a is "
<< accumulate(a, a + N, 1, MultClass()) //将类multclass传递给通用算法
<< endl;
return 0;
}
//10_15.cpp
#include <iostream>
#include <numeric> //包含数值算法头文件
#include <functional> //包含标准函数对象头文件
using namespace std;
int main() {
int a[] = { 1, 2, 3, 4, 5 };
const int N = sizeof(a) / sizeof(int);
cout << "The result by multipling all elements in A is “
<< accumulate(a, a + N, 1, multiplies<int>())
<< endl; //将标准函数对象传递给通用算法
return 0;
}
// 10_16.cpp
#include <functional>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
int intArr[] = { 30, 90, 10, 40, 70, 50, 20, 80 };
const int N = sizeof(intArr) / sizeof(int);
vector<int> a(intArr, intArr + N);
cout << "before sorting:" << endl;
copy(a.begin(),a.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
sort(a.begin(), a.end(), greater<int>());//按大小进行从大到小排序
cout << "after sorting:" << endl;
copy(a.begin(),a.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
return 0;
}