CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    [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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Two Dimensional Vector

    Quote Originally Posted by Peter_APIIT View Post
    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

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Two Dimensional Vector

    Quote Originally Posted by Peter_APIIT View Post
    The problem is that i don't know how many row are there ?
    vector::resize().

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Two Dimensional Vector

    Quote Originally Posted by Peter_APIIT View Post
    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

  8. #8
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  9. #9
    Join Date
    Nov 2007
    Posts
    74

    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.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    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

  11. #11
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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
  •  





Click Here to Expand Forum to Full Width

Featured