Skip to content

Instantly share code, notes, and snippets.

@imsjz
imsjz / factory.cpp
Created March 5, 2020 04:01
简单工厂模式和工厂模式
#include <iostream>
using namespace std;
enum PRODUCTTYPE {SFJ, XSL}; // 两种产品:舒肤佳,夏雪莲
// 简单工厂模式
namespace simple_factory {
// 抽象基类用首字母小写
class soapBase{
public:
virtual ~soapBase(){};
@imsjz
imsjz / string_to_int.cpp
Created February 24, 2020 05:36
c语言风格和c++风格stirng转int
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// c语言风格将string转为int, 缺点是不会进行数字检查,遇到非法字符就终止了,直接输出0
void CStyle() {
string str = "123s";
int a = atoi(str.c_str());
// 支持多进制转换
@imsjz
imsjz / cpp_thread.cpp
Created February 24, 2020 05:19
c++11新特性:可以用库函数创建平台无关的多线程
#include <iostream>
#include <thread>
#include <chrono> // 时间
#include <algorithm>
using namespace std;
using namespace std::chrono;
typedef unsigned long ul;
ul OddSum = 0;
ul EvenSum = 0;
@imsjz
imsjz / sort_algorithm_NSort.cpp
Last active February 20, 2020 17:41
实现冒泡、插入、选择排序;快速排序。
//
// Created by sanjay on 2020/2/21.
//
#include "NSort.h"
void NSort::QuickSort(int *array, int l, int r){
if(l < r) {
int i = l, j = r, pivot = array[l];
while (i < j) {
@imsjz
imsjz / sizeof_class.cpp
Created February 20, 2020 15:18
验证空类的大小,以及继承类的大小
#include <iostream>
using namespace std;
class X {};
class Y : public virtual X {};
class Z : public virtual X {};
class A : public Y, public Z{};
@imsjz
imsjz / stack_frame.cpp
Created February 19, 2020 04:03
学习栈帧
#include <iostream>
# 学习函数调用的过程和栈帧的理解
# 推荐链接:https://segmentfault.com/a/1190000007977460
int MyFunction(int x, int y, int z) {
int a, b, c;
a = 10;
b = 5;
c = 2;
#include <iostream>
#include <string>
namespace sanjay{
// 写一个简易版哈希表类
class MyHashTable {
public:
std::string operator[](const std::string& key) const {
if(key == key_) {
return value_;
@imsjz
imsjz / volatile.cpp
Created February 17, 2020 09:41
有关volatile的例子。一般用atomic来保存多线程的同步性,而非volatile。
#include <iostream>
#include <thread>
// 这个链接说这样写会产生问题,但是在mac上测试并没有问题
// 链接:https://liam.page/2018/01/18/volatile-in-C-and-Cpp/
namespace sanjay1 {
bool flag = false;
using Type = int;
void test_volatile_demo() {
@imsjz
imsjz / merge_sort_modified.cpp
Created February 16, 2020 15:02
更正了新的版本
#include <iostream>
#include <vector>
/* Copyright sanjay 2020 */
// 本程序使用两种方式实现归并排序,一种是c方式,一种是stl库方式
// 两者差异不大,仅传参类型不一样
namespace c_style {
void Merge(int a[], int left, int mid , int right) {
// 需要求得每一半的长度,以便申请空间
@imsjz
imsjz / merge_sort_demo.cpp
Created February 16, 2020 14:40
Two ways to realize merge sort algorithm.
#include <iostream>
#include <vector>
/* Copyright sanjay 2020 */
// 本程序使用两种方式实现归并排序,一种是c方式,一种是stl库方式
// 两者差异不大,仅传参类型不一样
namespace c_style {
void Merge(int a[], int left, int mid , int right) {
// 需要求得每一半的长度,以便申请空间