Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. no more debugging FAQ.

no more debugging FAQ.

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Resizable arrays? Can any one tell me how do you make a resizable array. Our Instructor wants us to start an array of size 1. and just grows as you keep adding stuff to it.(it does not have to shrink). Thanks

    A C 2 Replies Last reply
    0
    • L Lost User

      Resizable arrays? Can any one tell me how do you make a resizable array. Our Instructor wants us to start an array of size 1. and just grows as you keep adding stuff to it.(it does not have to shrink). Thanks

      A Offline
      A Offline
      Atul Dharne
      wrote on last edited by
      #2

      Use the vector class #include #include using std::vector; using std::cout; using std::endl; int main() { vector reszarr(1); //create a single element int vector //whenever you want to put in an extra value it goes on resizing reszarr.push_back(20); //here 20 is element number 2 reszarr[0] = 10; //populate the first element //To display contents of reszarr for(int count = 0;count < reszarr.size();++count) cout << "Element Number: " << count+1 << " " << reszarr[count] << endl; return EXIT_SUCCESS; } //for displaying the elements you can also use a iterator #using namespace std; vector::iterator inIter; for(inIter = reszarr.begin();inIter != reszarr.end();++inIter) cout << *inIter << endl; Atul Sonork ID : 100.13714 netdiva

      M L 2 Replies Last reply
      0
      • A Atul Dharne

        Use the vector class #include #include using std::vector; using std::cout; using std::endl; int main() { vector reszarr(1); //create a single element int vector //whenever you want to put in an extra value it goes on resizing reszarr.push_back(20); //here 20 is element number 2 reszarr[0] = 10; //populate the first element //To display contents of reszarr for(int count = 0;count < reszarr.size();++count) cout << "Element Number: " << count+1 << " " << reszarr[count] << endl; return EXIT_SUCCESS; } //for displaying the elements you can also use a iterator #using namespace std; vector::iterator inIter; for(inIter = reszarr.begin();inIter != reszarr.end();++inIter) cout << *inIter << endl; Atul Sonork ID : 100.13714 netdiva

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I am not supposed to include anything for this project that's why i have to create my own resizable array. so i can't use the vector class from the lib. so again. Can you tell me how do you make a resizable array? Thanks

        V N 2 Replies Last reply
        0
        • A Atul Dharne

          Use the vector class #include #include using std::vector; using std::cout; using std::endl; int main() { vector reszarr(1); //create a single element int vector //whenever you want to put in an extra value it goes on resizing reszarr.push_back(20); //here 20 is element number 2 reszarr[0] = 10; //populate the first element //To display contents of reszarr for(int count = 0;count < reszarr.size();++count) cout << "Element Number: " << count+1 << " " << reszarr[count] << endl; return EXIT_SUCCESS; } //for displaying the elements you can also use a iterator #using namespace std; vector::iterator inIter; for(inIter = reszarr.begin();inIter != reszarr.end();++inIter) cout << *inIter << endl; Atul Sonork ID : 100.13714 netdiva

          M Offline
          M Offline
          marouane miftah el kheir
          wrote on last edited by
          #4

          I am not supposed to include anything for this project that's why i have to create my own resizable array. so i can't use the vector class from the lib. so again. Can you tell me how do you make a resizable array? Thanks

          1 Reply Last reply
          0
          • L Lost User

            I am not supposed to include anything for this project that's why i have to create my own resizable array. so i can't use the vector class from the lib. so again. Can you tell me how do you make a resizable array? Thanks

            V Offline
            V Offline
            Volodya Orlenko
            wrote on last edited by
            #5

            Use realloc function.
            Example:
            char * flexBuff; flexBuff = (char *)malloc(1); // you wanted 1 element initially flexBuff[0] = 'a'; flexBuff = (char *)realloc((void *)flexBuff, 10); // flexBuff[0] still contains 'a', and you have 9 bytes more allocated Volodya Orlenko, orlenko [at] hotmail [dot] com

            1 Reply Last reply
            0
            • L Lost User

              I am not supposed to include anything for this project that's why i have to create my own resizable array. so i can't use the vector class from the lib. so again. Can you tell me how do you make a resizable array? Thanks

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              Hi Marwan Are you the same guy as marouane miftah el kheir ? Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

              A 1 Reply Last reply
              0
              • N Nish Nishant

                Hi Marwan Are you the same guy as marouane miftah el kheir ? Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

                A Offline
                A Offline
                Atul Dharne
                wrote on last edited by
                #7

                or are you'll twins?;P :-D Just kidding. Anyways it is a valid point are you'll both the same? And Marwan? Is it a shortened form of anything? Have you troubled your project giver? :) Is that the reason why he is troubling you?? Just a few questions zippedpast me... Atul

                1 Reply Last reply
                0
                • L Lost User

                  Resizable arrays? Can any one tell me how do you make a resizable array. Our Instructor wants us to start an array of size 1. and just grows as you keep adding stuff to it.(it does not have to shrink). Thanks

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  I'd recommend a class which creates an array that resizes by some sensible amount, keeps track of how much of it is used ( i.e. allocates more memory than it needs instead of allocating every time ), and then creates a new array using new when needed, and copies it across, deleting the old one which is too small. I would not use C functions such as malloc, even though new calls malloc internally. I believe that is how vector works. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001 Picture the daffodil. And while you do that, I'll be over here going through your stuff.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups