Skip to content

Commit 3e82a2b

Browse files
Merge pull request #14 from DoctorLai/map-with-unknown-key
Map with unknown key
2 parents e3b2e30 + 5a96372 commit 3e82a2b

File tree

7 files changed

+65
-1
lines changed

7 files changed

+65
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Install build tools
2121
run: |
2222
sudo apt-get update
23-
sudo apt-get install -y build-essential g++ clang clang-format
23+
sudo apt-get install -y build-essential g++-14 clang clang-format
2424
2525
# 3. Clang-format check
2626
- name: Clang-format Check

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Examples include (and will expand to):
3030
* [integer-factorization](./integer-factorization/)
3131
* Encoding
3232
* [rot47](./rot47/)
33+
* Data Structures
34+
* [map-with-unknown-key](./map-with-unknown-key/)
3335
* OOP
3436
* [virtual-interface](./virtual-interface/)
3537
* [oop-with-exception](./oop-with-exception/)
@@ -63,6 +65,12 @@ cpp-coding-exercise/
6365

6466
## Building
6567

68+
### C++ Version
69+
```bash
70+
sudo apt update
71+
sudo apt install g++-14
72+
```
73+
6674
### Build everything
6775

6876
```bash

common.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ endif
2626
# --------------------------
2727
# Compiler / Linker flags
2828
# --------------------------
29+
CXX := g++-14
2930
CXXFLAGS := -std=c++23 -Wall -Wextra -Werror $(OPTFLAGS) $(SAN_FLAGS)
3031
LDFLAGS := $(SAN_FLAGS)
3132

map-with-unknown-key/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# pull in shared compiler settings
2+
include ../common.mk
3+
4+
# per-example flags
5+
# CXXFLAGS += -pthread
6+
7+
## get it from the folder name
8+
TARGET := $(notdir $(CURDIR))
9+
## all *.cpp files in this folder
10+
SRCS := $(wildcard *.cpp)
11+
OBJS := $(SRCS:.cpp=.o)
12+
13+
all: $(TARGET)
14+
15+
$(TARGET): $(OBJS)
16+
$(CXX) $(CXXFLAGS) -o $@ $^
17+
18+
%.o: %.cpp
19+
$(CXX) $(CXXFLAGS) -c $< -o $@
20+
21+
run: $(TARGET)
22+
./$(TARGET) $(ARGS)
23+
24+
clean:
25+
rm -f $(OBJS) $(TARGET)
26+
27+
# Delegates to top-level Makefile
28+
check-format:
29+
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)
30+
31+
.PHONY: all clean run check-format

map-with-unknown-key/main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
What happens if you access an unknown key in the std::map, std::unordered_map?
3+
*/
4+
#include <iostream>
5+
#include <map>
6+
#include <string>
7+
#include <format>
8+
#include <print>
9+
10+
int
11+
main()
12+
{
13+
std::map<std::string, int> my_map;
14+
std::print("Accessing unknown key 'unknown_key' in std::map:\n");
15+
int value = my_map["unknown_key"];
16+
std::print("Value: {}\n", value); // Should print 0, as default-constructed int is 0
17+
std::print("Map size after access: {}\n", my_map.size()); // Should print 1
18+
return 0;
19+
}

map-with-unknown-key/tests.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
5+
./map-with-unknown-key
-52.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)