Skip to content

Commit

Permalink
add std::pair and std::tuple examples
Browse files Browse the repository at this point in the history
  • Loading branch information
George K. Thiruvathukal committed Dec 3, 2023
1 parent f541160 commit 69ba0d0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modern-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ set(MY_APP_NAMES
lambda-with-closures-byval
lambda-with-closures-byval-all
lambda-with-return-type
std-pair
std-tuple
)

foreach(APP_NAME IN LISTS MY_APP_NAMES)
Expand Down
18 changes: 18 additions & 0 deletions modern-cpp/std-pair.cpp
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;
}
24 changes: 24 additions & 0 deletions modern-cpp/std-tuple.cpp
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;
}

0 comments on commit 69ba0d0

Please sign in to comment.