Question:
How do you have an array with no limits in C++?
arabourn@sbcglobal.net
2015-11-17 11:17:23 UTC
ASSIGNMENT:

Create a program to do the following:

1) Read in names to sort until the user types the “enter” key as the first character of a string (the maximum number of names is 20, there is not a maximum length to a name).
2) After sorting and displaying the array of strings, ask the user for a string to search for (again, no maximum length).
3) Do a binary search to find if the string is in the array
4) If the string is in the array, tell the user the index of the string.
5) If the string is not in the array, tell the user that it is missing.
6) Repeat items 3 thru 5 until the user enters the “enter” key as the first character of the string, at which time the program will complete.

BONUS: A bonus of 20 points if you create the program in such a way that there is no limit on the number of names to be handled.


** I'm trying to achieve the bonus
Three answers:
_Object
2015-11-17 12:30:44 UTC
Short answer: Use `std:: vector <>'.

Edit: You need to sort these: forget `vector', use `std:: multiset <>'. `multiset' is sorted by default and otherwise shares most of the properties of `vector'. Note that while @Quentin's answer provides case-insensitive sorting, you can get case-sensitive sorting by default.



Longer answer:

You can keep track of the number of elements in the array and resize it as more space is required. You would do this by allocating "`new []'-ing" an array so many elements larger than the current size, copying the data from the old array to the new array, and then "`delete []'-ing" the old.



There is a C function `realloc' that is designed for this purpose, but you should not use it in C++ code. It is incompatible with `new[]' and `delete[]' anyways: you'd have to switch to `malloc / free'.



The "correct" answer would be to use the library component std:: vector. `vector' allows for amortized constant insertion by increasing it's capacity exponentially --- doubling the array size each time more is needed. That makes it so that you don't need to bother with dynamic allocation at all, nor the size of the array at all. You should prefer std:: vector over arrays all the time.
?
2015-11-17 12:40:15 UTC
When the user enters another name but the array is full, copy the names into a new array of a larger size.



if(numnames == arraysize){

arraysize += 20;

temp = new int[arraysize];

//copy

delete namesarray;

namesarray = temp;

}
?
2015-11-17 11:27:22 UTC
use a vector.



vector words;



Did

string words = new words[number];

compile OK. It looks as if it won't. If it doesn't try

string* words = new string[number];





then with inputStrings

cin >> astring;

words.push_back(astring); // this increases the array as much as req'd



later if you want to set number:-

number = words.size();



To sort a vector of strings case insensitively, there is a built in sort function already there for you to use:-



void sorter(vector& v){

locale loc;

sort (begin(v), end(v), [loc](const string&a, const string&b){

return lexicographical_compare(begin(a), end(a), begin(b), end(b), [loc](char x, char y){

return toupper(x,loc) < toupper(y,loc);

});

});

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...