std::wstring VS std::string

I am not able to understand the differences between std::string and std::wstring. I know wstring supports wide characters such as Unicode characters. I have got the following questions: When should I use std::wstring over std::string? Can std::string hold the entire ASCII character set, including the special characters? Is std::wstring supported by all popular C++ compilers? … Read more

Why does C++ code for testing the Collatz conjecture run faster than hand-written assembly?

I wrote these two solutions for Project Euler Q14, in assembly and in C++. They implement identical brute force approach for testing the Collatz conjecture. The assembly solution was assembled with: nasm -felf64 p14.asm && gcc p14.o -o p14 The C++ was compiled with: g++ p14.cpp -o p14 Assembly, p14.asm: section .data fmt db “%d”, … Read more

What is the strict aliasing rule?

When asking about common undefined behavior in C, people sometimes refer to the strict aliasing rule. What are they talking about? 1Best Answer 11 A typical situation where you encounter strict aliasing problems is when overlaying a struct (like a device/network msg) onto a buffer of the word size of your system (like a pointer … Read more

How to trim a std::string?

I’m currently using the following code to right-trim all the std::strings in my programs: std::string s; s.erase(s.find_last_not_of(” \n\r\t”)+1); It works fine, but I wonder if there are some end-cases where it might fail? Of course, answers with elegant alternatives and also left-trim solution are welcome. 48 s 48

Why should C++ programmers minimize use of ‘new’?

I stumbled upon Stack Overflow question Memory leak with std::string when using std::list<std::string>, and one of the comments says this: Stop using new so much. I can’t see any reason you used new anywhere you did. You can create objects by value in C++ and it’s one of the huge advantages to using the language. … Read more