-
January 10th, 2009, 11:15 PM
#1
[RESOLVED] Two Dimensional Vector
Hello to all expect, how to i declare a two dimensional array which its row is dynamic?
Code:
vector<string>* obj = new vector[10];
Why when i dynamic allocation memory for vector and it doesn't require a delete. If i delete it, run time exception is throw.
Thanks for your help.
Thanks for your help.
-
January 10th, 2009, 11:28 PM
#2
Re: Two Dimensional Vector
Originally Posted by Peter_APIIT
Hello to all expect, how to i declare a two dimensional array which its row is dynamic?
Code:
#include <vector>
#include <string>
typedef std::vector<std::string> StringVector1D;
typedef std::vector<StringVector1D> StringVector2D;
int main()
{
StringVector2D my2DArrayOfString(10, StringVector1D(20)); 2d array of string, 10 x 20
my2DArrayOfString[0][0] = "Test";
}
Regards,
Paul McKenzie
-
January 11th, 2009, 12:00 AM
#3
Re: Two Dimensional Vector
The problem is that i don't know how many row are there ?
Can i do this once i have know the total of row?
Code:
my2DArrayOfString[0].push_back("Test");
Thanks.
Thanks for your help.
-
January 11th, 2009, 12:14 AM
#4
Re: Two Dimensional Vector
-
January 11th, 2009, 12:35 AM
#5
Re: Two Dimensional Vector
Originally Posted by Peter_APIIT
The problem is that i don't know how many row are there ?
vector::resize().
Regards,
Paul McKenzie
-
January 11th, 2009, 02:10 AM
#6
Re: Two Dimensional Vector
The error is
error C2664: 'std::_Vector_val<_Ty,_Alloc>::_Vector_val(const std::_Vector_val<_Ty,_Alloc> &)' : cannot convert parameter 1 from 'const std::allocator<_Ty>' to 'const std::_Vector_val<_Ty,_Alloc> &' c:\program files\microsoft visual studio 8\vc\include\vector 487
From this code
Code:
StringVector2D my2DArrayOfString(10, StringVector1D(20));
Thanks.
Thanks for your help.
-
January 11th, 2009, 04:47 AM
#7
Re: Two Dimensional Vector
Originally Posted by Peter_APIIT
The error is
From this code
Code:
StringVector2D my2DArrayOfString(10, StringVector1D(20));
Thanks.
Please post a complete example!. How in the world can anyone tell you what the compiler error is without showing us all of the code?
Compiler errors can only be diagnosed when given all of the code. No one knows if you included everything, whether you spelled something wrong, whether you've missed a semicolon, or anything else. Just giving a single line where the error occurs is not enough. I'm surprised after all the posts you've done, you fail to do this very simple step of providing complete code to properly diagnose a compiler error.
The following compiles correctly in Comeau.
Code:
#include <vector>
#include <string>
typedef std::vector<std::string> StringVector1D;
typedef std::vector<StringVector1D> StringVector2D;
int main()
{
StringVector2D my2DArrayOfString(10, StringVector1D(20));
my2DArrayOfString[0][0] = "Test";
}
Note there is no difference in that one line of code between what I posted and what you posted.
Regards,
Paul McKenzie
-
January 12th, 2009, 01:25 AM
#8
Re: Two Dimensional Vector
This is the complete code. I bag your pardon.
Code:
#include "stdafx.h"
// =============================================
typedef vector<std::string> strVector;
typedef vector<strVector> nStrVector;
int main(int argc, char* argv[], char* envp[])
{
nStrVector aVtr(10, strVector(20));
for (int loop=0;loop<2;++loop)
{
aVtr[loop].push_back("asdu");
}
return 0;
}
error C2664: 'std::_Vector_val<_Ty,_Alloc>::_Vector_val(const std::_Vector_val<_Ty,_Alloc> &)' : cannot convert parameter 1 from 'const std::allocator<_Ty>' to 'const std::_Vector_val<_Ty,_Alloc> &' c:\program files\microsoft visual studio 8\vc\include\vector 487
Thanks.
Thanks for your help.
-
January 12th, 2009, 04:03 AM
#9
Re: Two Dimensional Vector
strVector strVV;
nStrVector strV;
Each of elements of strVV is a string vector not a string.
Input into a vector first then push the vector into vector of vector.
You don't need to allocate space for VV in its declaration.
-
January 12th, 2009, 04:08 AM
#10
Re: Two Dimensional Vector
The following compiles correctly in Comeau:
Code:
#include <vector>
#include <string>
typedef std::vector<std::string> strVector;
typedef std::vector<strVector> nStrVector;
int main(int argc, char* argv[], char* envp[])
{
nStrVector aVtr(10, strVector(20));
for (int loop=0;loop<2;++loop)
{
aVtr[loop].push_back("asdu");
}
return 0;
}
First, there is no header in standard C++ called "stdafx.h". Why did you include it, as it has nothing to do with the issue? This forum knows nothing about "stdafx.h". Next time, remove it from the posts.
You don't have the correct headers included. The correct headers that are to be included are <vector> and <string>.
Second:
Code:
typedef vector<std::string> strVector;
There is no definition of "vector", since vector is in the std namespace. Where did you specify std::vector, or "using namespace std"?
If you're just typing in the code into your posts, don't type it in! Copy and paste it directly from the code editor into the message.
Regards,
Paul McKenzie
-
January 12th, 2009, 05:00 AM
#11
Re: Two Dimensional Vector
First, there is no header in standard C++ called "stdafx.h".
This is the precompiled header at MS.
I try my code with code block cygwin gcc and it works perfectly and Comeau online compiler too.
MS VS 2005 good in editor but not compiler.
By the way, thanks for your patient and sincere help.
Hope GOD bless you all.
Thread solved.
Thanks for your help.
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
|