开发者

Declaring a C++ arraylist in Visual studio?

Apologies for the trivial question, but im having problems with the ex开发者_开发知识库amples i find on microsoft support website.

Could someone please show me how to declare the libraries require (above main) for the ArrayList so that i can just define it as such:

ArrayList a = new ArrayList();

I cant get the libraries for 'ArrayList' to be recognised?


Are you using C++/CLI (managed C++)? This class is not available in native C++, fyi.

std::vector is the closest native code equivalent.

If you are using C++/CLI then you have to add a reference to the required assembly (System.Collections) in your project - right click the project in Solution Explorer, select Add Reference, pick from .Net tab.

Then make it available to your code as shown below and in the MSDN examples:

using namespace System::Collections;

See this one for Add method, for instance.


According to this article: http://msdn.microsoft.com/en-us/library/system.collections.arraylist(VS.71).aspx

You need this syntax:

#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

ArrayList* a = new ArrayList();
a->Add(S"One");
a->Add(S"Two");


in C++/CLI it is ArrayList^ a = gcnew ArrayList()


use stl's vector:

#include <vector>
int main()
{
   std::vector<int> v_of_int;
   v_of_int.push_back(5);
   int val = v_of_int[0];
   ...
}


ArrayList sounds suspiciously like a Java container - are you after std::list<> or std::vector<> maybe? Infact, std::deque<> will do you better!


The documentation seems to indicate you need

#using <mscorlib.dll>
using namespace System::Collections;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