C++ 从元组中提取向量
我在元组函数中得到了C++ 从元组中提取向量,c++,vector,tuples,C++,Vector,Tuples,我在元组函数中得到了ints,一个vector,还有一个vector。如果运行此函数时出错,它将返回元组:{0,0,{0},{0}}。ints没有错误。我对这段代码做了很多简化,但我不明白为什么不能在元组中使用向量。它在{下面放了一条红色的曲线。 下面是一个更简单的版本: #include <iostream> #include <vector> using namespace std; tuple<vector<int>, vector<st
int
s,一个vector
,还有一个vector
。如果运行此函数时出错,它将返回元组:{0,0,{0},{0}}
。int
s没有错误。我对这段代码做了很多简化,但我不明白为什么不能在元组中使用向量。它在{
下面放了一条红色的曲线。
下面是一个更简单的版本:
#include <iostream>
#include <vector>
using namespace std;
tuple<vector<int>, vector<string>> tuples()
{
return { {0}, {"0"} };
}
int main()
{
tuple<vector<int>, vector<string>> result = tuples();
if (get<0>(result) == {0} && get<1>(result) == {"0"})
{
cout << "It worked!\n";
}
return 0;
}
#包括
#包括
使用名称空间std;
元组
{
返回{0},{“0”};
}
int main()
{
元组结果=元组();
if(get(result)={0}&&get(result)=={0})
{
不能使用初始化器
在右侧即=
之后列出{0}
不能在元组中使用向量
是一个false
语句
将代码更改为:
tuple<vector<int>, vector<string>> result = tuples();
vector<int> v = {0};
vector<string> v2 = {"0"};
if (get<0>(result) == v && get<1>(result) == v2)
{
cout << "It worked!\n";
}
tuple result=tuples();
向量v={0};
向量v2={“0”};
if(get(result)==v&&get(result)==v2)
{
不能使用初始化器
在右侧即=
之后列出{0}
不能在元组中使用向量
是一个false
语句
将代码更改为:
tuple<vector<int>, vector<string>> result = tuples();
vector<int> v = {0};
vector<string> v2 = {"0"};
if (get<0>(result) == v && get<1>(result) == v2)
{
cout << "It worked!\n";
}
tuple result=tuples();
向量v={0};
向量v2={“0”};
if(get(result)==v&&get(result)==v2)
{
cout检查这个:)只要您不能返回初始值设定项列表(向量的隐式构造函数),您就可以返回创建的元组和向量构造函数(显式)
#包括
#包括
#包括
使用名称空间std;
std::tuple tuples()
{
返回std::make_tuple(std::vector({0}),std::vector({0}));
}
int main()
{
元组结果=元组();
if(get(result)==std::vector{0}&&get(result)==std::vector{0})
{
cout检查这个:)只要您不能返回初始值设定项列表(向量的隐式构造函数),您就可以返回创建的元组和向量构造函数(显式)
#包括
#包括
#包括
使用名称空间std;
std::tuple tuples()
{
返回std::make_tuple(std::vector({0}),std::vector({0}));
}
int main()
{
元组结果=元组();
if(get(result)==std::vector{0}&&get(result)==std::vector{0})
{
库特