头文件
#include
注意:
①tuple 的c++17起,可以自己推导出来类型。
②std::forward_as_tuple的初始化方式是无法使用结构化绑定访问元素的。
#include
#include
// 构造方式
std::tuple<int, std::string, double> t1(42, "hello", 3.14); // 直接构造
auto t2 = std::make_tuple(100, "world", 2.718); // 使用辅助函数
// C++17起支持类模板参数推导(CTAD)
std::tuple t3(3.14f, "PI"); // 自动推导为 tuple
//tie()
int a;
double b;
string c;
tie(a,b,c)=make_tuple(1,2.33,"3");
//forward_as_tuple
auto tuple4=forward_as_tuple(1,2,"2222");
int i =get<0>(tuple1)
#include
#include
#include
using namespace std;
int main(){
int a;
int b;
string c;
tie(a,b,c)=make_tuple(1,2,"2222");
cout<<a<<endl;//1
cout<<b<<endl;//2
cout<<c<<endl;//2222
}
int main(){
auto [i,j,k] = make_tuple(1,2,"2");
cout<<i<<endl;//1
cout<<j<<endl;//2
cout<<k<<endl;//3
}
int main() {
// 创建一个包含int和string的元组tuple1,初始值为1和"hello"
tuple tuple1(1, "hello");
// 使用C++17的类模板参数推导,创建元组tuple2(2,"world")
// 编译器会自动推导类型为tuple
tuple tuple2(2, "world");
// 交换tuple1和tuple2的内容
tuple1.swap(tuple2);
cout<<get<0>(tuple1)<<" "<<get<1>(tuple1)<<endl;
cout<<get<0>(tuple2)<<" "<<get<1>(tuple2)<<endl;
/*
2 world
1 hello
* */
return 0; // 程序正常结束
}
#include // 包含标准输入输出流库
#include // 包含元组库
#include // 包含字符串库
using namespace std; // 使用标准命名空间
template <size_t I=0,class ... Ts>
void print_tuple(const tuple<Ts...>&t){
if constexpr (sizeof...(Ts)>I){
cout<<get<I>(t)<<" ";
print_tuple<I+1>(t);// 递归打印下一个元素
}
}
int main() {
// 创建一个包含int和string的元组tuple1,初始值为1和"hello"
tuple tuple1(1, "hello");
tuple tuple2(2, " world");
tuple tuple3(3, " you");
// 拼接tuple1和tuple2的内容
auto new_tuple = tuple_cat(tuple1,tuple2,tuple3);
print_tuple(new_tuple);
return 0;
/*
1 hello 2 world 3 you
* */
}
// 获取元组大小
constexpr size_t size1 = std::tuple_size<decltype(tuple1)>::value;
constexpr size_t size2 = std::tuple_size_v<decltype(tuple2)>;
#include
#include
#include
using namespace std; // 使用标准命名空间
int main() {
// 创建一个包含int和string的元组tuple1,初始值为1和"hello"
tuple tuple1(1, "hello");
using type0 = tuple_element<0,decltype(tuple1)>::type;// 传统写法
using type1 = tuple_element_t<0, decltype(tuple1)>;// C++14起更简洁的写法
static_assert(is_same_v<type0,int>);
static_assert(is_same_v<type1, int>); // 验证类型是否为int
cout<<"tuple1[0]的类型"<<typeid(type0).name()<<endl;
cout<<"tuple1[1]的类型"<<typeid(type1).name()<<endl;
return 0;
/*
* */
}
array是序列式容器,类似于C语言的数组,是固定大小的,一旦定义完成后就无法进行扩容或收缩。
#include头文件
begin
end
rbegin
rend
-返回指向第一个元素之前的迭代器
在这里插入代码片
#include
#include
int main()
{
std::array<int, 4> arr = { 1, 3, 2, 4 };
std::cout << "arr values:" << std::endl;
for (auto it = arr.begin(); it != arr.end(); it++) {
std::cout << *it << " ";
}
std::cout << std::endl;
std::cin.get();
return 0;
}
在这里插入代码片
#include
#include
int main()
{
std::array<int, 4> arr = { 1, 3, 2, 4 };
std::cout << "size of arr = " << arr.size() << std::endl;
std::cin.get();
return 0;
}
#include
#include
int main()
{
std::array<int, 4> arr = { 1, 3, 2, 4 };
std::cout << "empty = " << (arr.empty() ? "no" : "yes") << std::endl;
std::cin.get();
return 0;
}
在这里插入代码片
#include
#include
int main()
{
std::array<int, 4> arr = {};
for (int i = 0; i < arr.size(); i++)
{
arr.at(i) = i;
}
for (auto it = arr.begin(); it != arr.end(); it++)
{
std::cout << *it << " " ;
}
std::cout << std::endl;
std::cout << arr.data() << std::endl;
std::array<int, 4> arr1 = { 4, 5, 6,7 };
arr1.swap(arr);
for (auto it = arr.begin(); it != arr.end(); it++)
{
std::cout << *it << " ";
}
std::cout << std::endl;
arr.fill(1);
for (auto it = arr.begin(); it != arr.end(); it++)
{
std::cout << *it << " ";
}
std::cout << std::endl;
std::cin.get();
return 0;
}
作用:解包复合类型数据(元组、结构体、数组)
std::map<int, string> m{{1, "a"}, {2, "b"}};
for (const auto& [key, val] : m) { // 直接解包键值对
cout << key << ":" << val << endl;
}
auto getData() -> std::tuple<int, string, double> {
return {42, "PI", 3.14};
}
auto [id, name, value] = getData(); // 自动解包元组