Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down Expand Up @@ -63,6 +65,12 @@ cpp-coding-exercise/

## Building

### C++ Version
```bash
sudo apt update
sudo apt install g++-14
```

### Build everything

```bash
Expand Down
1 change: 1 addition & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ endif
# --------------------------
# Compiler / Linker flags
# --------------------------
CXX := g++-14
CXXFLAGS := -std=c++23 -Wall -Wextra -Werror $(OPTFLAGS) $(SAN_FLAGS)
LDFLAGS := $(SAN_FLAGS)

Expand Down
31 changes: 31 additions & 0 deletions map-with-unknown-key/Makefile
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions map-with-unknown-key/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
What happens if you access an unknown key in the std::map, std::unordered_map?
*/
#include <iostream>
#include <map>
#include <string>
#include <format>
#include <print>

int
main()
{
std::map<std::string, int> 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;
}
5 changes: 5 additions & 0 deletions map-with-unknown-key/tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -ex

./map-with-unknown-key
Binary file removed oop-with-exception/oop-with-exception
Binary file not shown.