All STL containers (and a lot of non-STL ones) would allow you to insert
unique values. Most of them will also allow you to insert non-unique
ones. Somehow, I don't think that was the question you really wanted to
ask.
> can u use map in a link list, when allocate memory to link list i can
> insert some values in map
I'm not sure I understand this question. If you are asking whether you
can insert the same element into a linked list and also into a map, then
yes, generally you can.
Describe the problem you are trying to solve, rather than your attempted
solution.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
I think you are asking for a linear container that enforces uniqueness of inserted elements. However, is there a reason why you
cannot just use a map instead of a list? You can iterate a map.
>>All STL containers (and a lot of non-STL ones) would allow you to insert
>>unique values.
Please tell me some name.
Scot T Brennecke" <Sc...@Spamhater.MVPs.org> wrote in message
news:e1BdN%23UOKH...@TK2MSFTNGP06.phx.gbl...
I have a tree/link list which structure is like
struct tree
{
int id;
char *name;
std::map<> *mapobject;
struct tree *left;
struct tree * right;
struct tree * child;
};
struct tree * treeobject;
If i allocate memory to treeobject
treeobject = malloc(sizeof(struct tree));
my motive is to insert more than one item in treeobject->mapobject. I think
we can use map pointer in "
struct tree" and allocate as much data as we want.
"Scot T Brennecke" <Sc...@Spamhater.MVPs.org> wrote in message
news:%23WNYgpo...@TK2MSFTNGP06.phx.gbl...
This doesn't compile, as the template parameters to std::map are missing.
Also, why are you using a 'char*' instead of a std::string? And why a
pointer to a map object instead of a map object? And why the
repeated 'struct' here? In C++, you don't need that, as opposed to C.
> struct tree * treeobject;
> If i allocate memory to treeobject
> treeobject = malloc(sizeof(struct tree));
Oh dear %DEITY% no! Please stop using malloc(), as it won't call any
constructor. Worse, this doesn't even compile because malloc() returns a
void pointer and you are assigning it to a tree pointer. However, even
using new, you would be faced with the task of properly releasing memory,
so I'd rather not use any dynamic allocation at all or at least use smart
pointers.
> my motive is to insert more than one item in treeobject->mapobject.
That object is (if I guess from your above broken code correctly) a pointer.
You cannot insert into a pointer.
> I think we can use map pointer in "struct tree" and allocate as much
> data as we want.
A std::map (or, basically any C++ object) doesn't care where it is stored,
its behaviour doesn't change with the surrounding entity or the way it is
allocated.
Ashish, I'm afraid that your understanding of C++ is extremely bad and mixed
with some (possibly equally bad) understanding of C. You must learn some
basics first, and for that you need a good book or tutorial. Note that C
and C++ are very different in their use.
Also, you are not supplying the relevant information. Who cares about
the 'id' or 'name' fields of above struct? You maybe, but for asking a
question, that information is best left out, unless it is necessary.
Lastly, you can freely mix any of the standard containers with each other.
All containers are also valid values for a container, because they are
freely copyable and assignable. So yes, you can create a list of maps or a
map of lists.
Uli
--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
typedef struct tree
{
int id;
std::string name;
std::map<> mapobject; // object not pointer
struct tree *left;
struct tree * right;
struct tree * child;
struct tree * parent;
}MyTree;
MyTree * treeobject = (MyTree*)malloc(sizeof(struct tree));
or i can use
MyTree * treeobject = new MyTree;
the total memory for treeobject (as per my knowledge) in win32 application
4(id) + 16(size of std::string class) + 16(size of std::map class) + 4(left)
+ 4(right) + 4(child) + 4(parent)
Now to insert any data in map
treeobject ->mapobject.insert(...).
I did not use map before, i thought without using map pointer we can not use
map in a tree(above structure) where we allocate memory, thats why i put a
query in this thread. I was not aware that when we insert data in mapi it's
std::map class headeche which allocate run time memory and it allocate b'se
mapobject contains an address not a value.
And why i cant use chatacter pointer (char* name) in above structure. I can
allocate memory to it as i want. If i have a very big string(around 1000
character) then will std::string works successfully.
> Ashish, I'm afraid that your understanding of C++ is extremely bad and
> mixed
> with some (possibly equally bad) understanding of C. You must learn some
> basics first, and for that you need a good book or tutorial. Note that C
> and C++ are very different in their use.
thanks i'll do that
"Ulrich Eckhardt" <eckh...@satorlaser.com> wrote in message
news:5qtjo6-...@satorlaser.homedns.org...
> Gesch�ftsf�hrer: Thorsten F�cking, Amtsgericht Hamburg HR B62 932
'typedef struct' is C-style, and is never necessary in C++. Just do:
struct MyTree
> {
> int id;
> std::string name;
> std::map<> mapobject; // object not pointer
> struct tree *left;
MyTree *left;
is fine with the above change.
> struct tree * right;
> struct tree * child;
> struct tree * parent;
> }MyTree;
Generally, in idiomatic C++ coding you rarely use the 'struct' keyword,
and rarely have any public member variables, and instead use classes
with member functions. You would have a constructor to create the
object. This makes it easier to separate the code implementing the tree
from the code using it, and will make it easier to change your structure.
>
> MyTree * treeobject = (MyTree*)malloc(sizeof(struct tree));
> or i can use
> MyTree * treeobject = new MyTree;
You must use the latter - the former will not initialize mapobject
correctly.
> the total memory for treeobject (as per my knowledge) in win32 application
> 4(id) + 16(size of std::string class) + 16(size of std::map class) + 4(left)
> + 4(right) + 4(child) + 4(parent)
+ possibly some fixed padding. But this is just the direct memory - the
string object and map object will allocate additional memory from the
heap to store their data.
>
> Now to insert any data in map
> treeobject ->mapobject.insert(...).
>
> I did not use map before, i thought without using map pointer we can not use
> map in a tree(above structure) where we allocate memory, thats why i put a
> query in this thread. I was not aware that when we insert data in mapi it's
> std::map class headeche which allocate run time memory and it allocate b'se
> mapobject contains an address not a value.
This is how all properly written objects work - they manage their own
memory without you having to worry about it. However, you have to make
sure that you use 'new' to allocate the object containing the map, so
that it is correctly initialized, and 'delete' to destroy that object,
otherwise you will get a memory leak. However, you can avoid using
'delete' directly if you use a smart pointer class.
> And why i cant use chatacter pointer (char* name) in above structure. I can
> allocate memory to it as i want. If i have a very big string(around 1000
> character) then will std::string works successfully.
Of course - std::string just simplifies your code by managing the memory
automatically. I'd recommend reading a modern C++ book, such as
Accelerated C++ http://www.acceleratedcpp.com/. You might be able to
find some decent online tutorials for the C++ Standard Library
(sometimes erroneously called the STL).
Tom
*sigh*
Again, why the repeated 'struct', why use this old, obsolete,
C-style 'typedef struct ...', where are the template parameters, for what
do you need the 'name' and 'id' in your example?
> MyTree * treeobject = (MyTree*)malloc(sizeof(struct tree));
Again: This does not work, because it doesn't call any constructor.
> I did not use map before, i thought without using map pointer we can not
> use map in a tree(above structure) where we allocate memory, thats why i
> put a query in this thread.
std::map doesn't care where you use it. However, if you use an object with a
constructor, you must (No exceptions!) invoke its constructor. 'malloc()'
will not do this for you, it only allocates raw memory. 'new' does.
> I was not aware that when we insert data in mapi it's std::map
> class headeche which allocate run time memory and it
> allocate b'se mapobject contains an address not a value.
I'm not sure what you are saying here. If I guess right, then your question
is who manages the memory inside the map. The answer to that is that it is
the particular map object. It allocates memory as it needs and releases the
memory in its destructor.
> And why i cant use chatacter pointer (char* name) in above structure. I
> can allocate memory to it as i want. If i have a very big string(around
> 1000 character) then will std::string works successfully.
It's not the case that you can not use a 'char*' but that you shouldn't,
because it puts various burdens on you concerning the use of that pointer.
For example, you must allocate sufficient amounts of memory in order to
store strings there. You also have to release that memory when it isn't
used any more, as C++ doesn't have a garbage collection that makes the
memory (re-)usable for other parts of the program. Further, you must
initialise it with e.g. NULL to even be able to distinguish it from an
allocated string.
std::string takes many of these tasks away from you and does so with very
little overhead. Further, std::string perfectly well handles strings of
various lengths, it manages its own memory dynamically.
Uli
--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Well i did not need to allocate/free memory at run time. I know
stl(vector,string,map) are really beneficial. But i needed to use tree and
allocate memory because of requirement (hierarchical format which is
supported by tree).
i hope following code will not give any error and run successfully always
struct tree
{
std::string name;
int id;
std::map<> mapobject;
struct tree *left;
struct tree * right;
struct tree * child;
struct tree * parent;
};
//create & allocate memory
struct tree * mytreeptr = new struct tree;
initialize each member in mytreeptr
now call
delete mytreeptr ; (suppose left,right,child,parent has NULL value).
mytreeptr = NULL;
does STL also provides tree like structure (hierarchical). As per my
knowledge i did not find any.
well id and name i'm uisng because each directory contains an id and a name.
"Ashish" <akohl...@hotmail.com> wrote in message
news:eiK75aq...@TK2MSFTNGP06.phx.gbl...
Did you write a constructor and destructor for tree? You should....