Skip to content

Commit

Permalink
ups
Browse files Browse the repository at this point in the history
  • Loading branch information
behnamasadi committed Nov 30, 2023
1 parent 1463533 commit a3e220d
Show file tree
Hide file tree
Showing 11 changed files with 394 additions and 223 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "CMake_Tutorials/project/extern/googletest"]
path = CMake_Tutorials/project/extern/googletest
url = https://github.com/google/googletest.git
[submodule "vcpkg"]
path = vcpkg
url = https://github.com/microsoft/vcpkg
30 changes: 11 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,25 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_library(add SHARED src/add.cpp)
add_executable(loadeding_libraries src/loadeding_libraries.cpp)
target_link_libraries(loadeding_libraries dl)

add_executable(align src/align.cpp)

add_executable(signals src/signals.cpp)
add_executable(system_call src/system_call.cpp)

# add_executable(date_time src/date_time.cpp)

add_executable(fork src/fork.cpp)

endif()

add_executable(template src/template.cpp)


ADD_DEFINITIONS(-DLOGING=0)
add_definitions(-DLOGING=0)
add_executable(macro src/macro.cpp)

add_executable(inline_functions src/inline_functions.cpp)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_executable(align src/align.cpp)
endif()

add_executable(algorithms_library src/algorithms_library.cpp)

add_executable(vector src/vector.cpp)
Expand Down Expand Up @@ -134,12 +139,6 @@ add_executable(switch_case src/switch_case.cpp)

add_executable(set_map_pair_tuple src/set_map_pair_tuple.cpp)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_executable(signals src/signals.cpp)

add_executable(system_call src/system_call.cpp)
endif()

add_executable(error_handling src/error_handling.cpp)

add_executable(bitwise_operations src/bitwise_operations.cpp)
Expand Down Expand Up @@ -232,18 +231,12 @@ add_executable(lambda src/lambda.cpp)

add_executable(unions src/unions.cpp)

# add_executable(date_time src/date_time.cpp)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_executable(fork src/fork.cpp)
endif()

add_executable(basic_IO_operation_streams src/basic_IO_operation_filesystem_streams_reading_writing_files_formating_output_cin_cout_scanf_printf_gets_puts_getline.cpp)

add_executable(explicit_constructor src/class/explicit_constructor.cpp)

add_executable(const_constexpr_mutable src/const_constexpr_mutable.cpp)


add_executable(literals src/literals.cpp)

add_executable(ternary src/ternary.cpp)
Expand Down Expand Up @@ -334,7 +327,6 @@ target_link_libraries(asynchronous_programming -pthread)

add_executable(circular_dependency src/class/circular_dependency/circular_dependency.cpp src/class/circular_dependency/classA.cpp src/class/circular_dependency/classB.cpp)


