How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?

Sounds like a job for std::copy_if. Define a predicate that keeps track of elements that already have been processed and return false if they have. If you don’t have C++11 support, you can use the clumsily named std::remove_copy_if and invert the logic. This is an untested example: template <typename T> struct NotDuplicate { bool operator()(const … Read more

Scope vs. Lifetime of Variable

What is Scope? Scope is the region or section of code where a variable can be accessed. What is a lifetime? Lifetime is the time duration where an object/variable is in a valid state. For, Automatic/Local non-static variables Lifetime is limited to their Scope. In other words, automatic variables are automagically destroyed once the scope({,}) … Read more

How do I pass a reference to a two-dimensional array to a function?

If you know the size at compile time, this will do it: //function prototype void do_something(int (&array)[board_width][board_height]); Doing it with void do_something(int array[board_width][board_height]); Will actually pass a pointer to the first sub-array of the two dimensional array (“board_width” is completely ignored, as with the degenerate case of having only one dimension when you have int … Read more

conversion of 2D array to pointer-to-pointer

A mere conversion won’t help you here. There’s no compatibility of any kind between 2D array type and pointer-to-pointer type. Such conversion would make no sense. If you really really need to do that, you have to introduce an extra intermediate “row index” array, which will bridge the gap between 2D array semantics and pointer-to-pointer … Read more

multiple definition in header file

The problem is that the following piece of code is a definition, not a declaration: std::ostream& operator<<(std::ostream& o, const Complex& Cplx) { return o << Cplx.m_Real << ” i” << Cplx.m_Imaginary; } You can either mark the function above and make it “inline” so that multiple translation units may define it: inline std::ostream& operator<<(std::ostream& o, … Read more