-
February 11th, 2004, 10:59 AM
#1
Vector list
I would like to use vector or list (or anything like that) as a multidimentional array, is it possible ? If so, how can I do that ?
For example, how can I change the following code into "something similar" but use vector/list/...
PHP Code:
string a[100][100][100];
Thanks
-
February 11th, 2004, 11:32 AM
#2
Re: Vector list
Originally posted by Charleston
I would like to use vector or list (or anything like that) as a multidimentional array, is it possible ? If so, how can I do that ?
For example, how can I change the following code into "something similar" but use vector/list/...
PHP Code:
string a[100][100][100];
Thanks
Code:
#include <vector>
#include <string>
typedef std::vector<std::string>S1;
typedef std::vector<S1> S2;
typedef std::vector<S2> S3;
int main()
{
S3 a(100, S2(100, S1(100)));
}
Regards,
Paul McKenzie
-
February 11th, 2004, 11:43 AM
#3
Originally posted by Paul McKenzie
Code:
#include <vector>
#include <string>
typedef std::vector<std::string>S1;
typedef std::vector<S1> S2;
typedef std::vector<S2> S3;
int main()
{
S3 a(100, S2(100, S1(100)));
}
Regards,
Paul McKenzie
Thanks Paul very much,
But I still have another question that, I do not know how to use that array a, if it is declared as what you have taught me, would you please spend some time explaining a little more ??
Thank You,
Regards,
-
February 11th, 2004, 11:48 AM
#4
Use it just like any other 3-dimensional array:
Code:
a[0][0][0] = "Test";
You also have the benefit of the std::vector functions to resize any dimension, etc.
Basically, I created a vector of a vector of a vector of strings. This is equivalent in concept to your three dimensional array of strings.
Regards,
Paul McKenzie
-
February 11th, 2004, 12:06 PM
#5
Originally posted by Paul McKenzie
Use it just like any other 3-dimensional array:
Code:
a[0][0][0] = "Test";
You also have the benefit of the std::vector functions to resize any dimension, etc.
Basically, I created a vector of a vector of a vector of strings. This is equivalent in concept to your three dimensional array of strings.
Regards,
Paul McKenzie
Thanks Paul so much, CG is really lucky to have a guru like You, and I am also lucky to be helped...True.
Regards,
-
February 11th, 2004, 01:07 PM
#6
I would like to have another question, would Paul or anyone please tell me how I can use push_back in this case ?
Thanks,
-
February 11th, 2004, 01:35 PM
#7
What I mean is like this, for example, I have a 2d array, and if I just use an C-style array, I would do something like
PHP Code:
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]=i+j;
But since I have just been taught about using vector of vector of string, I would like to use push_back and even some std functions like transform, I dont know how I can make it now
If it is just a normal 1d vector I can try to handle it, but now it gets doubled (vector<vector>),I am stuck,
I can do as what Paul told me but I would like to know a little more, would anyone help me ?
Thanks
-
February 11th, 2004, 02:26 PM
#8
multi-dimensional vectors are tricky to work with. The
important thing to remember is that they are "vector
or vectors".
for example :
Code:
typedef std::vector<int>I1;
typedef std::vector<I1> I2;
I2 v;
V[i][j] is an int
but each indivdual element is a vector<int>
v[i] is a vector<int>
likewise, when using iterators in one of the algorithms,
the elements are vetcor<int>. See example below:
Code:
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef std::vector<int>I1;
typedef std::vector<I1> I2;
template <typename T>
struct MultiplyVector
{
MultiplyVector(T factor) : m_factor(factor) {}
void operator () (I1 & v) // v is a vector<int>
{
std::transform(v.begin(),v.end(),v.begin(),std::bind2nd(std::multiplies<T>(),m_factor));
}
T m_factor;
};
int main()
{
I2 v;
I1 va;
va.push_back(0);
va.push_back(1);
v.push_back(va);
I1 vb;
vb.push_back(5);
vb.push_back(6);
vb.push_back(7);
v.push_back(vb);
int i,j;
for (i=0; i<v.size(); ++i)
{
for (j=0; j<v[i].size(); ++j)
{
cout << "v[" << i << "][" << j << "] = " << v[i][j] << endl;
}
}
// multiply everything by 2 using for_each
for_each(v.begin(),v.end(),MultiplyVector<int>(2));
cout << "\n\nafter for_each\n\n";
for (i=0; i<v.size(); ++i)
{
for (j=0; j<v[i].size(); ++j)
{
cout << "v[" << i << "][" << j << "] = " << v[i][j] << endl;
}
}
return 0;
}
-
February 11th, 2004, 02:47 PM
#9
I thought Philip didnot like me, thats why Philip didnot help me,
Now many many thanks to You,
You are really very kind, True ! I see it in the way You behave to people on boards.
Once again, Thank Philip so very much,
-
February 11th, 2004, 02:50 PM
#10
In general, a basic understanding of the 'vector' class should help...take a look at the following introduction...
-
February 11th, 2004, 03:18 PM
#11
Originally posted by Andreas Masur
In general, a basic understanding of the 'vector' class should help...take a look at the following introduction...
Thank Andreas a lot, I didnot pay attention to that link, I didnot know there is one out there,
But I think Philip's post is great and specific especially to the example I gave out, it may be a cake to many gurus like Andreas, but to a newb like me, it is not at all, and I like his solutions, True !
Once again, Thank Andreas a lot, True
-
February 12th, 2004, 08:13 PM
#12
Philip,
I would like to ask for another favor,
Philip gave me that solution but it is just for matrix 2xN, how about MxN matrix,I donot know how to do that, would Philip be nice to help me again ???
Thanks a lot
-
February 12th, 2004, 09:38 PM
#13
Anyone is Okay, please help me...
Thanks
-
February 12th, 2004, 09:58 PM
#14
Philip's code works fine for MxN matrices. In his example, M just happens to be 2 because he only pushed back twice.
You could use a for loop to initialize each entry to a specific value:
Code:
const int M = 26;
const int N = 38;
I2 v;
for (i=0; i<M; ++i)
{
I1 va;
for (j=0; j<N; ++j)
{
va.push_back(i);
}
v.push_back(va);
}
or if you can initialize them all to the same value then:
Code:
const int M = 26;
const int N = 38;
// Initialize all to 17.
I2 v(M, I1(N, 17));
// Initialize all to 0.
I2 v2(M, I1(N));
-
February 12th, 2004, 10:06 PM
#15
Originally posted by jlou
Philip's code works fine for MxN matrices. In his example, M just happens to be 2 because he only pushed back twice.
You could use a for loop to initialize each entry to a specific value:
Code:
const int M = 26;
const int N = 38;
I2 v;
for (i=0; i<M; ++i)
{
I1 va;
for (j=0; j<N; ++j)
{
va.push_back(i);
}
v.push_back(va);
}
or if you can initialize them all to the same value then:
Code:
const int M = 26;
const int N = 38;
// Initialize all to 17.
I2 v(M, I1(N, 17));
// Initialize all to 0.
I2 v2(M, I1(N));
Thanks jlou a lot, I was trying to make it work, but I couldnot, I didnot understand his code could work for NxM matrix,
Thanks jlou a lot, you helped me out...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|