tinyxml2 XMLElement constructor private? - tinyxml2

In TinyXml you could create an Element e.g. TiXmlElement("tag"),
but in TinyXml2 there is no public constructor for XMLElement?
How do you create elements ?

Similar to the existing answer, I wrote this helper utility for my application:
tinyxml2::XMLElement* CChristianLifeMinistryEntry::InsertNewElement(tinyxml2::XMLDocument& rDoc, tinyxml2::XMLElement*& pParent, LPCSTR strElement, CString strValue)
{
XMLElement *pElement = rDoc.NewElement(strElement);
USES_CONVERSION;
if (pElement == nullptr)
AfxThrowMemoryException();
pElement->SetText(CT2CA(strValue, CP_UTF8));
pParent->InsertEndChild(pElement);
return pElement;
}
It automatically adds a new child element to the end of the list. In addition, it sets the text value for the element.

You create an element within the context of the document, so call
tinyxml2::XMLElement * tinyxml2::XMLDocument::NewElement (const char * name).
E.g. to create a new element and add it as a child of an existing element e
XMLElement * new = e -> GetDocument() -> NewElement ("tag");
e -> InsertFirstChild (new);
Or, to do it in a single step, you could look up append_element in my tinyxml2 extension

Related

Implement phone directory using two tries

I have encountered an interview question
“Implement a phone directory using Data Structures”
I want to solve it using tries.By solving it with tries,I tried using two tries,one for name and another for phone number,
but I faced a difficulty .
Suppose ,I have to add three entries( AB “112” BC ”124” CD ”225”)
Then if I query the name for number “225”,how do I return CD.
that is,how these two tries will be linked .
One approach I was thinking was taking two pointers in both the tries.
These pointers will point to the first and last word in the other trie.
For example,if the structures are as follows:
Struct nametrie
{
Struct nametrie *child[26];
struct phonetrie*head,*tail;
struct phonetrie*root;
-----------
}
Struct phonetrie
{
struct phonetrie*child[9];
struct nametrie*head,*tail;
struct nametrie*root;
-----------
}
Then for AB “112”,
Name trie willstore head(1) and tail (2).
But I think this approach will not work for duplicate entries(one name and multiple numbers.)
Can someone please explain a good approach.I am not looking for code but good understanding of approach,may be via diagram or algorithm.
I dont know C so I cant comment in your code.
The idea of using tries is valid.
you seems to be missing what data the nodes can hold in tries
the node in trees has 2 main components
the data it has which can be anytype
list of childen (or left , right childeren) or any combination of children
what we will do here is that we will add another field to each node and call it the value "theValue"
So the trie node will look like this
Class TrieNode{
public char theChar;
public String theValue;
public List<TrieNode> children;
}
So for forward lookup (name to phone) you construct one Trie and on the node that match entry in the directory you will set theValue to that entrie.
you will need to create 2nd trie to do the same for reverse lookup (phone to name)
So to give you example how it will look like for this data it will be
( AB “112” AC ”124” ACD ”225”)
//create nodes
TrieNode root = new TrieNode();
TrieNode A = new TrieNode();
A.theChar = 'A';
TrieNode B = new TrieNode();
A.theChar = 'B';
TrieNode C = new TrieNode();
A.theChar = 'C';
TrieNode C2 = new TrieNode();
A.theChar = 'C';
TrieNode D = new TrieNode();
A.theChar = 'D';
//link nodes together
root.children = new ArrayList<>();
root.children.add(A);
A.children = new ArrayList<>();
A.children.add(B);
A.children.add(C);
B.children = new ArrayList<>();
B.children.add(C2);
//fill the data
B.theValue = "112";
C.theValue = "124";
C2.theValue = "225";
now you can easy traverse this Trie and when you reach a node and whant to check the value just read theValue
i hope it is clear

can we perform binary search on linked lists?

