Time complexity of a Priority Queue in C++

If you have an array of size n and you want to build a heap from all items at once, Floyd’s algorithm can do it with O(n) complexity. See Building a heap. This corresponds to the std::priority_queue constructors that accept a container parameter. If you have an empty priority queue to which you want to add n items, one at a time, … Read more

Does a Java to C++ converter/tool exist?

It’s possible to do anything given enough time, money and resources. Is it practical? Beyond trivial examples not really. Or rather it depends on what constitutes an acceptable error rate. The real problem is that the idioms are different in Java to C++. Java to C# for example would actually be far easier (because the … Read more

Replace part of a string with another string

There’s a function to find a substring within a string (find), and a function to replace a particular range in a string with another string (replace), so you can combine those to get the effect you want: In response to a comment, I think replaceAll would probably look something like this:

How do I declare a 2d array in C++ using new?

If your row length is a compile time constant, C++11 allows See this answer. Compilers like gcc that allow variable-length arrays as an extension to C++ can use new as shown here to get fully runtime-variable array dimension functionality like C99 allows, but portable ISO C++ is limited to only the first dimension being variable. Another efficient option is … Read more

What are forward declarations in C++?

Why forward-declare is necessary in C++ The compiler wants to ensure you haven’t made spelling mistakes or passed the wrong number of arguments to the function. So, it insists that it first sees a declaration of ‘add’ (or any other types, classes, or functions) before it is used. This really just allows the compiler to … Read more

C++ error: terminate called after throwing an instance of ‘std::bad_alloc’

This code has 3 holes: First hole: int numEntries. Later you do: ++numEntries; You increment unspecified value. Not sure if it’s UB, but still bad. Second and third hole: And numEntries has unspecified value (first hole). You use it to initialize length and size – that is Undefined Behaviour. But let’s assume it is just some big number – you allocate memory … Read more

C++ Vector of pointers

It means something like this: Then you add to the vector as you read lines: Remember to delete all of the Movie* objects once you are done with the vector.

“No viable overloaded ‘=’ ” why?

I’m making a project for one of my classes and I’m pretty sure I’m almost done with the project. Basically I have to input 2 people’s tickets and I have to find the maximum and minimum price. I need to overload the * and / operators to fix that issue for the project. Also, the friend declaration … Read more