I have a class that looks something like this (Don't try to compile it,
I haven't tested this)
class KVP
{
string key;
string value;
list<KVP> sublist;
};
class MyClass
{
private:
list<KVP> list;
public:
void parseInput(void);
};
My input looks something like.
a=b (Where a is the key and b is the value.)
c=e
d=e
b=c
f=g
h=j
k=l
So the number of tabs tells me how deeply nested I am.
I entend to implement this using a stack of KVPs.
void MyClass::parseInput(KVP & current)
{
int c;
while ((c = getchar()) != EOF)
{
if (c == '\t')
parseInput(list.front().sublist);
else
{
current.key = getkey();
current.value = getvalue();// get the key and value upto and
including the newline...
}
}
}
Of course at some point (the first call to parseInput needs 'this' as a
paramter...
Question is how do i know when to return and pop up to previous level?
When do I push_back the current to the list?
Got any tips or suggestions on how to do this? 9 1463
JustSomeGuy wrote: I have a class that looks something like this (Don't try to compile it, I haven't tested this)
class KVP { string key; string value; list<KVP> sublist; };
class MyClass { private: list<KVP> list; public: void parseInput(void); };
My input looks something like.
a=b (Where a is the key and b is the value.) c=e d=e b=c f=g h=j k=l
So the number of tabs tells me how deeply nested I am. I entend to implement this using a stack of KVPs.
void MyClass::parseInput(KVP & current) { int c; while ((c = getchar()) != EOF) { if (c == '\t') parseInput(list.front().sublist); else { current.key = getkey(); current.value = getvalue();// get the key and value upto and including the newline... } } }
Of course at some point (the first call to parseInput needs 'this' as a paramter... Question is how do i know when to return and pop up to previous level? When do I push_back the current to the list? Got any tips or suggestions on how to do this?
Honestly, I don't see a language problem here. It's a general programming
problem. The only condition that brings it closer to C++ (and similar
languages) is that the language should allow recursion. So, except for
that all you need is some pseudo-code that processes your input.
The input should read an entire line. Then count the tabs and determine
whether it should attempt to add to the sublist (if there more tabs than
the current level) or return (if the number of tabs is the same or less).
This is my attempt at pseudo-code:
void KVP::process_line(current_level, next_line, source)
begin
key = get_key_from_line(next_line)
value = get_value_from_line(next_line)
while (source.ok())
next_line = source.get_next_line()
new_tabs = number_of_tabs(next_line)
if (new_tabs > current_level)
new_KVP = new KVP
sublist.add(new_KVP);
new_KVP.process_line(current_level + 1, next_line, source)
else
return
endif
endwhile
end
Do criticize, please.
V
Victor Bazarov wrote: JustSomeGuy wrote: I have a class that looks something like this (Don't try to compile it, I haven't tested this)
class KVP { string key; string value; list<KVP> sublist; };
class MyClass { private: list<KVP> list; public: void parseInput(void); };
My input looks something like.
a=b (Where a is the key and b is the value.) c=e d=e b=c f=g h=j k=l
So the number of tabs tells me how deeply nested I am. I entend to implement this using a stack of KVPs.
void MyClass::parseInput(KVP & current) { int c; while ((c = getchar()) != EOF) { if (c == '\t') parseInput(list.front().sublist); else { current.key = getkey(); current.value = getvalue();// get the key and value upto and including the newline... } } }
Of course at some point (the first call to parseInput needs 'this' as a paramter... Question is how do i know when to return and pop up to previous level? When do I push_back the current to the list? Got any tips or suggestions on how to do this?
Honestly, I don't see a language problem here. It's a general programming problem. The only condition that brings it closer to C++ (and similar languages) is that the language should allow recursion. So, except for that all you need is some pseudo-code that processes your input.
The input should read an entire line. Then count the tabs and determine whether it should attempt to add to the sublist (if there more tabs than the current level) or return (if the number of tabs is the same or less).
This is my attempt at pseudo-code:
void KVP::process_line(current_level, next_line, source) begin key = get_key_from_line(next_line) value = get_value_from_line(next_line) while (source.ok()) next_line = source.get_next_line() new_tabs = number_of_tabs(next_line) if (new_tabs > current_level) new_KVP = new KVP sublist.add(new_KVP); new_KVP.process_line(current_level + 1, next_line, source) else return endif endwhile end
Do criticize, please.
V
Yes Of course you are right I hadn't actually thought of this and not being a
c++ problem.
Where is the 'Take back stupid OT posting' button! :)
Further I wish I could have edited the posting because there are a few errors.
As it stands if you return up to the very top of the 'stack' ie the lowest
recursion level
at then end of each line then you will end up back at the right level
eventually, at which
point you can simply add to the 'end' of that list. Agreed?
JustSomeGuy wrote: Victor Bazarov wrote:
JustSomeGuy wrote: I have a class that looks something like this (Don't try to compile it, I haven't tested this)
class KVP { string key; string value; list<KVP> sublist; };
class MyClass { private: list<KVP> list; public: void parseInput(void); };
My input looks something like.
a=b (Where a is the key and b is the value.) c=e d=e b=c f=g h=j k=l
So the number of tabs tells me how deeply nested I am. I entend to implement this using a stack of KVPs.
void MyClass::parseInput(KVP & current) { int c; while ((c = getchar()) != EOF) { if (c == '\t') parseInput(list.front().sublist); else { current.key = getkey(); current.value = getvalue();// get the key and value upto and including the newline... } } }
Of course at some point (the first call to parseInput needs 'this' as a paramter... Question is how do i know when to return and pop up to previous level? When do I push_back the current to the list? Got any tips or suggestions on how to do this?
Honestly, I don't see a language problem here. It's a general programming problem. The only condition that brings it closer to C++ (and similar languages) is that the language should allow recursion. So, except for that all you need is some pseudo-code that processes your input.
The input should read an entire line. Then count the tabs and determine whether it should attempt to add to the sublist (if there more tabs than the current level) or return (if the number of tabs is the same or less).
This is my attempt at pseudo-code:
void KVP::process_line(current_level, next_line, source) begin key = get_key_from_line(next_line) value = get_value_from_line(next_line) while (source.ok()) next_line = source.get_next_line() new_tabs = number_of_tabs(next_line) if (new_tabs > current_level) new_KVP = new KVP sublist.add(new_KVP); new_KVP.process_line(current_level + 1, next_line, source) else return endif endwhile end
Do criticize, please.
V
Yes Of course you are right I hadn't actually thought of this and not being a c++ problem. Where is the 'Take back stupid OT posting' button! :) Further I wish I could have edited the posting because there are a few errors.
As it stands if you return up to the very top of the 'stack' ie the lowest recursion level at then end of each line then you will end up back at the right level eventually, at which point you can simply add to the 'end' of that list. Agreed?
Which brings me to the C++ question....
If I wish to traverse the list of lists I'm getting syntax errors...
list<KVP> & listref(list); // ie at the beggining I wish to point to the 'head'
of the list.
for (i=0; i < level; ++i) listref = listref.sublist;
This is were I am getting lost now. I can't get this to compile as it doesn't
seem to like using
refrences using templates...
JustSomeGuy wrote: [...] Which brings me to the C++ question....
If I wish to traverse the list of lists I'm getting syntax errors...
list<KVP> & listref(list); // ie at the beggining I wish to point to the 'head' of the list. for (i=0; i < level; ++i) listref = listref.sublist;
This is were I am getting lost now. I can't get this to compile as it doesn't seem to like using refrences using templates...
Well, post the actual code, let's see where you get stuck.
For me the following works OK:
#include <list>
using std::list;
struct KVP { int i; list<KVP> sublist; KVP(int i) : i(i) {} };
#include <iostream>
#include <string>
void traverse(KVP& ref, int level) {
std::cout << std::string(level, ' ') << ref.i << std::endl;
list<KVP>::iterator it = ref.sublist.begin();
while (it != ref.sublist.end())
traverse(*it++, level + 1);
}
int main() {
KVP root(42);
KVP level1_1(4201), level1_2(4202);
KVP level2_1(420101), level2_2(420202);
level1_1.sublist.push_back(level2_1);
level1_2.sublist.push_back(level2_2);
root.sublist.push_back(level1_1);
root.sublist.push_back(level1_2);
traverse(root, 0);
}
I filled the structure manually, of course, but the traversal is
recursive as you want.
V
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Mv*****************@newsread1.dllstx09.us.to. verio.net... JustSomeGuy wrote: [...] Which brings me to the C++ question....
If I wish to traverse the list of lists I'm getting syntax errors...
list<KVP> & listref(list); // ie at the beggining I wish to point to
the 'head' of the list. for (i=0; i < level; ++i) listref = listref.sublist;
This is were I am getting lost now. I can't get this to compile as it
doesn't seem to like using refrences using templates...
Well, post the actual code, let's see where you get stuck.
For me the following works OK:
#include <list> using std::list;
struct KVP { int i; list<KVP> sublist; KVP(int i) : i(i) {} };
#include <iostream> #include <string>
void traverse(KVP& ref, int level) { std::cout << std::string(level, ' ') << ref.i << std::endl; list<KVP>::iterator it = ref.sublist.begin(); while (it != ref.sublist.end()) traverse(*it++, level + 1); }
int main() { KVP root(42); KVP level1_1(4201), level1_2(4202); KVP level2_1(420101), level2_2(420202); level1_1.sublist.push_back(level2_1); level1_2.sublist.push_back(level2_2); root.sublist.push_back(level1_1); root.sublist.push_back(level1_2);
traverse(root, 0); }
I filled the structure manually, of course, but the traversal is recursive as you want.
V
Ok, this is cool...
I don't know why you declared KVP as a struct and not a class.
(Other than the fact that it won't compile when its a class.)
Why doesn't this work when I declare KVP as a class?
#include <iostream>
#include <list>
using std::list;
using std::string;
struct KVP
{
int i;
list<KVP> sublist;
KVP(int i);
};
KVP::KVP(int i) : i(i)
{
}
int main (int argc, char * const argv[])
{
int c;
KVP root;
KVP ¤t=root;
while ((c=getchar()) != EOF)
{
if (c == '\t')
current = current.sublist.begin(); // This doesn't
work... I assume because I don't have an appropriate operator= defined.
else if (c == '\n')
current = root;
else
current.i=c;
}
return(0);
}
I think this is close to working. Note also that the root isn't a list.
Here I am not using recursion as I have no way of knowing it's time to 'pop'
up a level in recursion.
This might be simple to overcome if I put a period '.' into the input stream
when the stack should be popped.
"JustSomeGuy" <no**@nottelling.com> wrote... #include <iostream> #include <list>
using std::list; using std::string;
struct KVP { int i; list<KVP> sublist; KVP(int i); };
KVP::KVP(int i) : i(i) { }
int main (int argc, char * const argv[]) { int c; KVP root; KVP ¤t=root; while ((c=getchar()) != EOF) { if (c == '\t') current = current.sublist.begin(); // This doesn't work... I assume because I don't have an appropriate operator= defined.
'current' is a reference. If you are attempting to make it refer to
a differnt object, here is some news: it can't be done. You may need
to rethink 'current'.
else if (c == '\n') current = root; else current.i=c; } return(0); }
I think this is close to working. Note also that the root isn't a list.
Nothing _is_ a list. However, the 'root' and other KVP objects
_have_ lists.
Here I am not using recursion as I have no way of knowing it's time to 'pop' up a level in recursion.
Take my pseudo-code, loot at it well and translate into C++. Even if
it needs fixing, it is probably fairly close to what you need as far
as recursion is concerned.
This might be simple to overcome if I put a period '.' into the input stream when the stack should be popped.
Whatever floats your boat.
Good luck!
V
"JustSomeGuy" <no**@nottelling.com> wrote in message news:<cn**********@news.ucalgary.ca>... #include <iostream> #include <list>
using std::list; using std::string;
struct KVP { int i; list<KVP> sublist;
The definition of `sublist' is undefined behaviour because
KVP is not completly defined at this point.
It works by accident only.
KVP(int i); };
KVP::KVP(int i) : i(i) { }
int main (int argc, char * const argv[]) { int c; KVP root; KVP ¤t=root;
You are using reference here ...
.... every assignment to `current' will write to `root'!
I guess you should use
KVP* current;
instead.
while ((c=getchar()) != EOF) { if (c == '\t') current = current.sublist.begin(); // This doesn't work... I assume because I don't have an appropriate operator= defined.
BTW: what if current.sublist is empty?
current = *(current.sublist.begin()); else if (c == '\n') current = root;
.... this is always a self-assignment, most-likley `root' is no more
what it once was!
else current.i=c; } return(0); }
[snip]
regards,
Stephan Brönnimann br****@osb-systems.com
Open source rating and billing engine for communication networks.
Stephan Br?nnimann wrote: "JustSomeGuy" <no**@nottelling.com> wrote in message news:<cn**********@news.ucalgary.ca>...
#include <iostream> #include <list>
using std::list; using std::string;
struct KVP { int i; list<KVP> sublist;
The definition of `sublist' is undefined behaviour because KVP is not completly defined at this point. It works by accident only.
It's not a definition. It's a declaration. When the user needs to
create an object of type 'KVP' it will have been completely defined.
[..]
V This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: cfanatic |
last post by:
Hey all, I been reading through these forums for a long time but have
never posted. Well, I got a dillema. I have a program of the Eight
Queen's program and I have to make it work without...
|
by: chandra.somesh |
last post by:
I recently was having trouble converting implementaion of recursions
using stack.
While single level of recursions are quite intuitive , it is when we
have more than one recursive calls in the...
|
by: jw |
last post by:
what is the relation between a stack and recursion func.
and if i have recursion funct(a return type void),
void print(int n,int base){
static string digits="0123456789abcdef";
if(n>=base){...
|
by: rover8898 |
last post by:
Hello all,
I used setjmp() in a recent of program of mine (it is not completed, so
I have not the chance to test it out yet). I am not very profocient in
C coding as are some of my co-workers....
|
by: gaoxtwarrior |
last post by:
a sort which is stable means it keeps the object's original order. A sort
which is in place is stable. does anyone can explain me what is sort in
place?
thx.
|
by: elventear |
last post by:
Hello everyone,
I am runing into recursion limit problems. I have found that the
culprit was related to the __hash__ function that I had assigned to
the objects that were added to a set.
...
|
by: lielar |
last post by:
Hi
I'm writing a javascript class object that creates objects of itself.
Take for example
function One(x){
this.x = x;
}
One.prototype.addOne = function(x) {
|
by: cerise |
last post by:
I can't figure out how to make and handle multiple stacks and use them so I could create four linked list stacks representing each suit of cards, one stack each for diamonds, hearts, spades, and...
|
by: Andrew Morton |
last post by:
I'm recursing through the controls in an asp.net page to find if any TextBox
contains text.
JOOI, can I break out of the recursion as soon as it's found one, or does it
have to unwind itself?
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |