File tree Expand file tree Collapse file tree 7 files changed +65
-1
lines changed
Expand file tree Collapse file tree 7 files changed +65
-1
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 2626# --------------------------
2727# Compiler / Linker flags
2828# --------------------------
29+ CXX := g++-14
2930CXXFLAGS := -std=c++23 -Wall -Wextra -Werror $(OPTFLAGS ) $(SAN_FLAGS )
3031LDFLAGS := $(SAN_FLAGS )
3132
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+
3+ set -ex
4+
5+ ./map-with-unknown-key
You can’t perform that action at this time.
0 commit comments