no more debugging FAQ.
-
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
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
-
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
-
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
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
-
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
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 -
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
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
-
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
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
-
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
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.