feat: add Observer Pattern
This commit is contained in:
parent
492370ae45
commit
ed401eda98
9
Studio/DesignPatterns/DesignPatterns.cpp
Normal file
9
Studio/DesignPatterns/DesignPatterns.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// DesignPatterns.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
//int main()
|
||||||
|
//{
|
||||||
|
// std::cout << "Hello World!\n";
|
||||||
|
//}
|
153
Studio/DesignPatterns/DesignPatterns.vcxproj
Normal file
153
Studio/DesignPatterns/DesignPatterns.vcxproj
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup Label="ProjectConfigurations">
|
||||||
|
<ProjectConfiguration Include="Debug|Win32">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|Win32">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Debug|x64">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|x64">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
</ItemGroup>
|
||||||
|
<PropertyGroup Label="Globals">
|
||||||
|
<VCProjectVersion>16.0</VCProjectVersion>
|
||||||
|
<Keyword>Win32Proj</Keyword>
|
||||||
|
<ProjectGuid>{57f3c035-9c90-4e9f-934a-032804827444}</ProjectGuid>
|
||||||
|
<RootNamespace>DesignPatterns</RootNamespace>
|
||||||
|
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||||
|
<ImportGroup Label="ExtensionSettings">
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="Shared">
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<PropertyGroup Label="UserMacros" />
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<LinkIncremental>true</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<LinkIncremental>false</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<LinkIncremental>true</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<LinkIncremental>false</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="DesignPatterns.cpp" />
|
||||||
|
<ClCompile Include="Observer.cpp" />
|
||||||
|
<ClCompile Include="Singleton.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Observer.h" />
|
||||||
|
<ClInclude Include="Singleton.h" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
36
Studio/DesignPatterns/DesignPatterns.vcxproj.filters
Normal file
36
Studio/DesignPatterns/DesignPatterns.vcxproj.filters
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup>
|
||||||
|
<Filter Include="Source Files">
|
||||||
|
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||||
|
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="Header Files">
|
||||||
|
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||||
|
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="Resource Files">
|
||||||
|
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||||
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
|
</Filter>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="DesignPatterns.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Singleton.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Observer.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Singleton.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Observer.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
1
Studio/DesignPatterns/Observer.cpp
Normal file
1
Studio/DesignPatterns/Observer.cpp
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "Observer.h"
|
106
Studio/DesignPatterns/Observer.h
Normal file
106
Studio/DesignPatterns/Observer.h
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
/*******************************************************************
|
||||||
|
* Copyright (c) 2022~2023 XMuli All rights reserved.
|
||||||
|
* GitHub: https://github.com/XMuli
|
||||||
|
* Description: 观察者模式 Observer Pattern
|
||||||
|
* Reference: 情景 → Subject 发送一个消息,然后所有的 Observer A、B、C
|
||||||
|
* 都有收到,并且对相同的消息,做出各自不同的操作响应
|
||||||
|
* See: https://www.cnblogs.com/suzhou/p/dp16obsvr.html
|
||||||
|
******************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <list>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Observer { // 观察者
|
||||||
|
public:
|
||||||
|
virtual void update(string msg) = 0;
|
||||||
|
virtual void doSomeThing() = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
string m_msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Subject {
|
||||||
|
public:
|
||||||
|
Subject() = default;
|
||||||
|
virtual ~Subject() = default;
|
||||||
|
virtual void addObsvr(Observer* obj) = 0;
|
||||||
|
virtual void removeObsvr(Observer* obj) = 0;
|
||||||
|
virtual void notify(string msg) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
//---------------------------------------------------------
|
||||||
|
class ConcreteSubject: public Subject {
|
||||||
|
public:
|
||||||
|
ConcreteSubject() {};
|
||||||
|
virtual ~ConcreteSubject() { m_obsvrs.clear(); }
|
||||||
|
virtual void addObsvr(Observer* obj) override {
|
||||||
|
m_obsvrs.push_back(obj);
|
||||||
|
}
|
||||||
|
virtual void removeObsvr(Observer* obj) override {
|
||||||
|
m_obsvrs.remove(obj);
|
||||||
|
}
|
||||||
|
virtual void notify(string msg) override {
|
||||||
|
for (const auto& it : m_obsvrs)
|
||||||
|
it->update(msg);
|
||||||
|
|
||||||
|
for (const auto& it : m_obsvrs)
|
||||||
|
it->doSomeThing();
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
list<Observer*> m_obsvrs;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AObserver : public Observer {
|
||||||
|
public:
|
||||||
|
void update(string msg) override { m_msg = msg; }
|
||||||
|
virtual void doSomeThing() override { cout << "AObserver: 对 " << m_msg << " 进行 A 的操作" << endl; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class BObserver : public Observer {
|
||||||
|
public:
|
||||||
|
void update(string msg) override { m_msg = msg; }
|
||||||
|
virtual void doSomeThing() override { cout << "BObserver: 对 " << m_msg << " 进行 B 的操作" << endl; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class CObserver : public Observer {
|
||||||
|
public:
|
||||||
|
void update(string msg) override { m_msg = msg; }
|
||||||
|
virtual void doSomeThing() override { cout << "CObserver: 对 " << m_msg << " 进行 C 的操作" << endl; }
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
AObserver* a = new AObserver();
|
||||||
|
BObserver* b = new BObserver();
|
||||||
|
CObserver* c = new CObserver();
|
||||||
|
ConcreteSubject* concreSubject = new ConcreteSubject();
|
||||||
|
|
||||||
|
concreSubject->addObsvr(a);
|
||||||
|
concreSubject->addObsvr(b);
|
||||||
|
concreSubject->addObsvr(c);
|
||||||
|
concreSubject->notify("messagebox 1");
|
||||||
|
|
||||||
|
concreSubject->removeObsvr(c);
|
||||||
|
concreSubject->notify("messagebox 2");
|
||||||
|
|
||||||
|
//a->doSomeThing();
|
||||||
|
//b->doSomeThing();
|
||||||
|
//c->doSomeThing();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*****************************打印结果*******************************
|
||||||
|
AObserver: 对 messagebox 1 进行 A 的操作
|
||||||
|
BObserver: 对 messagebox 1 进行 B 的操作
|
||||||
|
CObserver: 对 messagebox 1 进行 C 的操作
|
||||||
|
|
||||||
|
AObserver: 对 messagebox 2 进行 A 的操作
|
||||||
|
BObserver: 对 messagebox 2 进行 B 的操作
|
||||||
|
******************************************************************/
|
1
Studio/DesignPatterns/Singleton.cpp
Normal file
1
Studio/DesignPatterns/Singleton.cpp
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "Singleton.h"
|
24
Studio/DesignPatterns/Singleton.h
Normal file
24
Studio/DesignPatterns/Singleton.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*******************************************************************
|
||||||
|
* Copyright (c) 2022~2023 XMuli All rights reserved.
|
||||||
|
* GitHub: https://github.com/XMuli
|
||||||
|
* Description: 单例模式 Singleton Pattern 的最优雅实现
|
||||||
|
* Reference: https://zhuanlan.zhihu.com/p/37469260 单例介绍及好一篇
|
||||||
|
******************************************************************/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// 《Effective C++》提出了一种更优雅的单例模式实现,称为 Meyers' Singleton
|
||||||
|
class Singleton
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static Singleton& instance() {
|
||||||
|
static Singleton instan;
|
||||||
|
return instan;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Singleton() {};
|
||||||
|
~Singleton() {};
|
||||||
|
Singleton(const Singleton& obj) = delete;
|
||||||
|
Singleton& operator=(const Singleton& obj) = delete;
|
||||||
|
};
|
||||||
|
|
@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.32802.440
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Studio", "Studio\Studio.vcxproj", "{11117C51-BEAF-4F63-B386-CDB0760BCDE8}"
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Studio", "Studio\Studio.vcxproj", "{11117C51-BEAF-4F63-B386-CDB0760BCDE8}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DesignPatterns", "DesignPatterns\DesignPatterns.vcxproj", "{57F3C035-9C90-4E9F-934A-032804827444}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|x64 = Debug|x64
|
Debug|x64 = Debug|x64
|
||||||
@ -21,6 +23,14 @@ Global
|
|||||||
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x64.Build.0 = Release|x64
|
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x64.Build.0 = Release|x64
|
||||||
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x86.ActiveCfg = Release|Win32
|
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x86.ActiveCfg = Release|Win32
|
||||||
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x86.Build.0 = Release|Win32
|
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x86.Build.0 = Release|Win32
|
||||||
|
{57F3C035-9C90-4E9F-934A-032804827444}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{57F3C035-9C90-4E9F-934A-032804827444}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{57F3C035-9C90-4E9F-934A-032804827444}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{57F3C035-9C90-4E9F-934A-032804827444}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{57F3C035-9C90-4E9F-934A-032804827444}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{57F3C035-9C90-4E9F-934A-032804827444}.Release|x64.Build.0 = Release|x64
|
||||||
|
{57F3C035-9C90-4E9F-934A-032804827444}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{57F3C035-9C90-4E9F-934A-032804827444}.Release|x86.Build.0 = Release|Win32
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
1
Studio/Studio/BinarySearch.cpp
Normal file
1
Studio/Studio/BinarySearch.cpp
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "BinarySearch.h"
|
88
Studio/Studio/BinarySearch.h
Normal file
88
Studio/Studio/BinarySearch.h
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int binarySearch(vector<int> vec, int target)
|
||||||
|
{
|
||||||
|
int left = 0;
|
||||||
|
int right = vec.size() - 1;
|
||||||
|
|
||||||
|
while (left <= right) {
|
||||||
|
int mid = left + (right - left) / 2;
|
||||||
|
|
||||||
|
if (target < vec[mid]) {
|
||||||
|
right = mid - 1;
|
||||||
|
} else if (target > vec[mid]) {
|
||||||
|
left = mid + 1;
|
||||||
|
} else {
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int binarySearch2(vector<int> vec, int target)
|
||||||
|
{
|
||||||
|
int left = 0;
|
||||||
|
int right = vec.size();
|
||||||
|
|
||||||
|
while (left < right) {
|
||||||
|
int mid = left + (right - left) / 2;
|
||||||
|
|
||||||
|
if (target < vec[mid]) {
|
||||||
|
right = mid;
|
||||||
|
} else if (target > vec[mid]) {
|
||||||
|
left = mid + 1;
|
||||||
|
} else {
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int binarySearch3(vector<int>& vec, int left, int right, int target)
|
||||||
|
{
|
||||||
|
if (left > right) // target 在数组左右两侧时的场景
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int mid = left + (right - left) / 2;
|
||||||
|
if (target < vec[mid]) {
|
||||||
|
return binarySearch3(vec, left, mid -1, target);
|
||||||
|
} else if (target > vec[mid]) {
|
||||||
|
return binarySearch3(vec, mid + 1, right, target);
|
||||||
|
} else {
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 毒药版-错误百出版本
|
||||||
|
//int binarySearch4(vector<int> vec, int target)
|
||||||
|
//{
|
||||||
|
// int mid = vec.size() / 2;
|
||||||
|
//
|
||||||
|
// if (target < vec[mid]) {
|
||||||
|
// vector<int> t(vec.begin(), vec.begin() + mid);
|
||||||
|
// return binarySearch4(t, target);
|
||||||
|
// } else if (target > vec[mid]) {
|
||||||
|
// vector<int> t(vec.begin() + mid + 1, vec.end());
|
||||||
|
// return binarySearch4(t, target);
|
||||||
|
// } else {
|
||||||
|
// return mid;
|
||||||
|
// }
|
||||||
|
// return -1;
|
||||||
|
//}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
vector<int> v = { 0, 3, 5, 6, 7 };
|
||||||
|
int target = 8;
|
||||||
|
cout << "binarySearch:" << binarySearch(v, target) << endl
|
||||||
|
<< "binarySearch2:" << binarySearch2(v, target) << endl
|
||||||
|
<< "binarySearch3:" << binarySearch3(v, 0, v.size() - 1, target) << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -89,22 +89,22 @@ A fn() {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
//int main()
|
||||||
{
|
//{
|
||||||
A a1("a1"); // default constructor
|
// A a1("a1"); // default constructor
|
||||||
A a2(a1); // copy constructor
|
// A a2(a1); // copy constructor
|
||||||
A a3 = a1; // copy constructor
|
// A a3 = a1; // copy constructor
|
||||||
a1 = a3; // copy assignment constructor
|
// a1 = a3; // copy assignment constructor
|
||||||
std::cout << "----------------------------\n\n";
|
// std::cout << "----------------------------\n\n";
|
||||||
|
//
|
||||||
//fn(); // function returning a A object
|
// //fn(); // function returning a A object
|
||||||
A a5 = std::move(a1); // move constructor
|
// A a5 = std::move(a1); // move constructor
|
||||||
A a6; // default constructor
|
// A a6; // default constructor
|
||||||
a6 = std::move(a1); // move assignment constructor
|
// a6 = std::move(a1); // move assignment constructor
|
||||||
std::cout << "Hello World!\n";
|
// std::cout << "Hello World!\n";
|
||||||
|
//
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
//}
|
||||||
|
|
||||||
/*************************** 运行结果 *******************************
|
/*************************** 运行结果 *******************************
|
||||||
* no-default-val constructor
|
* no-default-val constructor
|
||||||
|
@ -139,12 +139,14 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="BinarySearch.cpp" />
|
||||||
<ClCompile Include="SharedPtr.cpp" />
|
<ClCompile Include="SharedPtr.cpp" />
|
||||||
<ClCompile Include="SpecialMembers.cpp" />
|
<ClCompile Include="SpecialMembers.cpp" />
|
||||||
<ClCompile Include="Studio.cpp" />
|
<ClCompile Include="Studio.cpp" />
|
||||||
<ClCompile Include="UniquePtr.cpp" />
|
<ClCompile Include="UniquePtr.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="BinarySearch.h" />
|
||||||
<ClInclude Include="MemoryAlignment.h" />
|
<ClInclude Include="MemoryAlignment.h" />
|
||||||
<ClInclude Include="SharedPtr.h" />
|
<ClInclude Include="SharedPtr.h" />
|
||||||
<ClInclude Include="SpecialMembers.h" />
|
<ClInclude Include="SpecialMembers.h" />
|
||||||
|
@ -27,6 +27,9 @@
|
|||||||
<ClCompile Include="SpecialMembers.cpp">
|
<ClCompile Include="SpecialMembers.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="BinarySearch.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="MemoryAlignment.h">
|
<ClInclude Include="MemoryAlignment.h">
|
||||||
@ -41,5 +44,8 @@
|
|||||||
<ClInclude Include="SpecialMembers.h">
|
<ClInclude Include="SpecialMembers.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="BinarySearch.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Loading…
Reference in New Issue
Block a user