我有一个返回std::pair<int, double>
的函数。
我想创建另一个函数来使用前一个函数中的对并查看该对中的数据,即第一个和第二个。是否可以将前一个函数的返回作为新函数的参数传递,以便第二个函数可以查看数据?我不确定C ++中的语法。
void func2( const std::pair <int, double>& data )
{
}
std::pair <int, double> func1 ( /* ... * / )
{
//return pair
}
然后打电话
func2 ( fun1( /*... */ ) );
我想你的意思就像上面的例子:
#include <utility>
std::pair<int, double> create() {
return std::make_pair(4, 2.);
}
void accept(std::pair<int, double> p) { }
int main() {
accept(create());
}
答案是肯定的,你可以做到。
如果你不想传递接受该对的副本,而是想要在main函数中提交你正在使用它的确切实例,你可以稍微修改函数原型,如下所示:
#include <utility>
std::pair<int, double> create() {
return std::make_pair(4, 2.);
}
void accept(std::pair<int, double>& p) { }
int main() {
auto p = create();
accept(p);
}
等等...