4

Hello I need to have vector (or some other data structure, which is similar) filled with managed objects.

Usually I can write:

std::vector<Object> vect;

But I cannot use:

std::vector<Object^> vect;

Can somebody explain, how to change declaration or advice other structure instead of vector. Thanks.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
ST3
  • 8,826
  • 3
  • 68
  • 92
  • What is your intention behind this? Are you using C++/CLI? Or rather WinRT? – bash.d Feb 20 '13 at 13:58
  • 1
    Use .NET generic List class: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx – Alex F Feb 20 '13 at 14:00
  • @AlexFarber this is about C++, not C#. – bash.d Feb 20 '13 at 14:07
  • 2
    @bash.d: No. This is about C++.Net, in which `List` might indeed be the correct answer. – ereOn Feb 20 '13 at 14:10
  • @ereOn this might be, but how do you know? Nothing has been said about this... – bash.d Feb 20 '13 at 14:11
  • @bash.d: One would think that the `Object^` and the `C++`,`managed` tags kind of imply that. :) – ereOn Feb 20 '13 at 14:13
  • @ereOn The "hat"-notation (^) also exists in Windows 8 /XAML, which is a little different from C++/CLI. – bash.d Feb 20 '13 at 14:14
  • @bash.d: ...which aren't C++ either, invalidating your own previous statement. Where do you want to go with that ? – ereOn Feb 20 '13 at 14:15
  • Nonsense! There is no std-namespace in .NET. Whereas it does exist in WinRT. – bash.d Feb 20 '13 at 14:18
  • 1
    Thank you for advice, everything works, I used this: "List^ listName" – ST3 Feb 20 '13 at 14:18
  • @AlexFarber: You may want to post that as an answer, especially since it has been proven to be the right answer. – ereOn Feb 20 '13 at 14:22
  • I have one more question about this. How to iterate through this list? I have it in some class and I need method witch returns one value from list. – ST3 Feb 20 '13 at 14:42

2 Answers2

4

Use .NET generic List class: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

List enumeration sample from the same WEB page, as requested by user1237747's comment:

#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;

int main(array<System::String ^> ^args)
{
    List<String^>^ dinosaurs = gcnew List<String^>();

    dinosaurs->Add("Tyrannosaurus");
    dinosaurs->Add("Amargasaurus");

    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    return 0;
}

Replace String^ with the type you need. You can also access List elements by index using [] operator.

Generally, avoid mixing managed and unmanaged types, if this is not absolutely necessary.

Alex F
  • 42,307
  • 41
  • 144
  • 212
4

Yes, there are stl data structures for managed types. Below is an example using a vector (per your request).

#include <cliext/vector>

cliext::vector<Object^> vect;

Docs for this vector type are here

GPPK
  • 6,546
  • 4
  • 32
  • 57
Chad Crowe
  • 1,260
  • 1
  • 16
  • 21
  • This did not work for me: The decalration yields "Error C2833 'operator _Mycont_it' is not a recognized operator or type". – user74696c Jul 24 '20 at 11:50