C++ 从c+中的函数返回数组向量+;
我必须从函数中返回向量。我尝试返回值并将其作为引用传递。但我有垃圾价值。 下面的程序给出以下输出C++ 从c+中的函数返回数组向量+;,c++,vector,stl,C++,Vector,Stl,我必须从函数中返回向量。我尝试返回值并将其作为引用传递。但我有垃圾价值。 下面的程序给出以下输出 **This is the print inside the function** 1 7 8 1 2 3 4 5 6 **This is the print using the return value / referenced value outside the function**. 1 7 8 46980021526656 46980019425190 0
**This is the print inside the function**
1 7 8
1 2 3
4 5 6
**This is the print using the return value / referenced value outside the function**.
1 7 8
46980021526656 46980019425190 0
1 46980021526656 6
#include <iostream>
#include<vector>
using namespace std;
void process(vector<unsigned long long int*> &vec)
{
//vector<unsigned long long int*> vec;
unsigned long long int a[] ={1,2,3};
unsigned long long int b[] = {4,5,6};
vec.push_back(a);
vec.push_back(b);
for (int i = 0;i < vec.size();i++)
{
for (int j = 0;j < 3;j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
//return vec;
}
int main() {
// your code goes here
vector<unsigned long long int*> vec;
unsigned long long int c[] ={1,7,8};
vec.push_back(c);
process(vec);
for (int i = 0;i < vec.size();i++)
{
for (int j = 0;j < 3;j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
return 0;
}
**这是函数内部的打印**
1 7 8
1 2 3
4 5 6
**这是使用函数**之外的返回值/引用值进行打印。
1 7 8
46980021526656 46980019425190 0
1 46980021526656 6
#包括
#包括
使用名称空间std;
无效过程(向量和向量)
{
//向量向量机;
无符号长整型a[]={1,2,3};
无符号长整型b[]={4,5,6};
向量推回(a);
向量推回(b);
对于(int i=0;i
您的代码是:
void流程(向量和向量)
{
//向量向量机;
无符号长整型a[]={1,2,3};
无符号长整型b[]={4,5,6};
向量推回(a);
向量推回(b);
因此,程序在vec中存储局部变量a和b的地址,当你回到主程序中时,这些局部变量不再存在,它们的内容是未定义的/被破坏的,所以你在主程序中写入未定义的值
vec.push_back(a);
不复制a,它只会推送a的地址尝试使用标准库数组:
#include <iostream>
#include <vector>
#include <array>
using namespace std;
void process(vector<array<int, 3>> &vec)
{
//vector<unsigned long long int*> vec;
array<int, 3> a{1, 2, 3};
array<int, 3> b{4, 5, 6};
vec.push_back(a);
vec.push_back(b);
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < 3; j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
//return vec;
}
int main()
{
// your code goes here
vector<array<int, 3>> vec;
array<int, 3> c{1, 7, 8};
vec.push_back(c);
process(vec);
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < 3; j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
return 0;
}
#包括
#包括
#包括
使用名称空间std;
无效过程(向量和向量)
{
//向量向量机;
数组a{1,2,3};
数组b{4,5,6};
向量推回(a);
向量推回(b);
对于(int i=0;i 哈哈,我知道,这不是我的代码,这是他的代码,我只是解释他的错误。我想问题是关于正确的做法。仅解释错误本身并不是一个答案。这个答案绝对正确。Peeps在愤怒地投票前需要更仔细地阅读。@MateuszGrzejek不,问题是字面上的(引用):“请指出我做错了什么”布鲁诺说,这仍然是事实,从VC++中的本地范围存储数组是非常糟糕的。STL没有这样的功能。但是,C++标准库,最近添加了它。@ LexNeasraceSeNeReScET现在固定。
#include <iostream>
#include <vector>
#include <array>
using namespace std;
void process(vector<array<int, 3>> &vec)
{
//vector<unsigned long long int*> vec;
array<int, 3> a{1, 2, 3};
array<int, 3> b{4, 5, 6};
vec.push_back(a);
vec.push_back(b);
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < 3; j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
//return vec;
}
int main()
{
// your code goes here
vector<array<int, 3>> vec;
array<int, 3> c{1, 7, 8};
vec.push_back(c);
process(vec);
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < 3; j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
return 0;
}