Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpp-cheat/cpp/tuple.cpp at master · Bhupesh19/cpp-cheat · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Bhupesh19
cpp-cheat
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
cpp-cheat
/
cpp
/
tuple.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
151 lines (114 loc) · 3.85 KB
master
Breadcrumbs
cpp-cheat
/
cpp
/
tuple.cpp
Copy path
Top
File metadata and controls
Code
Blame
151 lines (114 loc) · 3.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
# tuple
Hold a ordered collection of elements.
Each element can be of a different type.
The length is always fixed.
*/
#
include
"
common.hpp
"
int
main
() {
#
if
__cplusplus >= 201103L
//
Create
{
//
Constructor
{
std::tuple<
int
,
char
,std::string>
t0
(
0
,
'
a
'
,
"
a
"
);
}
/*
# make_tuple
forwards arguments to tuple constructor.
The advantage over the constructor is that since it is a function
template argument deduction can be done, so we don't need to type in
template arguments.
Remember that template argument deduction cannot be done for constructors.
*/
{
std::tuple<
int
,
char
,std::string> t;
//
without make_tuple
t =
std::make_tuple
(
0
,
'
a
'
,
"
a
"
);
t = std::tuple<
int
,
char
,std::string>(
0
,
'
a
'
,
"
a
"
);
//
with make_tuple
}
//
Tuple from pair.
{
std::tuple<
int
,
char
>
t2
( std::pair<
int
,
char
>(
0
,
'
a
'
));
}
//
Uniform initialization.
{
std::tuple<
int
,
char
,std::string> t{
0
,
'
a
'
,
"
a
"
};
}
//
Fails because the tuple constructor are is `explicit`!
//
TODO Rationale? http://stackoverflow.com/questions/14961809/returning-a-tuple-from-a-function-using-uniform-initialization-syntax
{
//
std::tuple<int, int> t = {0, 1};
//
std::tuple<int, int> t[]{{0, 1}};
}
}
/*
# get
Get single element from tuple.
Returns references, so it is possible to modify the tuples with them.
Copies are made from input elements
*/
{
std::tuple<
int
,std::string>
t
(
0
,
"
abc
"
);
assert
(std::get<
0
>(t) ==
0
);
assert
(std::get<
1
>(t) ==
"
abc
"
);
//
Can also be used to set tuple values.
std::get<
0
>(t) =
1
;
assert
(std::get<
0
>(t) ==
1
);
//
Or you can modify contained objects.
std::get<
1
>(t)[
0
] =
'
0
'
;
assert
(std::get<
1
>(t) ==
"
0bc
"
);
}
/*
# tie
Unpack a tuple.
Unpack by reference seems not to be possible: <http://stackoverflow.com/questions/16571883/unpacking-a-std-tuple-into-pointers>
# ignore
Magic that exists only to ignore one of tie outputs.
*/
{
int
i;
std::string s;
std::tuple<
int
,
float
,std::string>
t
(
1
,
1.5
,
"
abc
"
);
std::tie
(i, std::ignore, s) = t;
assert
(i ==
1
);
assert
(s ==
"
abc
"
);
//
Clearly copies are made.
i =
2
;
assert
(std::get<
0
>(t) ==
1
);
//
Tie and declare variables at the same time. Nope?
//
Tie saves assignments, but repeats types which we can get rid of with auto.
//
If only we could save both types and assignments...
//
http://stackoverflow.com/questions/29312154/how-to-create-a-new-variable-and-use-stdtie-at-the-same-time
//
http://codereview.stackexchange.com/questions/61499/a-tuple-unpacking-macro-that-approximates-python-elegance
}
/*
Relational operators operations are implemented
http://www.cplusplus.com/reference/tuple/tuple/operators/>
`<` family is lexicographical.
*/
{
std::tuple<
int
,
char
>
t0
(
0
,
'
a
'
);
std::tuple<
int
,
char
>
t1
(
0
,
'
a
'
);
std::tuple<
int
,
char
>
t2
(
1
,
'
b
'
);
std::tuple<
int
,
char
>
t3
(-
1
,
'
b
'
);
std::tuple<
int
,
char
>
t4
(
0
,
'
b
'
);
assert
(t0 == t1);
assert
(t0 != t2);
assert
(t0 < t2);
assert
(t0 > t3);
//
-1 counts
assert
(t0 < t4);
//
0 ties, 'a' < 'b'
}
//
Swap contents of two tuples of same type
{
std::tuple<
int
,
char
>
t0
(
0
,
'
a
'
);
std::tuple<
int
,
char
>
t1
(
1
,
'
b
'
);
std::tuple<
int
,
char
> old_t0 = t0;
std::tuple<
int
,
char
> old_t1 = t1;
t0.
swap
(t1);
assert
(t0 == old_t1);
assert
(t1 == old_t0);
}
#
endif
}
You can’t perform that action at this time.