add_executable(core_dump src/core_dump.cpp)


Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ read more [here](https://ros-developer.com/2017/11/08/docker/)
## [C++ Tutorials](#)
* [Align](docs/align.md)
* [Allocator](docs/allocator.md)
* [Algorithms library](src/algorithms_library.cpp)
* [Algorithms library](docs/algorithms.md)
* [Assert](docs/assert.md)
* [Atomic operations and Atomic Types](docs/atomic.md)
* [Asynchronous programming](docs/asynchronous_programming.md)
Expand Down
173 changes: 173 additions & 0 deletions docs/algorithm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# algorithm
The `<algorithm>` header in C++20 includes a wide range of functions that are essential for various operations on containers like arrays, lists, and vectors. Some of the key components and functions included in this header are:

1. **Non-modifying Sequence Operations:**
- `std::for_each`: Apply a function to a range of elements.
- `std::count` and `std::count_if`: Count elements in a range.
- `std::find`, `std::find_if`, and `std::find_if_not`: Find elements in a range.
- `std::search`: Search for a subsequence in a range.

2. **Modifying Sequence Operations:**
- `std::copy`, `std::copy_if`, `std::copy_n`, and `std::copy_backward`: Copy elements.
- `std::move` and `std::move_backward`: Move elements.
- `std::fill` and `std::fill_n`: Assign a value to a range of elements.
- `std::transform`: Apply a function and store the result.
- `std::replace`, `std::replace_if`: Replace values in a range.
- `std::swap_ranges`: Swap ranges of elements.

3. **Partitioning Operations:**
- `std::partition`: Reorder elements in a range so that they satisfy a predicate.
- `std::stable_partition`: Stable version of `std::partition`.
- `std::partition_copy`: Copy elements that satisfy a predicate into another range.
- `std::partition_point`: Find the partition point.

4. **Sorting Operations:**
- `std::sort`: Sort elements in a range.
- `std::stable_sort`: Stable sort elements.
- `std::partial_sort`: Partially sort elements in a range.
- `std::nth_element`: Partially sort such that the nth element is in its sorted position.

5. **Binary Search Operations (on sorted ranges):**
- `std::lower_bound` and `std::upper_bound`: Find bounds in a sorted range.
- `std::binary_search`: Test if a value exists in a sorted sequence.
- `std::equal_range`: Get the range of equal elements.

6. **Set Operations (on sorted ranges):**
- `std::merge`: Merge two sorted ranges.
- `std::set_union`, `std::set_intersection`, `std::set_difference`, `std::set_symmetric_difference`: Perform set operations.

7. **Heap Operations:**
- `std::make_heap`, `std::push_heap`, `std::pop_heap`, `std::sort_heap`: Functions for managing heaps.

8. **Min/Max Operations:**
- `std::min`, `std::max`, `std::minmax`: Find minimum, maximum, and both.
- `std::min_element`, `std::max_element`, `std::minmax_element`: Find elements.

9. **Comparison Operations:**
- `std::lexicographical_compare`: Compare sequences.
- `std::equal`: Test if two ranges are equal.

10. **Permutation Operations:**
- `std::next_permutation`, `std::prev_permutation`: Generate permutations.

11. **Numeric Operations:**
- `std::iota`: Fill a range with successive increments of a start value.
- `std::accumulate`: Sum up elements.
- `std::inner_product`: Compute the inner product of two ranges.
- `std::adjacent_difference`: Compute the difference between adjacent elements.
- `std::reduce`, `std::transform_reduce`, `std::inclusive_scan`, `std::exclusive_scan`: Parallel versions of numeric operations (C++17 and above).

12. **Other Operations:**
- `std::ranges::sort`, `std::ranges::copy`: Range versions of algorithm functions (C++20).


## std::ranges::transform
`std::ranges::transform` is a function in C++20 that applies a given function to each element in a range and stores the result in another range. It's part of the Ranges library, which provides a more modern and flexible way to work with sequences of data.

Here's a basic explanation and some examples:

### Explanation
- **Function**: `std::ranges::transform(InputRange, OutputIterator, UnaryOperation)`
- **Parameters**:
- **InputRange**: The range of elements to transform.
- **OutputIterator**: Where to store the results of the transformation.
- **UnaryOperation**: A function or function object that will be applied to each element in the input range.

### Example 1: Simple Transformation
```cpp
#include <iostream>
#include <vector>
#include <ranges>

int main() {
std::vector<int> input = {1, 2, 3, 4};
std::vector<int> output(input.size());

std::ranges::transform(input, output.begin(), [](int x) { return x * x; });

for (int val : output) {
std::cout << val << ' ';
}
}
```
In this example, each element of `input` is squared and stored in `output`.

### Example 2: Using Different Input and Output Types
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <ranges>

int main() {
std::vector<std::string> input = {"Hello", "World"};
std::vector<size_t> output(input.size());

std::ranges::transform(input, output.begin(), [](const std::string& s) { return s.size(); });

for (auto val : output) {
std::cout << val << ' ';
}
}
```
Here, `std::ranges::transform` calculates the length of each string in `input` and stores it in `output`.

### Example 3: In-Place Transformation
```cpp
#include <iostream>
#include <vector>
#include <ranges>

int main() {
std::vector<int> data = {1, 2, 3, 4};

std::ranges::transform(data, data.begin(), [](int x) { return x + 10; });

for (int val : data) {
std::cout << val << ' ';
}
}
```
In this example, each element in `data` is increased by 10. The transformation is done in-place.

### Notes
- `std::ranges::transform` is a more flexible and "range-aware" version of the traditional `std::transform`.
- It works well with the new range-based for loops and other range utilities in C++20.


### std::back_inserter
`std::back_inserter` is a convenient way to append elements to a container that supports push back operations, such as `std::vector`, `std::list`, etc. When used with `std::ranges::transform`, it allows you to transform elements from one range and automatically append the results to another container.

Here's an example demonstrating how you can use `std::ranges::transform` with `std::back_inserter`:

### Example: Transform with Back Inserter

```cpp
#include <iostream>
#include <vector>
#include <ranges>
#include <iterator> // For std::back_inserter

int main() {
std::vector<int> input = {1, 2, 3, 4};
std::vector<int> output; // Notice, we don't need to pre-size the output vector

// Apply transformation and append results to 'output'
std::ranges::transform(input, std::back_inserter(output), [](int x) { return x * 2; });

// Print the transformed output
for (int val : output) {
std::cout << val << ' ';
}
}
```

### Explanation:
- **`std::vector<int> output;`**: Initially, `output` is an empty vector.
- **`std::ranges::transform(..., std::back_inserter(output), ...);`**: For each element in `input`, the lambda function is applied, and the result is appended to the end of `output`.
- The lambda function here simply multiplies each element by 2.
- **Printing the Results**: The transformed elements in `output` are then printed.

This approach is particularly useful when you don't know the size of the output container in advance or when you want to append the transformed elements to an existing container.

[code](../src/algorithms_library.cpp)
3 changes: 3 additions & 0 deletions docs/const_constexpr_mutable.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,10 @@ problem solved:
```cpp
auto f1 = [=]() mutable {x = 42;};
```
When a variable is captured by value in a lambda expression in C++, a copy of the variable is made and used within the lambda. This copy is independent of the original variable. By default, this copy is `const`, meaning it cannot be modified within the lambda.
However, when you use the `mutable` keyword with a lambda, it allows this copied variable to be modified within the lambda. It's important to understand that this modification affects only the lambda's internal copy of the variable, not the original variable outside the lambda. The original variable remains unchanged regardless of any modifications made to the copy within the lambda.
Refs: [1](https://www.youtube.com/watch?v=bP9z3H3cVMY), [2](https://stackoverflow.com/questions/105014/does-the-mutable-keyword-have-any-purpose-other-than-allowing-the-variable-to)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ or
S(const S& rhs){std::cout<<"copy constructor" <<std::endl;}
```
or for instance here, you create a `std::unique_ptr<std::vector<int>>` that points to a new `std::vector<int>` which is constructed using the copy constructor of `std::vector<int>`. This means you are copying `localVector` into a new `std::vector<int>` that is managed by the `std::unique_ptr`.
```cpp
std::unique_ptr<std::vector<int>> foo() {
std::vector<int> localVector;
localVector.push_back(2);
localVector.push_back(4);
localVector.push_back(5);
return std::make_unique<std::vector<int>>(localVector);
}
```


# Copy Assignment Operator
it happens at `s2 = s1;`

Expand Down
Loading

0 comments on commit a3e220d

Please sign in to comment.