add collections

This commit is contained in:
2025-03-20 12:31:44 +01:00
parent 4a753b0b8d
commit abb1ea4a38
9 changed files with 383 additions and 4 deletions
+14
View File
@@ -9,6 +9,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "forward_declaration", "forw
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Classes", "Classes\Classes.vcxproj", "{467AD272-2BB3-41B7-9B74-7D193D7D2AE9}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Classes", "Classes\Classes.vcxproj", "{467AD272-2BB3-41B7-9B74-7D193D7D2AE9}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Collections", "Collections\Collections.vcxproj", "{1B177393-37B1-4C7B-BCF0-163991832930}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64 Debug|ARM64 = Debug|ARM64
@@ -55,6 +57,18 @@ Global
{467AD272-2BB3-41B7-9B74-7D193D7D2AE9}.Release|x64.Build.0 = Release|x64 {467AD272-2BB3-41B7-9B74-7D193D7D2AE9}.Release|x64.Build.0 = Release|x64
{467AD272-2BB3-41B7-9B74-7D193D7D2AE9}.Release|x86.ActiveCfg = Release|Win32 {467AD272-2BB3-41B7-9B74-7D193D7D2AE9}.Release|x86.ActiveCfg = Release|Win32
{467AD272-2BB3-41B7-9B74-7D193D7D2AE9}.Release|x86.Build.0 = Release|Win32 {467AD272-2BB3-41B7-9B74-7D193D7D2AE9}.Release|x86.Build.0 = Release|Win32
{1B177393-37B1-4C7B-BCF0-163991832930}.Debug|ARM64.ActiveCfg = Debug|ARM64
{1B177393-37B1-4C7B-BCF0-163991832930}.Debug|ARM64.Build.0 = Debug|ARM64
{1B177393-37B1-4C7B-BCF0-163991832930}.Debug|x64.ActiveCfg = Debug|x64
{1B177393-37B1-4C7B-BCF0-163991832930}.Debug|x64.Build.0 = Debug|x64
{1B177393-37B1-4C7B-BCF0-163991832930}.Debug|x86.ActiveCfg = Debug|Win32
{1B177393-37B1-4C7B-BCF0-163991832930}.Debug|x86.Build.0 = Debug|Win32
{1B177393-37B1-4C7B-BCF0-163991832930}.Release|ARM64.ActiveCfg = Release|ARM64
{1B177393-37B1-4C7B-BCF0-163991832930}.Release|ARM64.Build.0 = Release|ARM64
{1B177393-37B1-4C7B-BCF0-163991832930}.Release|x64.ActiveCfg = Release|x64
{1B177393-37B1-4C7B-BCF0-163991832930}.Release|x64.Build.0 = Release|x64
{1B177393-37B1-4C7B-BCF0-163991832930}.Release|x86.ActiveCfg = Release|Win32
{1B177393-37B1-4C7B-BCF0-163991832930}.Release|x86.Build.0 = Release|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
+1
View File
@@ -159,6 +159,7 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
+1
View File
@@ -1,4 +1,5 @@
#include "Entity.h" #include "Entity.h"
#include <iostream>
Entity::Entity() Entity::Entity()
{ {
+28 -2
View File
@@ -1,3 +1,5 @@
#include <format>
#include "Monster.h" #include "Monster.h"
Monster::Monster(int strength) : Entity("My Monster"), _strength(strength) Monster::Monster(int strength) : Entity("My Monster"), _strength(strength)
@@ -5,10 +7,34 @@ Monster::Monster(int strength) : Entity("My Monster"), _strength(strength)
this->name = "My Monster"; this->name = "My Monster";
} }
//Monster Monster::Create(Monsters type)
//{
// Monster monster;
// switch (type)
// {
// case Ork:
// monster._strength = 10;
// monster._speed = 1.0f;
// break;
// case Troll:
// monster._strength = 5;
// monster._speed = 3.0f;
// break;
// case Elve:
// monster._strength = 1;
// monster._speed = 10.0f;
// break;
// default:
// break;
// }
//
// return monster;
//}
std::string Monster::ToString() std::string Monster::ToString()
{ {
return name + " " + "(" + std::to_string(_strength) + ")"; //return name + " " + "(" + std::to_string(_strength) + ")";
// TODO: show format return std::format("{} ({})", name, _strength);
} }
int Monster::Compare(ICompareable* other) int Monster::Compare(ICompareable* other)
+10
View File
@@ -2,15 +2,25 @@
#include "Entity.h" #include "Entity.h"
#include "ICompareable.h" #include "ICompareable.h"
//enum Monsters
//{
// Ork,
// Troll,
// Elve
//};
class Monster : public Entity, public ICompareable class Monster : public Entity, public ICompareable
{ {
public: public:
Monster(int strength); Monster(int strength);
//static Monster Create(Monsters type);
virtual void Move() = 0; // abstract (pure virtual method) virtual void Move() = 0; // abstract (pure virtual method)
std::string ToString() override; std::string ToString() override;
int Compare(ICompareable* other) override; int Compare(ICompareable* other) override;
protected: protected:
int _strength = 0; int _strength = 0;
float _speed = 0.0f;
}; };
+1
View File
@@ -12,6 +12,7 @@ int main()
entity.name = "My Entity"; entity.name = "My Entity";
//Monster monster; // can't be instantiated, because Monster is abstract //Monster monster; // can't be instantiated, because Monster is abstract
//Monster monster2 = Monster::Create(Monsters::Ork);
Ork ork; Ork ork;
ork.name = "My Ork"; ork.name = "My Ork";
+190
View File
@@ -0,0 +1,190 @@
<?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>
<ProjectConfiguration Include="Debug|ARM64">
<Configuration>Debug</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ARM64">
<Configuration>Release</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{1b177393-37b1-4c7b-bcf0-163991832930}</ProjectGuid>
<RootNamespace>Collections</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>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</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>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<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|ARM64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<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>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<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|ARM64'">
<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="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
@@ -0,0 +1,22 @@
<?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="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
+114
View File
@@ -0,0 +1,114 @@
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
#define INT_LIST list<int> // simple text replacement
// type definitions
typedef list<int> IntegerList;
typedef IntegerList::iterator IntListIterator;
void printArray(int numbers[]);
void printArray(int numbers[], int count);
int main()
{
// C Arrays
int numbers[10];
for (size_t i = 0; i < 10; i++)
{
numbers[i] = 0;
}
numbers[0] = 10;
cout << "numbers[5]: " << numbers[5] << endl;
int numbers2[] = { 0, 1, 2, 3, 4, 5 };
int numbers3[10] = { 0, 1, 2, 3, 4, 5 };
// 2d array - 10 rows, 5 columns
int numbers4[10 * 5];
numbers4[3 + 7 * 5] = 10; // 3rd column, 7th row
// size have to be always const, except by using pointers
const int count = 10;
int numbers5[count];
int length = sizeof(numbers2) / sizeof(numbers2[0]); // works locally only
for (size_t i = 0; i < std::size(numbers2); i++) // size is working locally only
{
cout << numbers2[i] << " ";
}
cout << endl;
cout << "sizeof(numbers2): " << sizeof(numbers2) << endl;
printArray(numbers2);
printArray(numbers2, std::size(numbers2));
// stl collections
vector<int> numbers6;
vector<int> numbers7 = { 0, 1, 2, 3 };
numbers6.push_back(10); // add an item at the end
numbers6.pop_back(); // remove an item at the end
numbers7[2] *= 10;
vector<int> numbers8;
numbers8.reserve(100);
cout << "size of numbers8: " << numbers8.size() << endl;
cout << "capacity of numbers8: " << numbers8.capacity() << endl;
printArray(numbers7.data(), numbers7.size());
list<int> numbersList;
numbersList.push_back(10);
numbersList.push_front(100);
//numbersList[1] = 200; // random access is not available for lists
list<int>::iterator it = numbersList.begin();
for (; it != numbersList.end(); it++)
{
cout << *it << " ";
}
cout << endl;
numbersList.insert(--it, 20);
for (it = numbersList.begin(); it != numbersList.end(); it++)
{
cout << *it << " ";
}
cout << endl;
for (int n : numbersList)
{
}
for_each(numbers7.begin(), numbers7.end(), [](const int& n) {
cout << n << " ";
});
return 0;
}
void printArray(int numbers[])
{
int length = sizeof(numbers) / sizeof(numbers[0]); // works not outside of local scope
cout << "sizeof(numbers2): " << sizeof(numbers) << endl;
//for (size_t i = 0; i < std::size(numbers); i++) // size is not working outside of local scope
//{
// cout << numbers[i] << " ";
//}
//cout << endl;
}
void printArray(int numbers[], int count)
{
for (size_t i = 0; i < count; i++)
{
cout << numbers[i] << " ";
}
cout << endl;
}