Boost Ptree Custom Sort - c++11

I want to use this Function and make a custom sorting function, but I don't know how the Compare Function works or with a lambda.
Could someone be so kind to give me a working example for using it.
/* Sorts the direct children of this node according to the predicate.
The predicate is passed the whole pair of key and child. */
template <class Compare> void sort (Compare comp);
The predicate is passed the whole pair of key and child.
Is this a pair of key/ptree? I don't know how what the datatype is.
Im searching something like a lambda function equal for lists
unorderedGenList.sort([](const boost::property_tree::ptree & treeA, const boost::property_tree::ptree & treeB)
{
if(std::stod(treeA.get<std::string>("sortA","0")) < std::stod(treeB.get<std::string>("sortA","0"))
|| (std::stod(treeA.get<std::string>("sortA","0")) == std::stod(treeB.get<std::string>("sortA","0")) && std::stod(treeA.get<std::string>("sortB","0")) < std::stod(treeB.get<std::string>("sortB","0")))
)
return true;
return false;
});

I think i got the Solution
typedef std::pair< const Key, self_type > value_type;
unorderedPtree.sort([](const boost::property_tree::value_type& treeA, const boost::property_tree::value_type & treeB){
treeA.second.get<std::string>("sortB","0")) ...
}

Related

I have a set of user defined dataType and using that set variable in a structure. I want to compare the values of set. How will I do that

using setIp = set<ipv4address>;
struct Test {
setIp IP1;
setIp IP2;
}
IP1 and IP2 can have more than 1 ipaddress.
I want to use find function on setIp and for that need operator <. How would I overload it.
You can do this:
bool operator<(const ipv4address& left, const ipv4address& right) {
return left.TODO < right.TODO;
}
The TODO is there because I don't know what the members of your ipv4address are, but I'm sure you can figure that part out. If your comparison needs to consider multiple members (for example if you store ipv4address as four separate octets), use std::tie:
bool operator<(const ipv4address& left, const ipv4address& right) {
return std::tie(left.o1, left.o2, left.o3, left.o4)
< std::tie(right.o1, right.o2, right.o3, right.o4);
}
That means "if left.o1 is less than right.o1, left is less than right" and so on.
Either way, the operator needs to be declared before you declare your std::set<ipv4address>.

Cannot understand C++ map semantics

I don't understand the constructor statement in the following code. How can the iterator to the past-of-end element be added to the map as a key?
template<typename K, typename V>
class my_map {
std::map<K,V> m_map;
public:
my_map( V const& val) {
m_map.insert(m_map.end(),std::make_pair(std::numeric_limits<K>::lowest(),val));
}
};
How can the iterator to the past-of-end element be added to the map as a key?
It's not the key. It's the position of the insertion. By passing end you're saying append to the map.
The key that you're inserting is the first part of the pair. i.e. std::numeric_limits<K>::lowest().
The value that you're inserting is the second part of the pair. i.e. val.
The docs for std::map::insert are useful.
How can the iterator to the past-of-end element be added to the map as a key?
That's an incorrect conclusion. std::map::insert has several overloads. The one that is use in your call is:
iterator insert( iterator hint, const value_type& value ); // Overload 4
which does the following:
Inserts value in the position as close as possible, just prior, to hint.

Find element that goes before target with C++ set

I know there's lower_bound and upper_bound method which both find first element that doesn't go before target element, include and exclude the target. But I need a method that can find the last element that goes before target. Is there such method or easy way to do this using set?
Thanks!
You could just decrement the lower bound?
In general you need to check if the lower bound yields the begin() iterator, in which case the element you mention doesn't exist.
Here's some example code (untested, just to give the idea):
template<typename T>
std::set<T>::iterator get_last_before(std::set<T> & s, const T & t) {
auto it = s.lower_bound(t);
if (it == s.begin()) { throw std::runtime_error(); }
return --it;
}

C++/CX way of iterating Map<String^, Object^>^?

