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:

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

Returning an empty string : efficient way in c++

Gcc 7.1 -O3 these are all identical, godbolt.org/z/a-hc1d – jterm Apr 25 at 3:27 Original answer: Did some digging. Below is an example program and the relevant assembly: Code: Assembly: This was compiled with -std=c++11 -O2. You can see that there is quite a lot more work for the return “”; statement and comparably little for return std::string and return {}; (these … Read more

1e-9 or -1e9, which one is correct?

Neither is more correct than the other. They just represent different values. 1e-9 is 0.000000001; the minus sign applies to the exponent. -1e9 is -1000000000.0; the minus sign applies to the number itself. The e (or E) means “times 10-to-the”, so 1e9 is “one times ten to the ninth power”, and 1e-9 means “one times ten to the negative ninth power”. In mathematical scientific notation, this … Read more

How can I solve the error LNK2019: unresolved external symbol – function?

One option would be to include function.cpp in your UnitTest1 project, but that may not be the most ideal solution structure. The short answer to your problem is that when building your UnitTest1 project, the compiler and linker have no idea that function.cpp exists, and also have nothing to link that contains a definition of multiple. A way to fix this is making use … Read more

undefined reference to WinMain@16 (codeblocks)

When there’s no project, Code::Blocks only compiles and links the current file. That file, from your picture, is secrypt.cpp, which does not have a main function. In order to compile and link both source files, you’ll need to do it manually or add them to the same project. Contrary to what others are saying, using a … Read more

What is a dangling pointer?

A dangling pointer is a pointer that points to invalid data or to data which is not valid anymore, for example: This can occur even in stack allocated objects: The pointer returned by c_str may become invalid if the string is modified afterwards or destroyed. In your example you don’t seem to modify it, but since it’s … Read more

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