473,379 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to code this with STL?

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

Jul 22 '05 #1
4 1368
"Milan Cermak" <mcermak_@_chello_._cz> wrote in message
news:Mi****************@news.chello.at...
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?
One possible implementation would be to define an iterator class like so:

// the following code is incomplete and untested
// just to demonstrate the basic idea

class walk_str_iterator
{
public:
walk_str_iterator(std::string s) : wsi(s) {}
bool operator==(const walk_str_iterator& x) const { return wsi == x.wsi; }
bool operator!=(const walk_str_iterator& x) const { return wsi != x.wsi; }
walk_str_iterator operator*() { return wsi; }
walk_str_iterator& operator++() {
// ...place here some of your code from the
// generateRoutesFromInterval() function to calculate the next
position...
}
private:
std::string wsi;
}

class print_wsi {
public:
void operator() (const walk_str_iterator& w) { std::cout << *w <<
std::endl; }
}

// ===========================
// you could then use the STL like so...

int main()
{
walk_str_iterator first ("420736300370");
walk_str_iterator last ("420736300399");
std::for_each(first, last, print_wsi() );
}

Thanks to all who will spend their time on this task.
Milan Cermak

Jul 22 '05 #2
On Sun, 06 Jun 2004 08:09:16 GMT, Milan Cermak <mcermak_@_chello_._cz>
wrote:
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.)
I think it would be more efficient to convert the input into integers
(perhaps long long or __int64 would be better, you can go to 18 digits
that way, I believe), build the collection and convert the numbers
back into strings instead of the way you propose. I would create a
class TelephoneNumber which could take care of bounds checking and
perhaps store the string representation and the actual number as
member data. Use std::vector<TelephoneNumber> or
std::list<TelephoneNumber> to hold the collection.
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()]; ^^^^^^^^^^^^^

This will not compile -- you cannot declare arrays dynamically in C++.
You need to use "new char[...]" (or std::string) instead.
sprintf(buffer, "%lu", firstNumber);
result->add(&(*commonPrefix + buffer));
}
return result;
}

Could this be done with STL? How?
You can do everything above with std::string.
Thanks to all who will spend their time on this task.
Milan Cermak


--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #3
Bob Hairgrove wrote:
char buffer[first->size()];

^^^^^^^^^^^^^

This will not compile -- you cannot declare arrays dynamically in C++.
You need to use "new char[...]" (or std::string) instead.


Fortunately it does. Using GCC 2.95.4. May be I'll need to fix it when
I'll move to GCC 3.2+.

Milan Cermak

Jul 22 '05 #4
Milan Cermak wrote:
Bob Hairgrove wrote:
char buffer[first->size()];

^^^^^^^^^^^^^

This will not compile -- you cannot declare arrays dynamically in
C++. You need to use "new char[...]" (or std::string) instead.


Fortunately it does. Using GCC 2.95.4. May be I'll need to fix it when
I'll move to GCC 3.2+.

Milan Cermak


But it's not standard C++, the topic of this group. Please leave your
compiler-specific extensions out of here.

A standard solution:

#include <vector>
....
std::vector<char> buffer(first->size());
....

- Pete
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
15
by: Enzo | last post by:
Hi Ng, It's possible to protect the source code of a js file? With PHP? Thanks in advance! Enzo
3
by: masood.iqbal | last post by:
In this day and age, you never say no to any work that is thrown at you ---- so when I was offered this short-term contract to convert legacy C code to C++, I did not say no. Personally I believed...
3
by: Sandy | last post by:
Hi, I am reading a book of C++ templates, it says the template code must be available in order to initialize it for a particular type. So my doubt is: What about STL source code, is it shipped...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.