热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

字符串数组在托管C++中的应用与实现-UtilizingArraysofStringsinManagedC++

在托管C++中开发应用程序时,遇到了如何声明和操作字符串数组的问题。本文详细探讨了字符串数组在托管C++中的应用与实现方法,包括声明、初始化、遍历和常见操作技巧,为开发者提供了实用的参考和指导。

I'm trying to write an application in Managed C++, but I cannot work out how to declare an array of strings.

我正在尝试用Managed C ++编写一个应用程序,但我无法弄清楚如何声明一个字符串数组。

String^ linet[];

throws an error

抛出错误

'System::String ^' : a native array cannot contain this managed type

'System :: String ^':本机数组不能包含此托管类型

So I suppose there's a different way to do this for managed data types. What exactly is it?

所以我认为对托管数据类型有不同的方法。究竟是什么?

3 个解决方案

#1


7  

Do you really mean Managed C++? Not C++/CLI?

你真的是指Managed C ++吗?不是C ++ / CLI?

Assuming you're actually using C++/CLI (because of the error message you posted), there are two ways to do this:

假设您实际使用的是C ++ / CLI(由于您发布的错误消息),有两种方法可以执行此操作:

array^ managedArray = gcnew array(10);

will create a managed array, i.e. the same type as string[] in C#.

将创建一个托管数组,即与C#中string []相同的类型。

gcroot[] unmanagedArray;

will create an unmanaged C++ array (I've never actually tried this with arrays - it works well with stl containers, so it should work here, too).

将创建一个非托管C ++数组(我从来没有真正尝试过使用数组 - 它适用于stl容器,因此它也可以在这里工作)。

#2


4  

http://www.codeproject.com/KB/mcpp/cppcliarrays.aspx

That should have all the answers you need :)

那应该有你需要的所有答案:)

When working with Managed C++ (aka. C++/CLI aka. C++/CLR) you need to consider your variable types in everything you do. Any "managed" type (basically, everything that derives from System::Object) can only be used in a managed context. A standard C++ array basically creates a fixed-size memory-block on the heap, with sizeof(type) x NumberOfItems bytes, and then iterates through this. A managed type can not be guarenteed to stay the same place on the heap as it originally was, which is why you can't do that :)

使用托管C ++(又名.C ++ / CLI又名.C ++ / CLR)时,您需要在所做的每件事情中考虑变量类型。任何“托管”类型(基本上,派生自System :: Object的所有内容)只能在托管上下文中使用。标准C ++数组基本上在堆上创建一个固定大小的内存块,使用sizeof(type)x NumberOfItems字节,然后遍历这个。托管类型无法保证在堆上保持原来的位置,这就是为什么你不能这样做:)

#3


1  

You use a collection class from .Net. For example:

您使用.Net的集合类。例如:

List^ dinosaurs = gcnew List();

推荐阅读
author-avatar
手机用户2602904231
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有