蓝桥ROS机器人之现代C++学习笔记4.3 元组
【摘要】
学习程序如下:
#include <tuple>#include <iostream>#include <variant> auto get_student(int id){ if (id == 0) return std::make_tuple(3.8, 'A', "John"...
学习程序如下:
-
#include <tuple>
-
#include <iostream>
-
#include <variant>
-
-
auto get_student(int id)
-
{
-
if (id == 0)
-
return std::make_tuple(3.8, 'A', "John");
-
if (id == 1)
-
return std::make_tuple(2.9, 'C', "Jack");
-
if (id == 2)
-
return std::make_tuple(1.7, 'D', "Ive");
-
// return type is std::tuple<double, char, std::string>
-
return std::make_tuple(0.0, 'D', "null");
-
}
-
-
template <size_t n, typename... T>
-
constexpr std::variant<T...> _tuple_index(const std::tuple<T...>& tpl, size_t i) {
-
if constexpr (n >= sizeof...(T))
-
throw std::out_of_range("out of range.");
-
if (i == n)
-
return std::variant<T...>{ std::in_place_index<n>, std::get<n>(tpl) };
-
return _tuple_index<(n < sizeof...(T)-1 ? n+1 : 0)>(tpl, i);
-
}
-
template <typename... T>
-
constexpr std::variant<T...> tuple_index(const std::tuple<T...>& tpl, size_t i) {
-
return _tuple_index<0>(tpl, i);
-
}
-
-
template <typename T>
-
auto tuple_len(T &tpl) {
-
return std::tuple_size<T>::value;
-
}
-
-
template <typename T0, typename ... Ts>
-
std::ostream & operator<< (std::ostream & s, std::variant<T0, Ts...> const & v) {
-
std::visit([&](auto && x){ s << x;}, v);
-
return s;
-
}
-
-
int main()
-
{
-
auto student = get_student(0);
-
std::cout << "ID: 0, "
-
<< "GPA: " << std::get<0>(student) << ", "
-
<< "Grade: " << std::get<1>(student) << ", "
-
<< "Name: " << std::get<2>(student) << '\n';
-
-
double gpa;
-
char grade;
-
std::string name;
-
-
// tuple unpack
-
std::tie(gpa, grade, name) = get_student(1);
-
std::cout << "ID: 1, "
-
<< "GPA: " << gpa << ", "
-
<< "Grade: " << grade << ", "
-
<< "Name: " << name << '\n';
-
-
-
std::tuple<std::string, double, double, int> t("123", 4.5, 6.7, 8);
-
std::cout << std::get<std::string>(t) << std::endl;
-
// std::cout << std::get<double>(t) << std::endl; // illegal, runtime error
-
std::cout << std::get<3>(t) << std::endl;
-
-
// concat
-
auto new_tuple = std::tuple_cat(get_student(1), std::move(t));
-
-
// iteration
-
for(int i = 0; i != tuple_len(new_tuple); ++i) {
-
std::cout << tuple_index(new_tuple, i) << std::endl; // runtime indexing
-
}
-
}
文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。
原文链接:zhangrelay.blog.csdn.net/article/details/124017497
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)