using Vectors insteard of Two dimentional arrays...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pra1983
    New Member
    • Oct 2007
    • 10

    using Vectors insteard of Two dimentional arrays...

    Can any one tell me how to use vectors insteard of two dimensional arrays...

    I was trying to store some thousands of values in an aray but i gives me error in between saying that
    " Stack is out of order in so and so ............... memory location "


    so i tried to use vectors.....
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by pra1983
    Can any one tell me how to use vectors insteard of two ...


    so i tried to use vectors.....
    How?

    Savage

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      [CODE=cpp]vector< vector<yourType > > myVec;[/CODE]

      Note the spaces! They are very important.

      Comment

      • pra1983
        New Member
        • Oct 2007
        • 10

        #4
        Originally posted by Ganon11
        [CODE=cpp]vector< vector<yourType > > myVec;[/CODE]

        Note the spaces! They are very important.
        how can i enter data into that vec . and how can i access the data from the vec after filling in it.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Well, myVec is a vector, so you will just use push_back on it. But myVec is a vector holding vectors. So you need to push_back a vector onto myVec. So the way to add data to myVec is to build a temporary vector with any data you want, and then push_back this vector.

          Once you have built the vector of vectors, access data just like you would a 2-dimensional array.

          Comment

          • pra1983
            New Member
            • Oct 2007
            • 10

            #6
            reply

            Originally posted by Ganon11
            Well, myVec is a vector, so you will just use push_back on it. But myVec is a vector holding vectors. So you need to push_back a vector onto myVec. So the way to add data to myVec is to build a temporary vector with any data you want, and then push_back this vector.

            Once you have built the vector of vectors, access data just like you would a 2-dimensional array.
            vec ---- vector in vector
            vec1----- vector which is pushedback into vec
            I am pushing back different size vec1's into vec. can u tell me how to check the length or size of each vector in 'vec' while accessing 'vec'.
            Last edited by pra1983; Oct 5 '07, 02:43 AM. Reason: due to appropriate explaination

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              [CODE=cpp]vec[i].length()[/CODE]

              Any member in vec (vec[i] is a vector, and so it has a .length() function.

              Comment

              • pra1983
                New Member
                • Oct 2007
                • 10

                #8
                Originally posted by Ganon11
                [CODE=cpp]vec[i].length()[/CODE]

                Any member in vec (vec[i] is a vector, and so it has a .length() function.
                how to clear the elements of the vector which has elements.

                if in previous i have

                vect=0 1 2 3 4 5 6
                i need to rewrite the vect to
                vect= 7 8 9 10 11 12

                but it is getting added up like

                vect= 1 2 3 4 5 6 7 8 9 10 11 12......

                how to clear the vector
                i am using ".empty()" vector.. should i keep it in loop.....

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by pra1983
                  how to clear the elements of the vector which has elements.

                  if in previous i have

                  vect=0 1 2 3 4 5 6
                  i need to rewrite the vect to
                  vect= 7 8 9 10 11 12

                  but it is getting added up like

                  vect= 1 2 3 4 5 6 7 8 9 10 11 12......

                  how to clear the vector
                  i am using ".empty()" vector.. should i keep it in loop.....
                  empty is a bool function which checks if vector is empty,if you want to delete the elements use erase instead.

                  Savage

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    pra1983:

                    You keep coming back here to ask more and more simple questions about vectors. Try looking at some of its documentation at www.cplusplus.com. Many, if not all, of your questions can be answered between this website and a little trial and error on your part.

                    Comment

                    • pra1983
                      New Member
                      • Oct 2007
                      • 10

                      #11
                      Originally posted by Ganon11
                      pra1983:

                      You keep coming back here to ask more and more simple questions about vectors. Try looking at some of its documentation at www.cplusplus.com. Many, if not all, of your questions can be answered between this website and a little trial and error on your part.
                      I got that but it is giving me an error sayig that

                      "Microsoft Visual Studio
                      Unhandled exception at 0x00000000 in project2.exe: 0xC0000005:
                      Access violation reading location 0x00000000
                      BREAK CONTINUE ."


                      I got this so I swiched from two dimensional arrays to vectors.
                      but in vectors also i am getting the same error .....
                      can any one resolve this error.....

                      Comment

                      Working...