diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4131adb..2c66c49 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: - name: Install build tools run: | sudo apt-get update - sudo apt-get install -y build-essential g++ clang clang-format + sudo apt-get install -y build-essential g++-14 clang clang-format # 3. Clang-format check - name: Clang-format Check diff --git a/README.md b/README.md index cb411da..cdedfac 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ Examples include (and will expand to): * [integer-factorization](./integer-factorization/) * Encoding * [rot47](./rot47/) +* Data Structures + * [map-with-unknown-key](./map-with-unknown-key/) * OOP * [virtual-interface](./virtual-interface/) * [oop-with-exception](./oop-with-exception/) @@ -63,6 +65,12 @@ cpp-coding-exercise/ ## Building +### C++ Version +```bash +sudo apt update +sudo apt install g++-14 +``` + ### Build everything ```bash diff --git a/common.mk b/common.mk index fd7ebf1..068230a 100644 --- a/common.mk +++ b/common.mk @@ -26,6 +26,7 @@ endif # -------------------------- # Compiler / Linker flags # -------------------------- +CXX := g++-14 CXXFLAGS := -std=c++23 -Wall -Wextra -Werror $(OPTFLAGS) $(SAN_FLAGS) LDFLAGS := $(SAN_FLAGS) diff --git a/map-with-unknown-key/Makefile b/map-with-unknown-key/Makefile new file mode 100644 index 0000000..ab8b3c0 --- /dev/null +++ b/map-with-unknown-key/Makefile @@ -0,0 +1,31 @@ +# pull in shared compiler settings +include ../common.mk + +# per-example flags +# CXXFLAGS += -pthread + +## get it from the folder name +TARGET := $(notdir $(CURDIR)) +## all *.cpp files in this folder +SRCS := $(wildcard *.cpp) +OBJS := $(SRCS:.cpp=.o) + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CXX) $(CXXFLAGS) -o $@ $^ + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +run: $(TARGET) + ./$(TARGET) $(ARGS) + +clean: + rm -f $(OBJS) $(TARGET) + +# Delegates to top-level Makefile +check-format: + $(MAKE) -f ../Makefile check-format DIR=$(CURDIR) + +.PHONY: all clean run check-format diff --git a/map-with-unknown-key/main.cpp b/map-with-unknown-key/main.cpp new file mode 100644 index 0000000..f905788 --- /dev/null +++ b/map-with-unknown-key/main.cpp @@ -0,0 +1,19 @@ +/** +What happens if you access an unknown key in the std::map, std::unordered_map? +*/ +#include +#include +#include +#include +#include + +int +main() +{ + std::map my_map; + std::print("Accessing unknown key 'unknown_key' in std::map:\n"); + int value = my_map["unknown_key"]; + std::print("Value: {}\n", value); // Should print 0, as default-constructed int is 0 + std::print("Map size after access: {}\n", my_map.size()); // Should print 1 + return 0; +} diff --git a/map-with-unknown-key/tests.sh b/map-with-unknown-key/tests.sh new file mode 100755 index 0000000..482ca04 --- /dev/null +++ b/map-with-unknown-key/tests.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +set -ex + +./map-with-unknown-key diff --git a/oop-with-exception/oop-with-exception b/oop-with-exception/oop-with-exception deleted file mode 100755 index 1e86554..0000000 Binary files a/oop-with-exception/oop-with-exception and /dev/null differ