Skip to content

Commit

Permalink
Added examples of std::format
Browse files Browse the repository at this point in the history
  • Loading branch information
lformaggia committed Jan 1, 2025
1 parent 403d2fe commit cd119e9
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Examples/src/Streams/format/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
############################################################
#
# An example of Makefile for the course on
# Advanced Programming for Scientific Computing
# It should be modified for adapting it to the various examples
#
############################################################
#
# The environmental variable PACS_ROOT should be set to the
# root directory where the examples reside. In practice, the directory
# where this file is found. The resolution of PACS_ROOT is made in the
# Makefile.h file, where other important variables are also set.
# The only user defined variable that must be set in this file is
# the one indicating where Makefile.h resides

MAKEFILEH_DIR=../../../
#
include $(MAKEFILEH_DIR)/Makefile.inc
#
# You may have an include file also in the current directory
#
-include Makefile.inc

#
# The general setting is as follows:
# mains are identified bt main_XX.cpp
# all other files are XX.cpp
#

# get all files *.cpp
SRCS=$(wildcard *.cpp)
# get the corresponding object file
OBJS = $(SRCS:.cpp=.o)
# get all headers in the working directory
HEADERS=$(wildcard *.hpp)
#
exe_sources=$(filter main%.cpp,$(SRCS))
EXEC=$(exe_sources:.cpp=)

#========================== ORA LA DEFINIZIONE DEGLI OBIETTIVI
.phony= all clean distclean doc

.DEFAULT_GOAL = all

all: $(DEPEND) $(EXEC)

clean:
$(RM) -f $(EXEC) $(OBJS)

distclean:
$(MAKE) clean
$(RM) -f ./doc $(DEPEND)
$(RM) *.out *.bak *~

doc:
doxygen $(DOXYFILE)

$(EXEC): $(OBJS)

$(OBJS): $(SRCS)

$(DEPEND): $(SRCS)
$(RM) $(DEPEND)
for f in $(SRCS); do \
$(CXX) $(STDFLAGS) $(CPPFLAGS) -MM $$f >> $(DEPEND); \
done

-include $(DEPEND)




18 changes: 18 additions & 0 deletions Examples/src/Streams/format/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# The std::format utility
The `std::format` utility is a new feature in C++20 that allows you to format strings in a more readable and maintainable way. It is similar to `printf` but with a more modern syntax.

The `std::format` function is defined in the `<format>` header file. It takes a format string and a list of arguments, and returns a formatted string.

## Basic usage
Here is a simple example of using `std::format`:

```cpp
#include <format>
#include <iostream>
std::string message = std::format("Hello, {}!", "world");
std::cout << message << std::endl;
```
In the given file you find more significant examples of the `std::format` utility. This utility makes writing formatted output much easier and more readable. Moreover, comparing to C-style formatting, it is type-safe and more secure.

# What do I learn here?
You will learn how to use the `std::format` utility to format strings in C++20. More details may be found in the [C++ reference](https://en.cppreference.com/w/cpp/utility/format/format).
37 changes: 37 additions & 0 deletions Examples/src/Streams/format/main_format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <format>
#include <iostream>
#include <numbers>
int
main()
{
std::string name = "John";
int age = 30;
double height = 1.75;

auto formattedString =
std::format("Name: {}, Age: {}, Height: {:.2f}", name, age, height);
std::cout << formattedString << std::endl;
formattedString =
std::format("Centered: {:^}, LeftJustified: {:<d}, RightJustified: {:>.2f}",
name, age, height);
std::cout << formattedString << std::endl;
formattedString =
std::format("With a space of 8 and - as filling\nCentered: {:-^8}, "
"LeftJustified: {:-<8d}, RightJustified: {:->8.2f}",
name, age, height);
std::cout << formattedString << std::endl;
std::cout << std::format("42 in binary format: {:b}", 42) << std::endl;
std::cout << std::format("42 in hex format: {:x}", 42) << std::endl;
std::cout << std::format("Maybe you need to print {{}} before printing {:d}",
42)
<< std::endl;
// different precisiones
std::cout << std::format("Pi with 2 decimal places: {:.2f}", std::numbers::pi)
<< std::endl;
std::cout << std::format("Pi with 4 decimal places: {:.4f}", std::numbers::pi)
<< std::endl;
std::cout << std::format("Pi with 15 decimal places: {:.15f}",
std::numbers::pi)
<< std::endl;
return 0;
}

0 comments on commit cd119e9

Please sign in to comment.