I am about to convert this 2D vector to managed code where I instead will use a List<String>.

The problem is that I dont know how to do that, I would be happy to see how that could be done.

std::vector<std::vector<string> > vector1(1000, std::vector<string>());

Recommended Answers

All 12 Replies

I am about to convert this 2D vector to managed code where I instead will use a List<String>.

The problem is that I dont know how to do that, I would be happy to see how that could be done.

Short answer: you can't. Vectors and Lists are both sequence containers, so you'll need a "list of vectors" or a "list of lists" if you want to keep the structure in your data.

If you don't want to keep the same structure in your data, then show me an example of some data in your 2d-vector and how you want it to look when it goes in the list.

Thanks for your answer. What I meen is this:

This is a declaration of a 2D vector:

std::vector<std::vector<string> > vector1(1000, std::vector<string>());

What I wonder now is how I will do the same declaration for a List<>. Normally I know that a List can be declared like this:

List<String^>^ vector1 = gcnew List<String^>();

So what I wonder is how I will declare a 2D List like I have done for the native code above ?

Thank you

I wonder if this is correct when looking at examples:

typedef List<String^> FirstD;
typedef List<FirstD^> SecondD;

List2D SecondD; 

for( int i = 0; i < 1000; i++ )
{
	List2D.Add(gcnew FirstD());	
}

A 2D managed list of strings with an initial capacity of 1000 would be declared like this:

List<List<String^>^>^ vector1 = gcnew List<List<String^>^>(1000);

You can use typedef to make that simpler too. ;)

Thanks for answer, this looks interesting but when I try to compile, I get compileerror for this line and I cant figure out what is wrong.

compileerror:
syntax error : '>'

List<List<String^>^>^ vector1 = gcnew List<List<String^>^>(1000);

A 2D managed list of strings with an initial capacity of 1000 would be declared like this:

List<List<String^>^>^ vector1 = gcnew List<List<String^>^>(1000);

You can use typedef to make that simpler too. ;)

I'm not sure about this, because I don't use managed code, but it looks like there's one ^ missing right before (1000). So perhaps:

List<List<String^>^>^ vector1 = gcnew List<List<String^>^>^(1000);

Not sure though.

compileerror:
syntax error : '>'

I do not have any problem with that declaration. Can you post a complete program that throws a syntax error so that I can test it on my end?

I dont know if this helps. When I compile it, I get that error and when I take the line away it works fine.

#include "stdafx.h"
#include <ctime>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream> 
#include <string>  
#include <vector>
#include <cmath>
#include <algorithm>
#include <limits>
#include <ios>
#include <cstdio>
#include <numeric>
#include <cstdlib>
#include <stdio.h>



using namespace System;
using namespace System::IO;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;
using namespace System::Runtime::InteropServices;
using namespace System::Threading;

public: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
List<List<String^>^>^ vector1 = gcnew List<List<String^>^>(1000);
}

The generic List class is in the System::Collections::Generic namespace, not the System::Collections namespace. Either use the full name or add a using directive to the namespace:

using namespace System::Collections::Generic;

Thanks alot, that was the problem, now it works and now I wonder if I do this correct.

This code compiles fine but when I run this code I get the running error that the size was less than the collection.
I am trying to put a string for the 2:nd dimension for element 50 in the first dimension:

List<List<String^>^>^ vector1 = gcnew List<List<String^>^>(1000);
		     
vector1[50]->Add("Hello");
 MessageBox::Show(vector1[50][0]->ToString());

You set the capacity of vector1 in the constructor, but not the size. vector1 is still an empty list, so the first order of business is adding all of the new lists to vector1 before trying to use them. The equivalent native code for that managed declaration would be:

vector<vector<string> > vector1;

vector1.reserve(1000);

Yes, now I get it, a little confusing at first but this seemed to be the way then

List<List<String^>^>^ vector1 = gcnew List<List<String^>^>(1000);	

for( int i = 0; i < 1000; i++ )
{
	vector1->Add(gcnew List<String^>());
}


vector1[50]->Add("Hello");
MessageBox::Show(vector1[50][0]->ToString());

Thank you for your kind help !

You set the capacity of vector1 in the constructor, but not the size. vector1 is still an empty list, so the first order of business is adding all of the new lists to vector1 before trying to use them. The equivalent native code for that managed declaration would be:

vector<vector<string> > vector1;

vector1.reserve(1000);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.