![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++学习笔记
在C++的海洋里挣扎
路漫漫其修远兮,吾将上下而求索
展开
-
C++: const类对象与const成员函数
#include <iostream>#include <string>using namespace std;class A{public: A(){} void print() const //const成员函数无法修改成员变量,原因是这里的this指针是一个(const A* const)型指针,无法改变其指向的值。 { // m_a = 1; cout << m_a << endl; } void setData(in.原创 2021-09-02 16:44:15 · 161 阅读 · 0 评论 -
C++: 三种循环方式
#include <iostream>#include <vector>#include <algorithm>using namespace std;void print(int n){ cout << n << " ";}int main(){ vector<int> v = {1,2,3,4,2,2}; sort(v.begin(), v.end()); for(vec.原创 2021-08-26 16:52:01 · 277 阅读 · 0 评论 -
C++: static关键字
欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/45290467C 语言的 static 关键字有三种(具体来说是两种)用途:1. 静态局部变量:用于函数体内部修饰变量,这种变量的生存期长于该函数。int foo(){ static int i = 1; // note:1 //int i = 1; // note:2 i += 1; return i;}要明白这个用法...转载 2021-08-26 15:37:44 · 160 阅读 · 0 评论 -
C++: const用法,函数前与函数后
const关键字,是“constant”的缩写,意思是"永恒不变",在C++中,一旦某个东西被const修饰,那么就有了无法修改、变动的特性,能够提高代码的健壮性。1.用const修饰函数的参数如果参数作输出用,不论它是什么数据类型,也不论它采用“指针传递”还是“引用传递”,都不能加const修饰,否则该参数将失去输出功能。const只能修饰输入参数:如果输入参数采用“指针传递”,那么加const修饰可以防止意外地改动该指针,起到保护作用。例如StringCopy函数:void Stri转载 2021-08-25 18:21:48 · 597 阅读 · 0 评论 -
C++: 简单运算符重载(加法, 减法, 前后置++, 输入, 输出)
#include <iostream>using namespace std;class Point{private: int m_x; int m_y;public: Point(); Point(int x, int y); friend Point operator+(Point& t1, Point& t2); //加法 friend Point operator-(Point& t1, .原创 2021-08-25 15:25:36 · 481 阅读 · 0 评论 -
C++右值引用和移动(for beginners)
Attention:this blog is a translation of https://www.internalpointers.com/post/c-rvalue-references-and-move-semantics-beginners ,which is posted by @internalpoiners.一、前言在我的前一篇文章里,我解释了右值背后的逻辑。核心的思想就是:在C++中你总会有一些临时的、生命周期较短的值,这些值无论如何你都无法改变。令人惊喜的是,现代C++(通常转载 2021-08-23 19:02:10 · 299 阅读 · 0 评论 -
C++:左值与右值
Attention:this blog is a translation of https://www.internalpointers.com/post/understanding-meaning-lvalues-and-rvalues-c ,which is posted by @internalpoiners.一、前言一直以来,我都对C++中左值(lvalue)和右值(lvalue)的概念模糊不清。我认为是时候好好理解他们了,因为这些概念随着C++语言的进化变得越来越重要。二、左值和右值—转载 2021-08-20 16:25:12 · 447 阅读 · 0 评论 -
C++:友元函数与友元类
#include <iostream>using namespace std;class A{ friend void set_value(A &a, int m); //声明友元函数 friend class B; //声明友元类public: A() {} void print() { cout << this->m_a << en.原创 2021-08-20 15:26:23 · 73 阅读 · 0 评论 -
C++:函数模板
1、单参数模板#include <iostream>using namespace std; template <typename T>void f_swap(T &a, T &b){ T tmp = a; a = b; b = tmp;} int main(){ int a = 10, b = 20; f_swap(a,b); //隐式实例化 cout << "a: " <..原创 2021-08-19 16:51:38 · 55 阅读 · 0 评论 -
C++: 发生重写时如何调用基类的方法
#include <iostream>using namespace std;class A1{public: A1() {} virtual void f() { cout << "A1::f" << endl; } virtual ~A1() {}private: int _a;};class B: public A1{public: B() {} virtual void f() { cout.原创 2021-08-18 11:20:07 · 742 阅读 · 0 评论 -
C++:list容器自定义类型排序
#include <iostream>#include <list>#include <string>using namespace std;class Person{public: Person(string name, int age, int hight) { this->m_Name = name; this-&...原创 2019-03-17 15:52:46 · 1965 阅读 · 0 评论 -
C++:子类vprt指针初始化问题
首先,欢迎并感谢博友进行知识补充与修正。1、理解图(截自传智扫地僧课堂笔记,如有侵权,请联系删除)2、代码实践#include <iostream>using namespace std;//vptr指针的分步初始化class Parent{public: Parent(int a=0) { this->a = a; fun();...原创 2019-03-04 16:52:01 · 197 阅读 · 0 评论 -
数字分类 (20)———PAT真题乙级
题目描述给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:A1 = 能被5整除的数字中所有偶数的和;A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;A3 = 被5除后余2的数字的个数;A4 = 被5除后余3的数字的平均数,精确到小数点后1位;A5 = 被5除后余4的数字中最大数字。 输入描述:每个输入包含1...原创 2018-11-19 20:10:05 · 119 阅读 · 0 评论 -
A+B和C (15)-----PAT真题乙级
A+B和C (15) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)题目描述给定区间[-2的31次方, 2的31次方]内的3个整数A、B和C,请判断A+B是否大于C。 输入描述:输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出...原创 2018-11-19 20:13:53 · 124 阅读 · 0 评论 -
c++ 多态小案例
#include <iostream>using namespace std; //实现多态三个条件//1:要有继承;//2:要有虚函数重写;//3:用父类指针(父类引用)指向子类对象class HeroPlane{public: virtual int power() { return 10; }};class EnemyPlane{publi...原创 2019-02-28 22:09:50 · 445 阅读 · 0 评论 -
C++:虚析构函数
首先,欢迎并感激博友进行知识补充与修正。#include <iostream>using namespace std;//虚析构函数class A{public: A() { p = new char[20]; cout << "A()" << endl; } virtual ~A() { delete[] p; ...原创 2019-03-01 16:09:07 · 107 阅读 · 0 评论 -
C++:sizeof相关
首先,欢迎并感激博友进行知识补充与修正。#include "pch.h"#include <iostream>using namespace std;//sizeof测试//空类 1//包含一个函数 1//包含一个虚函数 4 (存在vptr指针)class A{public: virtual void run() { cout <&...原创 2019-03-02 15:17:34 · 110 阅读 · 0 评论 -
C++:内联函数(inline)
首先,欢迎并感激博友进行知识补充与修正。#include <iostream>using namespace std;//1 inline关键字和函数体的实现写在一块inline void fun(){ int a = 0; cout << "a:" << a << endl;}int main(){ fun...原创 2019-03-02 15:38:22 · 104 阅读 · 0 评论 -
C++:深拷贝与浅拷贝
首先,欢迎并感激博友进行知识补充与修正。一、浅拷贝 只是简单的将数值进行copy,并不开辟新的内存;这里引用传智扫地僧的解释图,便于理解。 二、深拷贝 即手工的编写拷贝构造函数;自定义拷贝构造函数格式类名(const type &形参)...原创 2019-03-02 17:18:06 · 86 阅读 · 0 评论 -
C++:vector容器遍历方式
首先,欢迎并感谢博友进行知识补充与修正。#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;//迭代器三种遍历方式void Myprint(int e) //回调函数{ cout &...原创 2019-03-09 18:59:59 · 408 阅读 · 0 评论 -
C++:const与define的异同
首先,欢迎并感谢博友进行知识补充与修正。#include <iostream>using namespace std;//#define在编译预处理阶段处理//const常量是由编译器处理的,提供类型检查和作用域检查#define b 20int main01(){ /*int a=10; int b=20; int array[a + b];*/ /...原创 2019-03-04 16:25:51 · 108 阅读 · 0 评论 -
织女的红线
织女的红线Time Limit: 1000 ms Memory Limit: 65536 KiBSubmit StatisticProblem Description好久不见牛郎哥哥了,织女非常想他,但是她想考验一下牛郎在她不在的日子里有没有好好学习天天向上,于是乎想出一个问题考一考他。织女找了一跟很细的红线和N颗相同的钉子,将各颗钉子钉在墙上作为一个多边形的各个顶点,然后将...原创 2018-10-08 22:00:40 · 98 阅读 · 0 评论