将std :: vector转换为system :: array [英] converting std::vector to system::array

查看:109
本文介绍了将std :: vector转换为system :: array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将std :: vector转换为system :: array?



我敢这么做



How do you convert the std::vector to system::array?

I dared to do

array<int>^ Arr = gcnew array<int>( stdVec.size());

for(int i=0; i < stdVec.size(); i++)
{
  Arr[i] = stdVec[i];
}



没有编译器错误所以我不知道这是否真的没问题。我的程序崩溃了,所以我希望原因是转换而不是别的。



非常感谢,因为总是


There wasn''t a compiler error so I dont know if this is actually ok or not. My program crashes at this line though so I''m hoping the reason is the conversion and not something else.

Many thanks as always

推荐答案

首先,不要硬编码你的10,这使代码完全不受支持。听说过常数吗?你明白它可能是一个变量吗?



至于代码,它看起来是正确的,但谁知道什么是 stdVec ,声明的地方?你不应该对代码的某些未知部分提出任何要求。信不信由你,但由于春末和大雪融化,我对你硬盘的访问有限。如果正确声明 stdVec 作为 std :: vector int ,您的代码将正常运行。请参阅:

http://www.cplusplus.com/reference/vector/vector/ [ ^ ]。



应该类似

First of all, don''t hard-code your 10, this makes the code completely unsupportable. Ever heard of constants? Do you understand that it could be a variable?

As to the code, it looks correct, but who knows what is stdVec, where it is declared? You should not ask anything about some unknown part of the code. Believe or not, but because of late spring and massive snow melting, my access to your hard drive is somewhat limited. If you declare stdVec correctly as an instance of std::vector of int, your code will work correctly. Please see:
http://www.cplusplus.com/reference/vector/vector/[^].

Should be something like
#include <vector>
using namespace std;

//...

vector<int> stdVec;
// only name the object in a humanly manner, is some semantically significant way
// same goes about "Arr" and "i"... :-)





只是为了一些乐趣,请看这个CodeProject文章: C ++ / CLI中的数组 [ ^ ]。



-SA


这篇关于将std :: vector转换为system :: array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