可以使用 std::p air 保存三种数据类型的向量
Vector that can hold three data types using std::pair
vector< pair<pair<int, string> >, string> v;
还要提到如何使用"第一"和"第二"访问它们。是否有可能做到这一点,或者"联合"或"结构"是创建可以容纳两种以上数据类型的向量的唯一方法?
std::vector< std::pair<std::pair<int, std::string>, std::string> > v;
是可能的,使用
v[0].first.first = 42;
v[1].first.second = "hello";
v[2].second = "world";
std::tuple
是一个不错的选择:
std::vector<std::tuple<int, std::string, std::string>> v = /*..*/;
std::get<0>(v[0]) = 42;
std::get<1>(v[0]) = "Hello";
std::get<2>(v[0]) = "World";
适当的结构允许给出语义
struct Person
{
int age;
std::string firstName;
std::string lastName;
};
std::vector<Person> persons = /*...*/;
persons[0].age = 42;
persons[0].firstName = "John";
persons[0].lastName = "Doe";
相关文章:
- 有没有办法提示用户使用哪种数据类型作为模板 c++
- 当 c++ 需要一种数据类型并获取另一种数据类型时会发生什么?
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 生成一个类Name_class并将两种数据类型存储在一个向量中
- 如何创建一个包含三种不同类型的向量的向量
- 用于给定描述的哪种数据类型
- 可以使用 std::p air 保存三种数据类型的向量
- 解释C 中三种返回类型的方法
- 哪种数据类型用于存储文件名和文件大小的使用
- 使用两种数据类型作为参数堆叠STL
- 我们应该使用哪种数据类型来输入一个介于 0<= 数字 <= 10^18 之间的数字
- c++我需要将文件中的数据读取到多维数组中,然后用一种数据类型对数组进行排序.怎样
- 哪种数据类型可以容纳10^31的十进制数
- 我应该使用哪种数据类型在 c++ 中的变量中存储多达 18 位数字
- 无法从一种数据类型转换为相同的数据类型?
- 在c++程序中,哪种数据类型不能作为异常抛出
- C++如何在一个向量中存储两种数据类型
- 我想实现一种数据类型,它可以在c++中接受整数值加上一个无穷大符号
- 函数定义中使用的两种数据类型to_string(long double _Val)
- 为什么这个为三种整数类型重载的函数无法编译?