Implementing specialized data structures in modern C++ [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
(Okay, so my previous question is on hold as being too broad, so I'm narrowing it down here.)
I'm looking to take part in algorithmic programming contests, and a lot of problems hinge on the use of specialized data structures which are extremely good at a certain operation - for example, Fenwick trees allow calculation of prefix sums of a list of values in logarithmic time.
What is the preferred way of implementing such data structures in modern C++ (i.e. using C++11 features)? Is it possible to use STL algorithms and containers instead of writing structs and coding every operation by hand?
I'm looking for Fenwick trees, segment trees, treaps and some other data structures often useful in IOI-style contests, but general strategies are more than enough.

there's an implementation of a fenwick tree here: http://www.algorithmist.com/index.php/Fenwick_tree
It uses std::vector as the underlying container.
arguably the method increase could be written in terms of std::transform or std::foreach.

Related

Why do we need to learn different Sorting algorithms when the STL sort function is already available to us in C++? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Yesterday, this question came in my mind. Although I have neither read all the sorting algorithms like Quicksort, Merge Sort, Heapsort
Insertion Sort,
Selection Sort, and
Bubble Sort nor I have read the Introduction to Algorithms by CLRS but still, I am curious to know why there is a need to learn all such algorithms when the pre-defined sort function is already available to us in many languages.
Because
Simply sorting only may not be always the requirement. The requirement can be different. You may need to modify / integrate a sorting algorithm in order to develop a completely different thing.
The predefined sorting methods may not be the efficient at all cases.
Its always not about the sorted result but the approach of sorting in order to improve time and space complexity. Efficiency is the key.
There is no particular algorithm that is guaranteed to work best at all cases. Pros and cons may differ for different algorithms.
Need to understand which algorithm to be applied at what scenarios.
Sorting may not always done with numbers. It can be applied on other different complex types / structures. (There may not be pre-defined methods for complex cases )
There is always scope for a better approach.

Interesting Uses of STL Algorithms [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I apologize because I know in advance this question may be closed. I know it is not an essentially technical question, but it is one that could be meaningfully answered by experienced c++ programmers.
I am teaching a C++ class for programming students and, naturally, I will be covering the STL algorithms. I was trying to think of examples I could give in lecture that would be really cool applications of STL algorithms that make fairly complicated code really simple. I know some people here have ridiculously more C++ experience than I do so I was wondering if you could think of some interesting/exciting applications of the STL algorithms that could be done via loops and functions, but are really easy and concise using the stl algorithms.
Some of my examples:
Check if palindrome: Made really easy with equals(s.begin(), s.begin() + v.size()/2, s.rbegin())
Tokenize string into vector: Easy with copy(istream_iterator<int>(cin), istream_iterator<>(), back_inserter(data))
Permutations: Classic recursive problem made easy by stl algorithms
Sorting: This one is obvious but also not very interesting (i.e. it's not a cool application of <algorithm> for students)

Should I learn to use the C++ STL containers instead of building them? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I've learned about data structures and algorithms in my university course and they taught us how to implement them. After reading some answers on SO and Quora, it seems that using the STL containers are recommended.
As a software engineer/developer, you should know how a particular data structure works under the hood. and you should be able to customize/invent one when you need it. that's the reason they thought you so.
But generally you won't need to reinvent the wheel. so when there are tried and tested data structures available, like the std::stack, there's no need to do it again. specially because you'll have more bug issues in your implementation than those of STL or any other well designed one like Boost.
Unless you think you have a better knowledge of software engineering than the designers of the STL - or you have some very specific hardware requirements that they are unaware of

ASTs: prefer inheritance or variants? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In object oriented languages, it is fairly common to implement ASTs (Abstract Syntax Trees) using simple hierarchies (the Composite pattern), and to traverse them via visitors.
In functional programming languages, using variants/sum types, and then pattern matching, is the right approach.
C++ features both inheritance, and Boost.Variants. I have written several AST hierarchies using inheritance, but I'd like to get some feedback from people who would have used the variant approach. I'd like to know which one is the "best", in terms of performance (time and space), but also in terms of maintainability (easiness to create trees, and to traverse them).
I am especially interested in experience with implementation of hash-consing (keeping a single copy of each common subtree), possibly with Boost.Flyweight.
I am interested in battle field experience, not opinions. This question was original closed as "opinion-based". Which has never been the point...
Thanks!

How to implement Binary Indexed Tree? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have read that Binary Indexed Trees are very efficient. But I couldn't anything more than that. If anybody knows about that, please share your knowledge.
This solution will help you. There are direct algorithms available and this explanation is a good one I could see
This is how the author of the blog has described Binary indexed Tree
We often need some sort of data structure to make our algorithms faster. In this article we will discuss the Binary Indexed Trees structure. According to Peter M. Fenwick, this structure was first used for data compression. Now it is often used for storing frequencies and manipulating cumulative frequency tables.