-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add std::pair and std::tuple examples
- Loading branch information
George K. Thiruvathukal
committed
Dec 3, 2023
1 parent
f541160
commit 69ba0d0
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <iostream> | ||
#include <utility> | ||
|
||
int main() { | ||
// Creating a pair of int and std::string | ||
auto myPair = std::make_pair(1, "Hello"); | ||
|
||
// Accessing elements | ||
std::cout << "First: " << myPair.first << ", Second: " << myPair.second << std::endl; | ||
|
||
// Modifying elements | ||
myPair.first = 2; | ||
myPair.second = "World"; | ||
|
||
std::cout << "First: " << myPair.first << ", Second: " << myPair.second << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <iostream> | ||
#include <tuple> | ||
#include <string> | ||
|
||
int main() { | ||
// Creating a tuple of int, std::string, and float | ||
auto myTuple = std::make_tuple(1, "Hello", 3.14f); | ||
|
||
// Accessing elements using std::get | ||
std::cout << "First: " << std::get<0>(myTuple) | ||
<< ", Second: " << std::get<1>(myTuple) | ||
<< ", Third: " << std::get<2>(myTuple) << std::endl; | ||
|
||
// Modifying elements | ||
std::get<0>(myTuple) = 2; | ||
std::get<1>(myTuple) = "World"; | ||
std::get<2>(myTuple) = 1.59f; | ||
|
||
std::cout << "First: " << std::get<0>(myTuple) | ||
<< ", Second: " << std::get<1>(myTuple) | ||
<< ", Third: " << std::get<2>(myTuple) << std::endl; | ||
|
||
return 0; | ||
} |