如何从函数返回一个整数和一个向量.在c ++ 11中,我可以使用元组.但我必须使用C++ 98标准.
问题是这样的,
int myfunction(parameter 1,parameter 2)
{
vector<int> created_here;
//do something with created here
return int & created_here both
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点.顺便说一下,我必须递归地使用我的函数.所以我想到了这样的方式,
int n;
vector<int> A;
int myfunction(int pos,int mask_cities,vector<int> &A)
{
if(mask = (1<<n)-1)
return 0;
vector<int> created_here;
int ans = 999999;
for(int i=0;i<n;++i){
int tmp = myfunction(pos+1,mask|1<<i,created_here);
if(tmp<ans){
A = created_here;
ans = tmp;
}
}
return ans;
}
Run Code Online (Sandbox Code Playgroud)
这会有用吗?或者有一个更好的解决方案.顺便说一句,我的实际问题是找到旅行商问题的解决方案.这应该澄清我的需求
用途std::pair<>:
std::pair<int, std::vector<int> > myfunction() {
int i;
std::vector<int> v;
return std::make_pair(i, v);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3540 次 |
| 最近记录: |