Skip to content

Commit 14a7ac8

Browse files
committed
pre post ++ -- added, pair updated
1 parent ba96ddb commit 14a7ac8

File tree

4 files changed

+193
-2
lines changed

4 files changed

+193
-2
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ install:
3232
before_build:
3333
- mkdir build
3434
- cd build
35-
- cmake -G "Ninja" -A x64 ..
35+
- cmake -G "Ninja" ..
3636

3737
build_script:
3838
- cmake --build . --config %CONFIGURATION%

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [C++](#)
22

33
<!---[![Build Status](https://app.travis-ci.com/behnamasadi/cpp_tutorials.svg?branch=master)](https://app.travis-ci.com/behnamasadi/cpp_tutorials)-->
4-
[![Build status](https://ci.appveyor.com/api/projects/status/i4omlfovqaswpj41/branch/master?svg=true)](https://ci.appveyor.com/project/behnamasadi/cpp-tutorials/branch/master)
4+
<!---[![Build status](https://ci.appveyor.com/api/projects/status/i4omlfovqaswpj41/branch/master?svg=true)](https://ci.appveyor.com/project/behnamasadi/cpp-tutorials/branch/master)-->
55
![build workflow](https://github.com/behnamasadi/cpp_tutorials/actions/workflows/docker-build.yml/badge.svg)
66
![alt text](https://img.shields.io/badge/license-BSD-blue.svg)
77

@@ -194,6 +194,7 @@ This change ensures that VSCode uses the "Ninja Multi-Config" generator by defau
194194
* [Numeral Systems in C++ Decimal, Binary, Octal, Hexadecimal](docs/numeral_system.md)
195195
* [Optional](docs/optional.md)
196196
* [Parameter Pack Expansion ...](docs/parameter_pack_expansion_(...).md)
197+
* [Post-increment, Pre-increment, Unary plus](docs/post-increment_pre-increment.md)
197198
* [Packaged Task](docs/packaged_task.md)
198199
* [Register Keyword](docs/register.md)
199200
* [Regex](docs/regex.md)

docs/post-increment_pre-increment.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## Post-increment, Pre-increment
2+
3+
**Understanding num++ and +num:**
4+
5+
- **num++ (Post-increment):** Increments the value of `num` after the expression is evaluated.
6+
- **+num (Unary plus):** Returns the value of `num` without modifying it.
7+
8+
**Cases where num++ and +num differ and cause different output:**
9+
10+
1. **In expressions involving assignment:**
11+
- **Example:**
12+
```c++
13+
int num = 5;
14+
int result1 = num++; // result1 will be 5, num will be 6
15+
int result2 = +num; // result2 will be 6, num will remain 6
16+
```
17+
- **Explanation:**
18+
- In the first case, `result1` gets the original value of `num` (5), and then `num` is incremented to 6.
19+
- In the second case, `result2` simply gets the current value of `num`, which is 6, without modifying it.
20+
21+
2. **In function calls as arguments:**
22+
- **Example:**
23+
```c++
24+
void printValue(int value) {
25+
cout << value << endl;
26+
}
27+
28+
int num = 5;
29+
printValue(num++); // Will print 5
30+
printValue(+num); // Will print 6
31+
```
32+
- **Explanation:**
33+
- In the first case, the value of `num` (5) is passed to the function before it's incremented.
34+
- In the second case, the value of `num` (6) is passed to the function directly.
35+
36+
3. **In expressions involving logical operators:**
37+
- **Example:**
38+
```c++
39+
int num = 0;
40+
bool result1 = num++ && false; // result1 will be false
41+
bool result2 = +num && false; // result2 will be false
42+
```
43+
- **Explanation:**
44+
- In the first case, `num++` evaluates to 0, which is considered false in a logical context. Since the first operand is false, the entire expression is false.
45+
- In the second case, `+num` evaluates to 0, which is still false. However, the evaluation of the second operand (false) is not necessary due to short-circuiting.
46+
47+
4. **In expressions involving the comma operator:**
48+
- **Example:**
49+
```c++
50+
int num = 5;
51+
int result1 = num++, num++; // result1 will be 6
52+
int result2 = +num, +num; // result2 will be 6
53+
```
54+
- **Explanation:**
55+
- In the first case, the first `num++` evaluates to 5, and then the second `num++` evaluates to 6. The result of the comma expression is the value of the second operand, which is 6.
56+
- In the second case, both `+num` expressions evaluate to 6, but the result of the comma expression is still the value of the second operand, which is 6.
57+
58+
**Key points to remember:**
59+
60+
- The difference between `num++` and `+num` lies in when the increment occurs.
61+
- `num++` increments the value after the expression is evaluated, while `+num` does not modify the value.
62+
- The choice between `num++` and `+num` depends on the desired behavior in the specific context.
63+
64+
By understanding these cases and their implications, you can effectively use `num++` and `+num` in your C++ code to achieve the intended results.
65+

docs/set_map_pair_tuple.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,131 @@ courses.find(c2)->m_name;
584584

585585
These examples demonstrate how `std::unordered_map` and `std::unordered_set` can be applied to solve practical problems efficiently, leveraging their strengths in situations where speed and unique data management are critical.
586586

587+
588+
589+
In C++, `std::tie`, `std::pair`, and `std::tuple` are useful features provided by the Standard Library that allow you to handle multiple values together in a convenient and structured way. Here's a breakdown of each and how they can be used in real-world applications:
590+
591+
### 1. `std::pair`
592+
A `std::pair` is a simple container that holds two values, which may be of different types. It is often used when you want to return two values from a function or associate two related values together.
593+
594+
#### **Example: Using `std::pair` in a real-world application**
595+
596+
Imagine you are building a function that returns the minimum and maximum values from an array of integers. You can use `std::pair` to return both values together:
597+
598+
```cpp
599+
#include <iostream>
600+
#include <vector>
601+
#include <utility> // for std::pair
602+
603+
std::pair<int, int> findMinMax(const std::vector<int>& numbers) {
604+
int min = numbers[0];
605+
int max = numbers[0];
606+
607+
for (int number : numbers) {
608+
if (number < min) min = number;
609+
if (number > max) max = number;
610+
}
611+
612+
return std::make_pair(min, max); // Returning a pair of min and max
613+
}
614+
615+
int main() {
616+
std::vector<int> numbers = {3, 5, 1, 9, 2, 8, -1, 6};
617+
std::pair<int, int> minMax = findMinMax(numbers);
618+
619+
std::cout << "Min: " << minMax.first << ", Max: " << minMax.second << std::endl;
620+
621+
return 0;
622+
}
623+
```
624+
625+
### 2. `std::tuple`
626+
A `std::tuple` is like an extension of `std::pair` that can hold more than two values of potentially different types. It is useful when you need to return multiple values from a function or group related values together.
627+
628+
#### **Example: Using `std::tuple` in a real-world application**
629+
630+
Suppose you're writing a program to process student records, and you want a function that returns a student's name, age, and GPA. You can use `std::tuple` to return all three values together.
631+
632+
```cpp
633+
#include <iostream>
634+
#include <tuple>
635+
#include <string>
636+
637+
std::tuple<std::string, int, double> getStudentInfo() {
638+
std::string name = "John Doe";
639+
int age = 20;
640+
double gpa = 3.75;
641+
642+
return std::make_tuple(name, age, gpa); // Returning a tuple of name, age, and GPA
643+
}
644+
645+
int main() {
646+
auto studentInfo = getStudentInfo();
647+
648+
std::string name;
649+
int age;
650+
double gpa;
651+
652+
// Unpacking the tuple using std::tie
653+
std::tie(name, age, gpa) = studentInfo;
654+
655+
std::cout << "Name: " << name << ", Age: " << age << ", GPA: " << gpa << std::endl;
656+
657+
return 0;
658+
}
659+
```
660+
661+
### 3. `std::tie`
662+
`std::tie` is used to unpack values from a `std::pair` or `std::tuple` into individual variables. This is especially handy when you don't want to deal with the tuple's index-based access.
663+
664+
#### **Example: Using `std::tie` to unpack values**
665+
666+
In the example above, `std::tie` is used to unpack the tuple into individual variables (`name`, `age`, and `gpa`). This makes the code more readable and convenient.
667+
668+
Another example could be unpacking the return value of a function that returns a `std::pair`:
669+
670+
```cpp
671+
#include <iostream>
672+
#include <vector>
673+
#include <tuple>
674+
#include <string>
675+
676+
std::tuple<std::string, int, double> analyzeText(const std::string& text) {
677+
int wordCount = 0;
678+
int charCount = text.length();
679+
double averageWordLength = charCount;
680+
681+
// Simple analysis: let's say each word is roughly 5 characters
682+
wordCount = charCount / 5;
683+
averageWordLength = charCount / static_cast<double>(wordCount);
684+
685+
return std::make_tuple(text, wordCount, averageWordLength);
686+
}
687+
688+
int main() {
689+
std::string text = "This is a simple example sentence.";
690+
std::string analyzedText;
691+
int wordCount;
692+
double avgWordLength;
693+
694+
// Unpacking tuple returned from analyzeText
695+
std::tie(analyzedText, wordCount, avgWordLength) = analyzeText(text);
696+
697+
std::cout << "Text: " << analyzedText << "\nWord Count: " << wordCount
698+
<< "\nAverage Word Length: " << avgWordLength << std::endl;
699+
700+
return 0;
701+
}
702+
```
703+
704+
### Summary
705+
706+
- **`std::pair`**: Useful for storing two related values, often used for returning two values from a function.
707+
- **`std::tuple`**: Extends `std::pair` by allowing more than two values of different types to be stored together.
708+
- **`std::tie`**: Simplifies unpacking `std::pair` or `std::tuple` into individual variables, improving code readability.
709+
710+
711+
587712
588713
# tie
589714
The work of `tie()` is to unpack the tuple values into seperate variables. There are two variants of `tie()`, with and without “ignore” ,

0 commit comments

Comments
 (0)