零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门
零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C++ 面向对象
零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C++ 设计模式
零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C++ STL
零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C/C++ 技术杂谈
零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C/C++ 常用函数
一.error C2027: 使用了未定义类型“std::tuple”
C++ 中使用 std::tuple 需要包含头文件 <tuple>,如下:
#include <tuple>
二.std::tuple 简介
std::tuple 是类似 pair 的模板。每个 pair 的成员类型都不相同,但每个 pair 都恰好有两个成员;std::tuple 成员类型不同,有任意数量的成员。
当我们创建一个 std::tuple 对象时,可以使用 tuple 的默认构造函数,它会对每个成员进行值初始化;也可以为每个成员提供一个初始值,此时的构造函数是 explicit 的,因此必须使用直接初始化方法。类似 make_pair 函数,标准库定义了 make_tuple 函数;
std::tuple 的关系和相等运算符的行为类似容器的对应操作。这些运算符逐对比较左侧 tuple 和右侧 tuple 的成员。只有两个 tuple 具有相同数量的成员时,我们才可以比较它们。而且,为了使用 tuple 的相等或不等运算符,对每对成员使用==运算符必须都是合法的;为了使用关系运算符,对每对成员使用<必须都是合法的。由于 tuple 定义了 < 和 == 运算符,我们可以将 tuple 序列传递给算法,并且可以在无序容器中将 tuple 作为关键字类型。
std::tuple 中元素是被紧密地存储的(位于连续的内存区域),而不是链式结构。
#include <iostream>
#include <tuple>
#include <string>
#include <functional>
#include <utility>
int test_tuple_4()
{
{ // tuple::tuple: Constructs a tuple object. This involves individually constructing its elements,
// with an initialization that depends on the constructor form invoke
std::tuple<int, char> first; // default
std::tuple<int, char> second(first); // copy
std::tuple<int, char> third(std::make_tuple(20, 'b')); // move
std::tuple<long, char> fourth(third); // implicit conversion
std::tuple<int, char> fifth(10, 'a'); // initialization
std::tuple<int, char> sixth(std::make_pair(30, 'c')); // from pair / move
std::cout << "sixth contains: " << std::get<0>(sixth);
std::cout << " and " << std::get<1>(sixth) << '\n';
}
{ // std::tuple::operator=: Each of the elements in the tuple object is assigned its corresponding element
std::pair<int, char> mypair(0, ' ');
std::tuple<int, char> a(10, 'x');
std::tuple<long, char> b, c;
b = a; // copy assignment
c = std::make_tuple(100L, 'Y'); // move assignment
a = c; // conversion assignment
c = std::make_tuple(100, 'z'); // conversion / move assignment
a = mypair; // from pair assignment
a = std::make_pair(2, 'b'); // form pair /move assignment
}
{ // std::tuple::swap: (containing objects of the same types in the same order)
std::tuple<int, char> a(10, 'x');
std::tuple<int, char> b(20, 'y');
a.swap(b);
std::swap(a, b);
}
{ // std::relational operators: Performs the appropriate comparison operation between the tuple objects lhs and rhs
std::tuple<int, char> a(10, 'x');
std::tuple<char, char> b(10, 'x');
std::tuple<char, char> c(10, 'y');
if (a == b) std::cout << "a and b are equal\n";
if (b != c) std::cout << "b and c are not equal\n";
if (b<c) std::cout << "b is less than c\n";
if (c>a) std::cout << "c is greater than a\n";
if (a <= c) std::cout << "a is less than or equal to c\n";
if (c >= b) std::cout << "c is greater than or equal to b\n";
}
return 0;
}
int test_tuple_3()
{
typedef std::tuple<int, double, int, double> Mytuple;
Mytuple c0(0, 1, 2, 3);
Mytuple c1;
c1 = c0;
std::cout << " " << std::get<3>(c1);
std::cout << std::endl;
typedef std::tuple<int, float, int, float> Mytuple2;
Mytuple c4(Mytuple2(4, 5, 6, 7));
return (0);
}
static std::tuple<double, char, std::string> get_student(int id)
{
if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten");
throw std::invalid_argument("id");
}
int test_tuple_2()
{
auto student0 = get_student(0);
//表达式之间在功能上没有差别
//std::tie(x, y, z) 和 std::make_tuple(std::ref(x), std::ref(y), std::ref(z))
double gpa1;
char grade1;
std::string name1;
std::tie(gpa1, grade1, name1) = get_student(1);
std::cout << "ID: 1, "
<< "GPA: " << gpa1 << ", "
<< "grade: " << grade1 << ", "
<< "name: " << name1 << '\n';
return 0;
}
static void fun(int &a)
{
a = 15;
}
int test_tuple_1()
{
{ // std::tuple_element: class template, Class designed to access the type of the Ith element in a tuple.
// It is a simple class with a single member type, tuple_element::type,
// defined as an alias of the type of the Ith element in a tuple of type T.
auto mytuple = std::make_tuple(10, 'a');
std::tuple_element<0, decltype(mytuple)>::type first = std::get<0>(mytuple);
std::tuple_element<1, decltype(mytuple)>::type second = std::get<1>(mytuple);
}
{ // std::tuple_size: Class template designed to access the number of elements in a tuple
std::tuple<int, char, double> mytuple(10, 'a', 3.14);
std::cout << "mytuple has ";
std::cout << std::tuple_size<decltype(mytuple)>::value;
std::cout << " elements." << '\n';
}
{ // std::forward_as_tuple: function template, Constructs a tuple object with rvalue references
// to the elements in args suitable to be forwarded as argument to a function.
std::string str("John");
print_pack(std::forward_as_tuple(str + " Smith", 25));
print_pack(std::forward_as_tuple(str + " Daniels", 22));
}
{ // std::get: funtion template, Returns a reference to the Ith element of tuple tpl.
std::tuple<int, char> mytuple(10, 'a');
std::get<0>(mytuple) = 20;
std::cout << "mytuple contains: ";
std::cout << std::get<0>(mytuple) << " and " << std::get<1>(mytuple);
std::cout << std::endl;
}
{ // std::make_tuple: function template, Constructs an object of the appropriate tuple type
// to contain the elements specified in args
auto first = std::make_tuple(10, 'a'); // tuple < int, char >
const int a = 0; int b[3]; // decayed types:
auto second = std::make_tuple(a, b); // tuple < int, int* >
auto third = std::make_tuple(std::ref(a), "abc"); // tuple < const int&, const char* >
std::cout << "third contains: " << std::get<0>(third);
std::cout << " and " << std::get<1>(third);
std::cout << std::endl;
}
{ // std::tie: function template, Constructs a tuple object whose elements are references
// to the arguments in args, in the same order
// std::ignore: object, This object ignores any value assigned to it. It is designed to be used as an
// argument for tie to indicate that a specific element in a tuple should be ignored.
int myint;
char mychar;
std::tuple<int, float, char> mytuple;
mytuple = std::make_tuple(10, 2.6, 'a'); // packing values into tuple
std::tie(myint, std::ignore, mychar) = mytuple; // unpacking tuple into variables
std::cout << "myint contains: " << myint << '\n';
std::cout << "mychar contains: " << mychar << '\n';
}
{ // std::tuple_cat: function template, Constructs an object of the appropriate tuple type
// to contain a concatenation of the elements of all the tuples in tpls, in the same order
std::tuple<float, std::string> mytuple(3.14, "pi");
std::pair<int, char> mypair(10, 'a');
auto myauto = std::tuple_cat(mytuple, std::tuple<int, char>(mypair));
std::cout << "myauto contains: " << '\n';
std::cout << std::get<0>(myauto) << '\n';
std::cout << std::get<1>(myauto) << '\n';
std::cout << std::get<2>(myauto) << '\n';
std::cout << std::get<3>(myauto) << '\n';
}
{ // tuple::tuple: A tuple is an object capable to hold a collection of elements.
// Each element can be of a different type.
std::tuple<int, char> foo(10, 'x');
auto bar = std::make_tuple("test", 3.1, 14, 'y');
std::get<2>(bar) = 100; // access element
int myint; char mychar;
std::tie(myint, mychar) = foo; // unpack elements
std::tie(std::ignore, std::ignore, myint, mychar) = bar; // unpack (with ignore)
mychar = std::get<3>(bar);
std::get<0>(foo) = std::get<2>(bar);
std::get<1>(foo) = mychar;
std::cout << "foo contains: ";
std::cout << std::get<0>(foo) << ' ';
std::cout << std::get<1>(foo) << '\n';
}
{
std::tuple<int, char> foo{ 12, 'a' };
std::cout << std::get<0>(foo) << "\n"; // 12
fun(std::get<0>(foo));
std::cout << std::get<0>(foo) << "\n"; // 15
}
return 0;
}
三.猜你喜欢
- C语言 数组下标越界和内存溢出区别
- C语言 使用指针遍历数组
- C语言 指针和数组区别
- C语言 指针数组和数组指针区别
- C语言 野指针
- C语言 函数值传递和址传递
- C语言 函数不定长参数
- C语言 函数指针
- C语言 指针函数
- C语言 回调函数 callback
- C语言 #pragma once
- C语言 #include <> 与 #include “” 区别
- C语言 const 修饰函数参数
- C语言 const 和 define 区别
- C语言 va_start / va_end / va_arg 自定义 printf 函数
- C语言 main 函数参数 main(int argc, char *argv[])
- C语言 结构体struct简介(一)
- C语言 结构体struct定义和使用(二)
- C语言 结构体struct数组(三)
- C语言 结构体struct指针(四)
- C语言 结构体struct成员函数(五)
- C语言 结构体struct嵌套(六)
- C语言 结构体struct值传递和址传递(七)
- C/C++ error: cannot assign to non-static data member within const member function ‘xxxx’
- C++ 关于类中 const 的使用
- C/C++ =delete
- C/C++ 条件编译 #ifdef
- C/C++ error C2065: “M_PI”: 未声明的标识符
- C/C++ error C2027: 使用了未定义类型“std::tuple”
ChatGPT 3.5 国内中文镜像站免费使用啦
暂无评论内容