
C++
全都是泡饃
赖床一时爽,一直赖床一直爽
展开
-
C++remove和erase
remove(str.begin(),str.end(),val);remove是将strz中等于val的值用后一个值覆盖,并返回最后一个未被删除的元素的下一个迭代器:若str为"abcaad"对'a'remove后则变为"bcdaaa"配合erase可将所有a删除str.erase(remove(str.begin(),str.end(),val),str.end());...原创 2022-04-05 23:40:13 · 1285 阅读 · 1 评论 -
C++求解最大公约数和最小公倍数
最大公约数(greatest common diversor)int gcd(int x,int y){ if(y%x==0) return x; return gcd(y%x,x);}最小公倍数(lowest common multiple)int lcm(int x,int y){ return x*y/gcd(x,y);}原创 2022-04-05 19:32:22 · 1027 阅读 · 0 评论 -
剑指Offer002:二进制加法
class Solution {public: string addBinary(string a, string b) { int idx_a=a.size()-1,idx_b=b.size()-1; string ans=""; int carry=0;//进位初始化为0 while(idx_a>=0||idx_b>=0||carry!=0){ int sum=carry; ...原创 2022-02-19 12:15:39 · 7544 阅读 · 0 评论 -
剑指Offer001:整数除法
提示:-231<= a, b <= 231- 1 b != 0class Solution {public: int divide(int a, int b) { bool sign=(a^b)>>31;//判断a和b是否异号,右移31位到符号位 long dividend=abs((long)a),divisor=abs((long)b);//转为正数 long ans=0; while(d...原创 2022-02-19 12:11:31 · 321 阅读 · 0 评论 -
巧用C++位运算
C++位运算可以提高运算效率移位:用x<<1代替x*2;x>>1代替x/2位与:取余运算x%n可用x&(n-1)代替原创 2021-12-04 11:41:01 · 317 阅读 · 0 评论 -
C++ char*,const char*,string,int的相互转换
1.string转const char*string s ="abc";constchar* c_s = s.c_str();2.const char*转stringconstchar* c_s ="abc";string s(c_s);3.string转char*string s ="abc";char* c;constintlen = s.length();c =newchar[len+1];strcpy(c,s.c_str());4.c...转载 2021-11-07 22:21:32 · 1902 阅读 · 0 评论 -
[侯捷]C++ STL 体系结构与内核分析源代码
// author : Hou Jie (侯捷)// date : 2015/10/28 // compiler : DevC++ 5.11 (MinGW with GNU 4.9.9.2)//// 說明:這是侯捷 E-learning video "C++標準庫" 的實例程式.// 該課程的所有測試都出現在此.// 每一個小測試單元都被放進一個 namespace 中, // 如此保持各單元間最大的獨立性.// 每個 namespace 上方皆有該單元相應的 #include <..原创 2020-08-23 20:43:51 · 491 阅读 · 1 评论 -
C++内存报错汇总
* 0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory* 0xABADCAFE : A startup to this value to initialize all free memory to catch errant pointers* 0xBAADF00D : Used by Microsoft's LocalAlloc(LME原创 2020-08-23 00:02:58 · 304 阅读 · 0 评论 -
C++11支持的for循环
#include <iostream>#include <vector>using namespace std;int main(){ int foo[9] = {1,2,3,4,5,6,7,8,9}; vector<int> vi(foo,foo+9); for (int& elem:vi) { elem *= 3; cout << elem << endl; } return 0;}原创 2020-08-22 21:09:23 · 413 阅读 · 0 评论 -
C++ set容器
set容器使用方法如下面代码所示,需要注意的是,读取set容器最后一个值时不可用end(),需要用--end()或者rbegin()#include <iostream>#include <set>using namespace std;int main(){ set<int> s; s.insert(1); s.insert(2); s.insert(3); s.insert(1); for (auto iter = s.begin();原创 2020-08-12 17:35:52 · 620 阅读 · 0 评论 -
C++虚继承
#include <iostream>using namespace std;class A {public:void printA() {cout<<"this is A\n";}};class B:virtual public A{}; //这里需要虚继承 public virtual亦可class C:virtual public A{};//这里需要虚继承class D:public B,public C{};int main(){D d;d.pri.原创 2020-06-05 17:37:23 · 227 阅读 · 0 评论 -
C++signal函数的使用
#include "stdafx.h"#include <signal.h> #include <stdio.h> #include <windows.h> #include <iostream>using namespace std;void catch_signal(int) { cout << "Program interrupted by Ctrl-C [SIGINT,2]\n"; exit(0); }int ma.原创 2020-06-04 18:53:57 · 1998 阅读 · 0 评论 -
C++中虚函数的作用
我最近刚刚开始学习C++,一直对虚函数有些困惑,不知道它有什么用处,于是写了段代码测试了一下,发现它在面向对象编程中的确很有用,下面是代码。#include <iostream>#include <vector>using namespace std;class A{public: virtual void vfunc1() { cout << "A's vfunc1" << endl; } void func1() { c原创 2020-06-03 22:50:34 · 358 阅读 · 0 评论 -
POSIX简介
POSIX表示可移植操作系统接口(Portable Operating System Interface of UNIX,缩写为 POSIX ),POSIX标准定义了操作系统应该为应用程序提供的接口标准。POSIX标准意在期望获得源代码级别的软件可移植性。换句话说,为一个POSIX兼容的操作系统编写的程序,应该可以在任何其它的POSIX操作系统(即使是来自另一个厂商)上编译执行。简单总结:完成同一功能,不同内核提供的系统调用(也就是一个函数)是不同的,例如创建进程,linux下是fork函数,..转载 2020-06-01 16:41:55 · 540 阅读 · 0 评论 -
C++类型转换函数与转换构造函数
#include "stdafx.h"#include <iostream>using namespace std;class Complex{public:Complex( ){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}operator double( ) {return real;} //类型...转载 2019-03-07 11:00:31 · 967 阅读 · 0 评论 -
C++ 输入/输出流
两个重要的输入/输出流一个iostream对象可以是数据的源或目的两个重要的i/o流类都是从iostream派生的,它们是fstream和stringstream。这些类继承了前面描述的istream和ostream类的功能fstream类fstream类支持磁盘文件输入和输出如果需要在同一个程序中从一个特定磁盘文件读并写到该磁盘文件,可以构造一个fstream对象一个fs...原创 2019-03-04 10:26:45 · 215 阅读 · 0 评论 -
C++输入流
重要的输入流类istream类最适合用于顺序文本模式输入,cin是其实例ifstream类支持磁盘文件输入istringstream构造输入流对象如果在构造函数中指定一个文件名,在构造该对象时该文件便自动打开ifstream myFile("filename");在调用默认构造函数之后使用open函数来打开文件ifstream file;file.open("f...原创 2019-03-03 22:11:01 · 3797 阅读 · 0 评论 -
C++ override与final
override多态行为的基础:基类声明虚函数,派生类声明一个函数覆盖该虚函数;覆盖要求:函数签名(signature)完全一致;函数签名包括:函数名 参数列表 constoverride作用是保证派生类函数能覆盖基类的函数。#include "stdafx.h"using namespace std;class Base1{public: virtual void...原创 2019-02-27 13:11:15 · 434 阅读 · 0 评论 -
C++抽象类
纯虚函数:纯虚函数是一个在基类中声明的虚函数,它在该基类中没有定义具体的操作内容,要求各派生类根据实际需要定义自己的版本virtual 函数类型 函数名(参数表)=0;//等于0表示没有函数体带有纯虚函数的类称为抽象类,不可实例化抽象类的作用:将有关的数据和行为组织在一个继承层次结构中,保证派生类具有要求的行为。对应暂时无法实现的函数,可以声明为纯虚函数,留给派生类去实现。需要注意...原创 2019-02-27 12:46:52 · 13613 阅读 · 0 评论 -
C++ 理解 __declspec(dllexport)和__declspec(dllimport)
1、解决的问题: 考虑下面的需求,使用一个方法,一个是提供者,一个是使用者,二者之间的接口是头文件。头文件中声明了方法,在提供者那里方法应该被声明为__declspec(dllexport),在使用者那里,方法应该被声明为__declspec(dllimport)。二者使用同一个头文件,作为接口,怎么办呢?2、解决办法: 使用条件编译:定义一个变量,针对提供者和使用者,设置不同的值...转载 2019-02-26 20:39:32 · 1222 阅读 · 0 评论 -
C++输出流
最重要的三个输出流ostreamofstreamostringstream预先定义的输出流对象cout 标准输出cerr 标准错误输出,没有缓冲,发送给它的内容立即被输出clog 类似于cerr,但是有缓冲,缓冲区满时被输出构造输出流对象ofstream类支持磁盘文件输出如果在构造函数中指定一个文件名,当构造这个文件时该文件是自动打开的ofstream ...原创 2019-03-02 22:14:38 · 1123 阅读 · 0 评论 -
C++ I/O流的概念及流类库结构
流是信息流动的一种抽象流对象与文件操作程序建立一个流对象指定这个流对象与某个文件对象建立连接程序操作流对象流对象通过文件系统对所连接的文件对象产生作用提取与插入读操作在流数据抽象中被称为从流中提取写操作被称为向流中插入...原创 2019-03-02 21:04:47 · 633 阅读 · 0 评论 -
C++分割字符串
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "string"#include "vector"#include "iostream"using namespace std;vector<string> split(const string line, const c...原创 2019-07-17 21:23:03 · 228 阅读 · 0 评论 -
C++迭代器
输入流迭代器istream_iterator<T>以输入流如cin为参数构造,可用*(p++)获得下一个输入的元素输出流迭代器ostream_iterator<T>构造时需要提供输出流如cout可用(*p++)=x将x输出到输出流#include "stdafx.h"#include <vector>#include <i...原创 2019-03-02 20:41:30 · 262 阅读 · 0 评论 -
vs2012下 error C4996:‘strcpy’:this function or variable maybe unsafe
error C4996:‘strcpy’:this function or variable maybe unsafe:使用strcpy函数的时候报错解决方案:项目---->属性----->加入_CRT_SECURE_NO_WARNINGS点击确定即可...原创 2019-03-17 19:44:48 · 1807 阅读 · 0 评论 -
vs2012常用快捷键
1. 强迫智能感知:Ctrl+J;2.强迫智能感知显示参数信息:Ctrl-Shift-空格;3.格式化整个块:Ctrl+K+F4. 检查括号匹配(在左右括号间切换): Ctrl +]5. 选中从光标起到行首(尾)间的代码: Shift + Home(End)6. 在方法定义和调用之点切换:Ctrl+Shift+7(8)7. 设置断点:F98. 使用Tab增加缩进,Shift+Tab减...转载 2019-03-17 20:06:03 · 421 阅读 · 0 评论 -
C++GPS潮位提取
#include "stdafx.h"#include <string>#include <fstream>#include <iostream>#include <stdlib.h>#include <cmath>#include <iomanip>#include <vector>#inclu...原创 2019-05-15 16:46:53 · 366 阅读 · 0 评论 -
C++联合体
#include "stdafx.h"#include <iostream>#include <string>using namespace std;class ExamInfo{public: ExamInfo(string name,char grade):name(name),mode(GRADE),grade(grade){} ExamInfo(...原创 2019-01-27 14:08:11 · 885 阅读 · 1 评论 -
多线程通俗解释
每个进程必定有且只有一个主线程线程:一条代码的执行通路,每个线程都需要一个独立的堆栈空间可通过编码创建多个线程,一般不超过200-300个...原创 2019-08-16 15:53:02 · 695 阅读 · 0 评论 -
C++ Dijkstra最短路径算法
// ShortPathcpp.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "iostream"#include "fstream"#include "string"#include "vector"#include "cmath"#include <algorithm>using namespace st...原创 2019-07-21 11:32:11 · 994 阅读 · 0 评论 -
C++ 道格拉斯-普克(DP)算法
道格拉斯-普克算法(Douglas–Peucker algorithm,亦称为拉默-道格拉斯-普克算法、迭代适应点算法、分裂与合并算法)是将曲线近似表示为一系列点,并减少点的数量的一种算法。该算法的原始类型分别由乌尔斯·拉默(Urs Ramer)于1972年以及大卫·道格拉斯(David Douglas)和托马斯·普克(Thomas Peucker)于1973年提出,并在之后的数十年中由其他学者予...原创 2019-07-21 11:28:39 · 4554 阅读 · 8 评论 -
CCF201509-1 数列分段
// ArraySegmentation.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "iostream"#include "windows.h"using namespace std;int _tmain(int argc, _TCHAR* argv[]){ int a[1000], n, count = 0; ...原创 2019-07-17 22:30:46 · 284 阅读 · 0 评论 -
vs2019编译时报错:link.exe已退出,代码2
搞了一上午,最后发现是特么的杀毒软件搞的[○・`Д´・ ○]把杀毒软件退出就ok了原创 2019-07-17 11:12:24 · 2541 阅读 · 0 评论 -
C++利用string::find()函数分割字符串
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "string"#include "vector"#include "iostream"using namespace std;vector<string> split(const string line, const s...原创 2019-07-19 10:18:28 · 1990 阅读 · 0 评论 -
CCF201509-2 日期计算
// DateCalculation.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "iostream"#include "windows.h"using namespace std;int _tmain(int argc, _TCHAR* argv[]){ DWORD start_time = GetTickCount...原创 2019-07-18 17:42:05 · 246 阅读 · 0 评论 -
C++strtok分割字符串
#include "stdafx.h"#include <string>#include <cstring>#include <fstream>#include <iostream>#include <stdlib.h>#include <vector>using namespace std;struct ...原创 2019-03-24 12:33:33 · 899 阅读 · 0 评论 -
C++ stdafx.h、targetver.h文件
stdafx是预编译头文件,在其中包含的头文件之外第一次编译时编译,之后若不修改,不会再编译这些头文件,加快了编译的速度。targetver定义了程序运行需要的最低Windows版本,如果你要在旧版本的Windows(如非NT架构的Win98)上运行,除了代码部分还需要修改这个头文件。...原创 2019-02-26 19:28:28 · 1507 阅读 · 0 评论 -
C++派生类成员的标识与访问
当派生类与积累中有相同成员时:同名隐藏规则:若未特别限定,则通过派生类对象使用的是派生类中的同名成员;如要通过派生类对象访问基类中被隐藏的同名成员,应使用基类名和作用域操作符(::)来限定。#include "stdafx.h"using namespace std;class Base1{public: int var; void print() { cout&l...原创 2019-02-20 17:31:15 · 374 阅读 · 0 评论 -
C++ 类模板示例
#include "stdafx.h"using namespace std;struct Student{ int id; double gpa;};template<class T>class Store{public: Store(); T &getElem(); void putElem(const T &...原创 2019-03-01 21:11:29 · 1262 阅读 · 0 评论 -
C++类的友元
一、类的友元1、友元是C++提供的一种破坏数据封装和数据隐藏的机制。2、通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。3、可以声明友元函数和友元类。4、为了确保数据的完整性,及数据封装与隐藏的原则,建议慎用友元。二、友元函数1、友元函数是在类声明中由关键字friend修饰说明的非成员函数,在它的函数体中能够通过对象名访问private...原创 2019-01-28 16:41:55 · 301 阅读 · 0 评论