there was a reputed site which claimed that we cannot do binary search on linked list but I know for a fact that we can perform merge sort( which uses divide and conquer just like binary search) on a linked list so we must be able to perform binary search on a linked list as well right??
Purely in the abstract, a linked list does not support direct indexing. The only way to get to the third element is by starting at the first element, following the next link to the second element, then following it's next link to the third element.
If the implementation allows for indexing (e.g., java.util.LinkedList), it is possible to implement a binary search by calling get(index) when you need an element. If the underlying data structure is a simple linked list without an auxiliary lookup structure, then this will perform very poorly. As an example, the following is the code from the OpenJDK java.util.LinkedList.get method.
package java.util;
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
transient int size = 0;
transient Node<E> first;
transient Node<E> last;
public E get(int index) {
return node(index).item;
}
Node<E> node(int index) {
if (index < (size >> 2)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
}
When list.get(5) is called to get the sixth element in a list of more than 11 elements, it iterates over the first five elements before returning the sixth. The generic interface provides indexed access into the sequence but makes no guarantees on its performance.

Linq to split/analyse substrings

I have got a List of strings like:
String1
String1.String2
String1.String2.String3
Other1
Other1.Other2
Test1
Stuff1.Stuff1
Text1.Text2.Text3
Folder1.Folder2.FolderA
Folder1.Folder2.FolderB
Folder1.Folder2.FolderB.FolderC
Now I would like to group this into:
String1.String2.String3
Other1.Other2
Test1
Stuff1.Stuff1
Text1.Text2.Text3
Folder1.Folder2.FolderA
Folder1.Folder2.FolderB.FolderC
If
"String1" is in the next item "String1.String2" I will ignore the first one
and if the second item is in the third I will only take the third "String1.String2.String3"
and so on (n items). The string is structured like a node/path and could be split by a dot.
As you can see for the Folder example Folder2 has got two different Subfolder items so I would need both strings.
Do you know how to handle this with Linq? I would prefer VB.Net but C# is also ok.
Regards Athu
Dim r = input.Where(Function(e, i) i = input.Count - 1 OrElse Not input(i + 1).StartsWith(e + ".")).ToList()
Condition within Where method checks if element is last from input or is not followed by element, that contains current one.
That solution uses the fact, that input is List(Of String), so Count and input(i+1) are available on O(1) time.
LINQ isn't really the correct approach here, because you need to access more than one item at a time.
I would go with something like this:
public static IEnumerable<string> Filter(this IEnumerable<string> source)
{
string previous = null;
foreach(var current in source)
{
if(previous != null && !current.Contains(previous))
yield return previous;
previous = current;
}
yield return previous;
}
Usage:
var result = strings.Filter();
Pretty simple one. Try this:
var lst = new List<string> { /*...*/ };
var sorted =
from item in lst
where lst.Last() == item || !lst[lst.IndexOf(item) + 1].Contains(item)
select item;
the following simple line can do the trick, I'm not sure about the performance cost through
List<string> someStuff = new List<string>();
//Code to the strings here, code not added for brewity
IEnumerable<string> result = someStuff.Where(s => someStuff.Count(x => x.StartsWith(s)) == 1);

Java ME ArrayList - Vector - Define object types and access object methods through vector

I'm making a shopping list mobile application (Java ME) and i have two classes; item, list.
item object allows get/set name and quantity (string itemName, int quantity)
Now i need to store an array of items in my list class and be able to access the methods of the object from its list array index as follows; code below is pseudo code
item[] itemList = new item[]
for(int x = 0; x < itemList.length; x++)
{
String tempStoreOfName = itemList[x].getItemName()
System.out.println(tempStoreOfName)
}
I've googled a lot and found out that you can use vectors however i cannot seem to be able to call the object's methods. Where am i going wrong?
I've done something like this in C# and i used ArrayLists however these are not supported in Java ME
Current Code
Vector itemList = new Vector();
for(int x = 0; x <= itemList.size(); x++)
{
dataOutputStream.writeUTF(itemList.elementAt(x)*here i cannot put .getItemName()*);
}
Since you can't use generics you have to cast so that Java knows what you got out of the Vector. Notice that Vector.elementAt() returns Object? all you can do with it is treat it like an Object:
item myItem = itemList.elementAt(n);
fails because java can't auto-cast to a more specific class. You'd have to use:
Object myItem = itemList.elementAt(n);
which is useless to you because you want an item, not an Object.
You have to cast it to an object of the type you want:
for(...)
{
item myItem = (item) itemList.elementAt(n);
myItem.method();
}
From then on you just use myItem.

Problem with QVariant/QTreeWidgetItem/iterator on qt4.4.3

In my qt app I have this object, filled before setting up my QTreeWidget's content:
QList<QTreeWidgetItem*> items;
I fill the QList by this way:
QVariant qv; // I need this for "attaching" to the item my linuxPackage object
qv.setValue(linuxPackage);
packRow->setData(1, Qt::UserRole,qv); // packRow is my own object inherited from QTreeWidgetItem, I "put" the QVariant into it
items.append(packRow); // then I put my item into the QList
at the end of the work, my QList has almost 1000 items.
I need to iterate over them and for each item I need to get the "linuxPackage" data by this (tested and working) way:
Pkg linuxPackage = this->data(1,Qt::UserRole).value<Pkg>(); // Pkg is my own class for the linuxPackage object
So, I've been trying to extract needed data by this manner:
QList<QTreeWidgetItem*>::iterator iter;
for (iter = items.begin(); iter != items.end(); ++iter){
Pkg pack = iter->data(1,Qt::UserRole).value<Pkg>();
}
But nothing works, I can not even get the program compiling. Help! :D
Perhaps:
(*iter)->data(1,Qt::UserRole).value<Pkg>();
BTW, an easier way of doing this with Qt4:
foreach (const QTreeWidgetItem *item, items) {
Pkg pack = item->data(1,Qt::UserRole).value<Pkg>();
}
at the very least, you should use const_iterators =)
QList<QTreeWidgetItem*>::const_iterator iter;
for (iter = items.constBegin(); iter != items.constEnd(); ++iter){
...
}

Resources