Is it possible to serialize and deserialize a class in C++?

The Boost::serialization library handles this rather elegantly. I’ve used it in several projects. There’s an example program, showing how to use it, here. The only native way to do it is to use streams. That’s essentially all the Boost::serialization library does, it extends the stream method by setting up a framework to write objects to a text-like format and read … Read more

Resizing dynamic array in c++

A() is not modifying nums to point at the new array. Even if it were, it is deleting the new array, so nums would end up pointing at invalid memory. You need to declare the arr parameter as a reference, and delete the old array instead of the new array: For what you are attempting, I think you have too much indirection. … Read more

Hash function for a string

First, it usually does not matter that much in practice. Most hash functions are “good enough”. But if you really care, you should know that it is a research subject by itself. There are thousand of papers about that. You can still get a PhD today by studying & designing hashing algorithms. Your second hash … Read more

bit vectors in c++

Here is a very simple statically sized bit vector implementation. It requires C++11 to function since it relies on the <cstdint> header, but this header is fairly commonly found since it’s based on a C99 feature. In a pinch you can use the C <stdint.h> header and simply use types in the global namespace instead. Note: This was typed on-the-fly … Read more

How to get current time in milliseconds?

Simply use std::chrono. The general example below times the task “of printing 1000 stars”: Instead of printing the stars, you will place your sorting algorithm there and time measure it. Do not forget to enable the optimization flags for your compiler, if you intend to do some benchmarking, e.g. for g++, you need -O3. This is serious, check … Read more

Function for C++ struct

Yes, a struct is identical to a class except for the default access level (member-wise and inheritance-wise). (and the extra meaning class carries when used with a template) Every functionality supported by a class is consequently supported by a struct. You’d use methods the same as you’d use them for a class.

The system cannot find the file specified. in Visual Studio

The system cannot find the file specified usually means the build failed (which it will for your code as you’re missing a # infront of include, you have a stray >> at the end of your cout line and you need std:: infront of cout) but you have the ‘run anyway’ option checked which means … Read more

C++ error: expected identifier before “(” token

In these if statements there are absent external parentheses I think that there should be And the loops look suspeciously. Take into account that array indices start from 0. So for example if you have an array of size N then the valid range of indices is [0, N-1] And you forgot to initialize nn1 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)