M
Milan Cermak
Hi all,
I'm developing my own basic classes library (http://stlib.sf.net for
those how are interested) and I'd like to compare its power to STL.
Unfortunately I'm not familiar with STL - it blows up my mind everytime
I try to understand it.
Could you, please, show me the code implementing following functionality?
Consider two strings that contains only digits. These strings are 7 to
15 digits long. (They are telephone numbers read from text file.)
These numbers are upper and lower limit of interval. How can I get a
collection containing all numbers from this interval (including limits)?
Resulting numbers should be also strings (textual representation of the
number).
Hint: The collection has maximally 100 items.
I can do this in Smalltalk (which is the simpliest implementation):
first := '420736300370'.
last := '420736300399'.
result := (first asNumber to: last asNumber)
collect: [:num | num printString].
I can do this also in C++ with my own library. I only need to get rid of
very long numbers which I didn't implement yet. (That's where the hint
becomes usefull.)
Set *generateRoutesFromInterval(String *first, String *last)
{
Set *result;
int firstUncommonIndex;
String *commonPrefix;
unsigned long firstNumber, lastNumber;
result = new Set;
for (firstUncommonIndex = 0;
firstUncommonIndex < first->size();
firstUncommonIndex++) {
if (!first->at(firstUncommonIndex)->isEqual(last->at(firstUncommonIndex)))
break;
}
commonPrefix = dynamic_cast<String *>(first->copy(0,
firstUncommonIndex));
firstNumber = dynamic_cast<String *>(first->copy(firstUncommonIndex,
first->size()))->asNumber()->asUnsignedLong();
lastNumber = dynamic_cast<String
*>(last->copy(firstUncommonIndex,
last->size()))->asNumber()->asUnsignedLong();
for (; firstNumber <= lastNumber; firstNumber++) {
char buffer[first->size()];
sprintf(buffer, "%lu", firstNumber);
result->add(&(*commonPrefix + buffer));
}
return result;
}
Could this be done with STL? How?
Thanks to all who will spend their time on this task.
Milan Cermak
I'm developing my own basic classes library (http://stlib.sf.net for
those how are interested) and I'd like to compare its power to STL.
Unfortunately I'm not familiar with STL - it blows up my mind everytime
I try to understand it.
Could you, please, show me the code implementing following functionality?
Consider two strings that contains only digits. These strings are 7 to
15 digits long. (They are telephone numbers read from text file.)
These numbers are upper and lower limit of interval. How can I get a
collection containing all numbers from this interval (including limits)?
Resulting numbers should be also strings (textual representation of the
number).
Hint: The collection has maximally 100 items.
I can do this in Smalltalk (which is the simpliest implementation):
first := '420736300370'.
last := '420736300399'.
result := (first asNumber to: last asNumber)
collect: [:num | num printString].
I can do this also in C++ with my own library. I only need to get rid of
very long numbers which I didn't implement yet. (That's where the hint
becomes usefull.)
Set *generateRoutesFromInterval(String *first, String *last)
{
Set *result;
int firstUncommonIndex;
String *commonPrefix;
unsigned long firstNumber, lastNumber;
result = new Set;
for (firstUncommonIndex = 0;
firstUncommonIndex < first->size();
firstUncommonIndex++) {
if (!first->at(firstUncommonIndex)->isEqual(last->at(firstUncommonIndex)))
break;
}
commonPrefix = dynamic_cast<String *>(first->copy(0,
firstUncommonIndex));
firstNumber = dynamic_cast<String *>(first->copy(firstUncommonIndex,
first->size()))->asNumber()->asUnsignedLong();
lastNumber = dynamic_cast<String
*>(last->copy(firstUncommonIndex,
last->size()))->asNumber()->asUnsignedLong();
for (; firstNumber <= lastNumber; firstNumber++) {
char buffer[first->size()];
sprintf(buffer, "%lu", firstNumber);
result->add(&(*commonPrefix + buffer));
}
return result;
}
Could this be done with STL? How?
Thanks to all who will spend their time on this task.
Milan Cermak