9 ms·
C++11, 14 and 17 finally made me fall in love with C++. It's almost like having a nice and easy scripting language with the added bonus of static typing and a h
by mschuetz 10y ago
C++11, 14 and 17 finally made me fall in love with C++. It's almost like having a nice and easy scripting language with the added bonus of static typing and a huge performance boost.
There are a few things that I'm still not quite happy with.
* It's still incredibly cumbersome to set up a project
* Setting up a proper build system is tedious, especially if you're targeting multiple platforms.
* I'm missing some "functional" methods attached to the collections, e.g. find, filter, map, some, each, ...
Afaik some of them exist as functions that take 2 iterators and a lambda but that's just plain ugly and unreadable. Instead of
std::find_if(list.begin(), list.end(), condition});
I'd like something like
list.find(condition);
- godelski 10y agoThis might interest you then http://www.modernescpp.com/index.php/the-new-ranges-library http://www.modernescpp.com/index.php/the-new-ranges-library
- Koshkin 10y agoAlthough I agree that it would seem natural to have such a method in a standard container class, I would like to make a couple of observations. First, there are containers that do have a find() method, std::map, for example (although not on a condition). It could be a wrong impression, but to me this tells something. Perhaps, if I intend to do a search, I might be better off using a different structure (rather than a list) that is more suitable for searching, or maybe a different - more efficient - algorithm (such as binary_search(), if applicable). Second, if you use a container as a part of your own class, defining a find() method is trivial. I have no doubt that there are other, more technical reasons for the standard library being the way it is, for example, the idea of separation of algorithms from containers by means of iterators.
- clishem 10y agoThe reason is that `std::find` is O(N) while `std::map::find` is O(1).
- slavik81 10y agoIt doesn't affect your point, but std::map::find is O(log n). std::map is ordered and is typically a tree.
- WildUtah 10y agoYes, except the canonical map is—surprising the users of other collection frameworks—an ordered, balanced tree. So std::map::find() is O(ln n) while std::unordered_map::find() is O(1).
- mschuetz 10y agoFind probably wasn't the best example but even with find, I sometimes want to look for an item in a vector with 20 elements. In cases like that, I'm fine if find() has to apply a comperator to every element until it finds the first that matches. And functions like map (apply function to each element of a collection), filter (return new collection with all elements that match a condition) and possibly some others have to be applied to all elements in a collection anyway. > Second, if you use a container as a part of your own class, defining a find() method is trivial. It is, but I'd rather not have to define 20 trivial things when I start a new project.
- Shorel 10y agoIf you are willing to try CMake (which I know has sparse documentation), browse this repository: https://github.com/Akagi201/learning-cmake https://github.com/Akagi201/learning-cmake
- sqeaky 10y agoHave you tried CMake for setting up you C++ projects? It hide pretty much all the dirty details of find libraries and linking against libraries. If you already have a compiler installed, you can go from nothing to knowing how to build a library, executable and an installer for multiple platforms in just a couple of hours with the CMake tutorial: https://cmake.org/cmake-tutorial/ https://cmake.org/cmake-tutorial/
- Koshkin 10y agoIncidentally, it looks like Visual Studio 2017 will support CMake: https://blogs.msdn.microsoft.com/vcblog/2016/10/05/cmake-support-in-visual-studio/ https://blogs.msdn.microsoft.com/vcblog/2016/10/05/cmake-sup...
- voltagex_ 10y agoIf anyone wants me to test anything in particular, I've got that RC on my laptop - although the setup for 2017 is much nicer than anything that came before.
- kevin_thibedeau 10y agoCMake itself can be confounding if you want to do anything slightly unusual.
- wfunction 10y agoWhile CMake is better than anything I've seen, I still find it awful given it doesn't let you move your project to another folder...
- aoloe 10y agoI fear that you must be doing something very wrong... Or you did not find the rights word to explain your issue (to me). Do you mean that you, then, have to rebuild your project from scratch?
- chairmanwow 10y agoI agree that CMake is better than handwriting a Makefile, it is still absolutely shit compared to Rust's build system or the straightforwardness of sourcing external dependencies with Maven or Gradle in Java. If I want to use an external library in a C++ project, I need to: * download it from the website / GitHub * build it from source * add the library into my CMake file. Whereas in Gradle, I add a single line to one file and the library is installed on my next build attempt. Full stop. There are no more steps. C++ makes it searingly painful to get a project going. I'm honestly afraid to add any new libraries to my projects because I know that (at minimum) that means I'm sacrificing an afternoon to resolving new errors.
- ioquatix 10y agoIt's not perfect, but teapot tries to address the first two points (cumbersome to make a new project and cross-platform builds). http://teapot.nz http://teapot.nz
- beached_whale 10y agoUnified call syntax would have allowed something like a function of the form f( list, condition ); to be called as list.f( condition ). I don't think it made it into C++ 17 though. It's too bad because that would have allowed some really powerful extending of existing code. C# has this already. I hope that it makes it into C++20 https://isocpp.org/blog/2016/02/a-bit-of-background-for-the-unified-call-proposal https://isocpp.org/blog/2016/02/a-bit-of-background-for-the-... Also, you can do stuff like auto blah = with( list ) << sort( ) << filter( predicate ); now. It's not in the std lib but it is doable
- braveo 10y agoC# doesn't have unified call syntax, it has extension methods. You're right in that they do allow you to write a function that takes an argument and call it as if it wasn't on that argument, but it doesn't have UCS generically, just as a special case.
- beached_whale 10y agoYou are technically correct, the best kind. The ability is similar though, although I can see some fun C++ code with templates to apply it to all types. Then, if allowed, something like int i; i.print( ); could happen.
- bjourne 10y ago> * Setting up a proper build system is tedious, especially if you're targeting multiple platforms. There is a tool called waf (https://github.com/waf-project/waf https://github.com/waf-project/waf) which is getting way too little love, but is really the perfect build system for C and C++ projects: from glob import glob def options(ctx): ctx.load('compiler_cxx') def configure(ctx): ctx.load('compiler_cxx') def build(ctx): ctx.program(source = glob('src/*.cpp'), target = 'your-program') That build script will work with all major compilers (Visual Studio, gcc, clang, mingw) out of the box.
- 0xFFC 10y agoI do use CMake as my build system, It is like torture ! literally, I always liked waf, I am curious why not many people use it ? it is incredible, I have very high hopes for waf and Meson.
- slavik81 10y agoThe built-in rules are nice but have you tried doing anything fancy with waf? I wanted to define a rule that would transform an input file into a output file using a tool that was built from source at build-time, then use that output file as a dependency for a unit test. After spending a full week mucking around with waf, I gave up. CMake was much easier for me to understand. Sure, the language is hot garbage, but the mental model of how it functions is simpler. I did something similar to the above and it took an afternoon of learning.
- ktRolster 10y agoI do use CMake as my build system, It is like torture ! I don't want to diminish your experience, but I think you might be doing something wrong there. It doesn't need to be torture.
- mschuetz 10y agoI'm having the same problem with CMake. Adding a third party library was an intense challenge. CMake isn't easy.
- utopcell 10y agoMicrosoft has open-sourced their LINQ C++ implementation [1] which allows you to do things like this (from their repo) : random_device rd; // non-deterministic generator mt19937 gen(rd()); uniform_int_distribution<> dist(4, 18); // for testing purposes, produce byte stream that from lines of text auto bytes = range(0, 10) | flat_map([&](int i){ auto body = from((uint8_t)('A' + i)) | repeat(dist(gen)) | as_dynamic(); auto delim = from((uint8_t)'\r'); return from(body, delim) | concat(); }) | window(17) | flat_map([](observable<uint8_t> w){ return w | reduce( vector<uint8_t>(), [](vector<uint8_t> v, uint8_t b){ v.push_back(b); return v; }) | as_dynamic(); }) | tap([](vector<uint8_t>& v){ // print input packet of bytes copy(v.begin(), v.end(), ostream_iterator<long>(cout, " ")); cout << endl; }); // // recover lines of text from byte stream // auto removespaces = [](string s){ s.erase(remove_if(s.begin(), s.end(), ::isspace), s.end()); return s; }; // create strings split on \r auto strings = bytes | concat_map([](vector<uint8_t> v){ string s(v.begin(), v.end()); regex delim(R"/(\r)/"); cregex_token_iterator cursor(&s[0], &s[0] + s.size(), delim, {-1, 0}); cregex_token_iterator end; vector<string> splits(cursor, end); return iterate(move(splits)); }) | filter([](const string& s){ return !s.empty(); }) | publish() | ref_count(); // filter to last string in each line auto closes = strings | filter( [](const string& s){ return s.back() == '\r'; }) | Rx::map([](const string&){return 0;}); // group strings by line auto linewindows = strings | window_toggle(closes | start_with(0), [=](int){return closes;}); // reduce the strings for a line into one string auto lines = linewindows | flat_map([&](observable<string> w) { return w | start_with<string>("") | sum() | Rx::map(removespaces); }); // print result lines | subscribe<string>(println(cout)); [1] https://github.com/Reactive-Extensions/RxCpp https://github.com/Reactive-Extensions/RxCpp