I have some C++ methods that have std::set<std::string> as argument or return value.
I would like to map this to a Python frozenset (or regular set) but there does not seem to be a straightforward way to do this.
Does anyone know how one may accomplish this task.
Or you can use std::map<YourType, int> instead of std::set<YourType>, the value can be for example 0. std::map has the same insert/search time complexity as std::set, it also keeps the keys ordered, it will only bloat the memory a little. Then you can use map indexing suite and in python you can hide the difference in some wrapper class if needed. The disanvantage is that you have to modify your existing c++ code a little bit.
Unfortunately, the standard indexing_suite from Boost.Python does not support std::set. There is a indexing_suite v2, that works on all stl containers. (http://mail.python.org/pipermail/cplusplus-sig/2009-July/014704.html)
It may not have made it to the official distribution, but you can find it by asking around.
(http://mail.python.org/pipermail/cplusplus-sig/2009-July/014691.html)
I found it to be harder to use then the original indexing_suite, but it might fit your needs.
If that does not work, you can just manually wrap std::set<std::string> like you would any other class. This will get you a std::set<std::string> into python, where you can turn it into a python set fairly easily.
I think that both of those are more work then is called for though. Here is what I would do:
First, wrap the function in C++ with one that has the same signature, but stuffs the returned data in to a std::vector<std::string> instead of a std::set<std::string>. expose that function rather then the original
Now you have the data in python.
Second, wrap the c++ function in python function that takes the data in that std::vector<std::string> and stuffs it into a python set.
Yes, this is rather silly from a design aesthetics point of view, and not the most performant code in the world, but is gets you to where you are going with a minimum of code, and it is fairly robust.
Related
I'm looking for a data structure which behaves similar to boost::property_tree but (optionally) leaves the get/set implementation for each value item to the developer.
You should be able to do something like this:
std::function<int(void)> f_foo = ...;
my_property_tree tree;
tree.register<int>("some.path.to.key", f_foo);
auto v1 = tree.get<int>("some.path.to.key"); // <-- calls f_foo
auto v2 = tree.get<int>("some.other.path"); // <-- some fallback or throws exception
I guess you could abuse property_tree for this but I haven't looked into the implementation yet and I would have a bad feeling about this unless I knew that this is an intended use case.
Writing a class that handles requests like val = tree.get("some.path.to.key") by calling a provided function doesn't look too hard in the first place but I can imagine a lot of special cases which would make this quite a bulky library.
Some extra features might be:
subtree-handling: not only handle terminal keys but forward certain subtrees to separate implementations. E.g.
tree.register("some.path.config", some_handler);
// calls some_handler.get<int>("network.hostname")
v = tree.get<int>("some.path.config.network.hostname");
search among values / keys
automatic type casting (like in boost::property_tree)
"path overloading", e.g. defaulting to a property_tree-implementation for paths without registered callback.
Is there a library that comes close to what I'm looking for? Has anyone made experiences with using boost::property_tree for this purpose? (E.g. by subclassing or putting special objects into the tree like described here)
After years of coding my own container classes I ended up just adopting QVariantMap. This way it pretty much behaves (and is as flexible as) python. Just one interface. Not for performance code though.
If you care to know, I really caved in for Qt as my de facto STL because:
Industry standard - used even in avionics and satellite software
It has been around for decades with little interface change (think about long term support)
It has excellent performance, awesome documentation and enormous user base.
Extensive feature set, way beyond the STL
Would an std::map do the job you are interested in?
Have you tried this approach?
I don't quite understand what you are trying to do. So please provide a domain example.
Cheers.
I have some home-cooked code that lets you register custom callbacks for each type in GitHub. It is quite basic and still missing most of the features you would like to have. I'm working on the second version, though. I'm finishing a helper structure that will do most of the job of making callbacks. Tell me if you're interested. Also, you could implement some of those features yourself, as the code to register callbacks is already done. It shouldn't be so difficult.
Using only provided data structures:
First, getters and setters are not native features to c++ you need to call the method one way or another. To make such behaviour occur you can overload assignment operator. I assume you also want to store POD data in your data structure as well.
So without knowing the type of the data you're "get"ting, the only option I can think of is to use boost::variant. But still, you have some overloading to do, and you need at least one assignment.
You can check out the documentation. It's pretty straight-forward and easy to understand.
http://www.boost.org/doc/libs/1_61_0/doc/html/variant/tutorial.html
Making your own data structures:
Alternatively, as Dani mentioned, you can come up with your own implementation and keep a register of overloaded methods and so on.
Best
I have good command over c++ language.I am interested in creating code which generates the object dynamically for which no schema has already been defined in code.First of all, is it possible to do so in c++? If yes, please anyone would guide me how to do it?
For example,
<name>android</name>
<count>5</count>
<version>4.4.4</version>
If this is given in file(.xml), the automatic object should be created and I should be able to access it.
TempObj.name, TempObj.count and TempObj.version etc..
No, you cannot do this at runtime in C++. Types have to be known at compile time in C++.
You can do code generation, i.e. read XML, generate C++, and compile. Or you can use a slightly different syntax (and much slower performance) to access the methods, basically:
typedef std::map<std::string, std::string> Object;
Object obj = load("whatever.xml");
assert(obj["name"] == "android");
But to do exactly what you've asked will require a different language. Tons of choices: Python and Lua are two that easily integrate with a C++ application directly, if you want that.
Using a hash table would solve your problem. in C++, the STL (standard template library) provides std::unordered_map<>
You should use that to solve your problem.
std::unordered_map<std::string, std::string> hm
The API is nice, where you can insert and change elements by using the [] operator.
hm["name"] = "android"
hm["count"] = "5";
hm["version"] = "4.4.4";
If you need dynamic types, you could probably use templates.
You may be wondering why you would use an ordered or an unordered map in C++. With an ordered map, all elements are able to iterated in the order they were inserted, whereas an unordered map the order in which you will access them is random based on the hashed value.
A bit of background:
I am currently working on an assignment from my OOP course which consists in designing and implementing a phone book manager around various design patterns.
In my project there are 3 classes around which all the action happens:
PhoneBook;
Contact (the types stored in the phone book);
ContactField (fields stored in the Contact).
ContactManager must provide a way to iterate over its contacts in 2 modes: unfiltered and filtered based on a predicate; Contact must provide a way to iterate over its fields.
How I initially decided to implement:
All design patterns books I came across recommend coding to an interface so my first thought was to extract an interface from each of the above classes and then make them implement it.
Now I also have to create some kind of polymorphic iterator for things to be smooth so I adapted the Java iterator interface to write forward iterators.
The problems:
The major setback with this design is that I lose interoperability
with stl <algorithm> and the syntactic sugar offered by range
based for loops.
Another issue I came across is the Iterator<T>::remove() function. If
I want an iterator that can alter the sequence it iterates over
(remove elements) then all is fine however if I don't want
that behavior I'm not exactly sure what to do.
I see that in Java one can throw UnsupportedOperationException
which isn't that bad since (correct me if I'm wrong) if an
exception isn't handled then the application is terminated and a
stack trace is shown. In C++ you don't really have that luxury
(unless you run with a debugger attached I think) and to be honest
I'd rather prefer to catch such errors at compile time.
The easiest way out (that I see) of this mess is to avoid using interfaces on the iterable types in order to accommodate my own stl compatible iterators. This will increase coupling however I'm not sure it will actually have any impact in the long run (not in the sense that this project will become throw away code soon of course). My guess is that it won't however, I'd like to hear the elders opinion as well before I proceed with my design.
I would probably take a slightly different approach.
Firstly, iteration over a contact is pretty simple since it's a single type of iteration and you can just provide begin and end methods to allow iteration over the underlying fields.
For the iteration over a PhoneBook I would still just provide a normal begin and end, and then provide a for_each_if function that you use to iterate over only the contacts that are interesting, instead of trying to provide a super-custom iterator that skips over un-interesting elements.
I am designing a class in C++ that extracts URLs from an HTML page. I am using Boost's Regex library to do the heavy lifting for me. I started designing a class and realized that I didn't want to tie down how the URLs are stored. One option would be to accept a std::vector<Url> by reference and just call push_back on it. I'd like to avoid forcing consumers of my class to use std::vector. So, I created a member template that took a destination iterator. It looks like this:
template <typename TForwardIterator, typename TOutputIterator>
TOutputIterator UrlExtractor::get_urls(
TForwardIterator begin,
TForwardIterator end,
TOutputIterator dest);
I feel like I am overcomplicating things. I like to write fairly generic code in C++, and I struggle to lock down my interfaces. But then I get into these predicaments where I am trying to templatize everything. At this point, someone reading the code doesn't realize that TForwardIterator is iterating over a std::string.
In my particular situation, I am wondering if being this generic is a good thing. At what point do you start making code more explicit? Is there a standard approach to getting values out of a function generically?
Yes, it's not only fine but a very nice design. Templating that way is how most of the standard library algorithms work, like std::fill or std::copy; they are made to work with iterators so that you can fill a container that already has elements in it, or you can take an empty container and fill it up with data by using std::back_inserter.
This is a very good design IMO, and takes advantage of the power of templates and the iterator concept.
You can use it like this (but you already know this):
std::list<Url> l;
std::vector<Url> v;
x.get_urls(begin(dat1), end(dat1), std::back_inserter(l));
y.get_urls(begin(dat2), end(dat2), std::back_inserter(v));
I get the feeling that you are afraid of using templates, that they are not "normal" C++, or that they should be avoided and are bloated or something. I assure you, they are very normal and a powerful language feature that no other language (that I know of) has, so whenever it is appropriate to use them, USE THEM. And here, it is very appropriate.
Looks to me that you have the wrong interface.
There are already algorithms for copying from iterators in-to containers. Seems to me that your class is providing a stream of urls (without relying modifying its source). So all you really need is a way to expose you internal data via iterators (forward iterators) and thus all you need to provide begin() and end().
UrlExtractor page(/* Some way of constructing page */);
std::vector<std::string> data;
std::copy(page.begin(), page.end(), std::back_inserter(data));
I would just provide the following interface:
class UrlExtractor
{
...... STUFF
iterator begin();
iterator end();
};
Yes, you are being too general. The point of a template is that you can generate multiple copies of the function that behave differently. You probably don't want that because you should pick one way of representing a URL and use that in your entire program.
How about you just do:
typedef std::string url;
That allows you to change the class you use for urls in the future.
Maybe std::vector implements some interface with push_back() in it and your method can take a reference to that interface (back_inserter?).
It's hard to say without knowing the actual use case scenarios, but in
general, it's better to avoid templates (or any other unnecessary
complexity) unless it actually buys you something. The most obvious
signature here would be:
std::vector<Url> UrlExtractor::get_urls( std::string const& source );
Is there really any likely scenario where you'll have to parse anything
but an std::string? (There might be if you also supported input
iterators. But in practice, if you're parsing, the sources will be
either a std::string or an std::istream&. Unless you really want to
support the latter, just use std::string.) And of course, client code
can do whatever it wants with the returned vector, including appending
it to another type of collection.
If the cost of returning a std::vector does become an issue, then you
could take an std::vector<Url>& as an argument. I can't see any
reasonable scenario where any additional flexibility would buy you much,
and a function like get_urls is likely to be fairly complicated, and
not the sort of thing you'd want to put in a header.
I'm new to C++, and while writing a class I realized one of my methods was asking for a vector-of-vectors. Should this be done or should I rethink my class's interface? (How?)
I think it is no problem what container you use. You could do it like
void func(std::vector<std::vector<int> > const& int_matrix);
or in C++11, successive > won't be considered as '>>' so you could also use
void func(std::vector<std::vector<int>> const& int_matrix);
But the problem is, if your work are published as binary instead of source code, the users of the interface should have the same STL implement as yours, otherwise strange runtime errors may occur. So use STL container as interface is not proper in this situation. You have to define some structures yourself as the type of parameters.
A vector of vectors isn't necessarily a bad thing. If you need something like a multidimensional array, then you need what you need. (Just make sure you pass the vector by [const] reference).
You might want to change the title of your question though, because the title says "vector<T>" (boldness because it thinks it's an HTML tag) but your question asks about a vector of vectors.
IMO, if possible it's better to merge all the vectors into a single vector. Having vector of vector doesn't make much sense to me.