I have a object of type of Map<String^, Object^>^. How do I iterate in C++/CX way? I am trying to use iterator but I am not clear about syntax. Documentation doesn't provide an example.
C++/CX collections follow the same principles as c++ collections, so they have iterators and begin, end functions.
IMap<Platform::String^, Platform::Object^>^ map = ref new Map<Platform::String^, Platform::Object^>();
map->Insert("key1", "val1");
map->Insert("key2", 2.0f);
// Exactly like you would iterate over a map, but instead of std::pair you have IKeyValuePair
std::for_each(begin(map), end(map), [](IKeyValuePair<Platform::String^, Platform::Object^>^ pair)
{
// do stuff
auto key = pair->Key;
auto value = pair->Value;
});
for( auto pair : map )
{
// do stuff
auto key = pair->Key;
auto value = pair->Value;
}
Also, don't forget to include collection.h and use namespace Platform::Collections.

Print a simply linked list backwards with no recursion, in two passes at most, using constant extra memory, leaving it intact

You must print a simply linked list backwards:
Without recursion
With constant extra memory
In linear time
Leaving the list intact
Added Later Two passes at most
Invert the list, print it forwards, invert again. Each step can be done without violating restrictions except the last one.
EDIT: As cube notes in the comments the second and the third stages can be combined into one pass. This gives two passes – first reverse, then print while reversing again.
Building on sharptooth's reply, you can combine the printing and second inversion in the same pass.
Edit: The "list is left intact" from a single-threaded view because the post-condition equals the pre-condition.
Edit 2: Not sure how I got the answer, but I'll take it since I've hit the rep cap for the day. I gave sharptooth a +1 too.
Here's a C# implementation that holds for all the current rules. It mutates the list during the execution, but the list is restored before returning.
using System;
using System.Diagnostics;
namespace SO1135917.Classes
{
public class ReverseListPrinter
{
public static void Execute(Node firstNode, Action<Node> action)
{
Reverse(Reverse(firstNode, null), action);
}
private static Node Reverse(Node firstNode, Action<Node> action)
{
Node node = firstNode;
Debug.Assert(node != null);
Node nextNode = node.Next;
node.Next = null;
while (node != null)
{
if (action != null)
action(node);
if (nextNode == null)
break;
Node nextNode2 = nextNode.Next;
nextNode.Next = node;
node = nextNode;
nextNode = nextNode2;
}
return node;
}
}
}
There is one problem, however, and that is that the state of the list is undefined if an exception should occur in the above methods. Probably not impossible to handle though.
A subversion repository of the above code, with unit tests, for Visual Studio 2008 is available here, username and password is both 'guest' without the quotes.
You can first check the length of the list. Then create a print-buffer, which you fill in backwards as you traverse the list once again for the information.
Or
You can create another linked list where you add all the printing data in the front when you traverse the first list, and then print the second list from front to back.
Either way makes only two passes at most. The first idea could be done in one pass if you have a header struct that keeps track of the amount of elements in the list.
Edit: I just realised that these ideas does not use constant memory.
The only way to do this sensibly seems to be Sharptooths reply, but that requires three passes.
a function like the following might solver your issue:
void invert_print(PtNo l){
PtNo ptaux = l;
PtNo last;
PtNo before;
while(ptaux != NULL){
last = ptaux;
ptaux = ptaux->next;
}
while(ptaux != last){
printf("%s\n", last->info.title);
ptaux = l;
before = last;
while(ptaux != before){
last = ptaux;
ptaux = ptaux->next;
}
}
}
you will need a structure like the following:
typedef struct InfoNo{
char title20];
}InfoNo;
typedef struct aPtNo{
struct InfoNo info;
struct aPtNo* nextx;
}*PtNo;
Objective-C Link class with reverse method:
Link.h
#import <Foundation/Foundation.h>
#interface Link : NSObject
#property(nonatomic) int value;
#property(nonatomic) Link *next;
- (Link*)reversedList;
#end
Link.m
#import "Link.h"
#implementation Link
- (Link*)reversedList {
Link* head;
Link *link = self;
while (link) {
// save reference to next link
Link *next = link.next;
// "insert" link at the head of the list
link.next = head;
head = link;
// continue processing the rest of the list
link = next;
}
return head;
}
#end

Resources