Const, Pointers and References

Summary In this post, I will introduce const, * and & and their combinations. I also have similar posts discussed auto and &. Conclusion As usual, I present the golden rule first: “read from right to left”. In other words, the const-on-the-right style always puts the const on the right of what it constifies”. When…

SZ and RZ on MAC OS

Summary In this post, I will introduce how to send files from a server to a local machine. Details 1. Use SCP SCP is a well-known command. scp a.zip fighternan@mydestop.com: 2. Use Iterm and SZ&RZ Install Iterm Iterm with tmux helps me a lot. Install lrzsz in the server and the desktop. sudo brew install…

References Versus Pointers

Summary In this post, I will discuss when to use References and when to use Pointers in C++. Conclusion As usual, I present the conclusion first. 1. If the varible can be nullptr, declare the varible as a pointer; 2. If the you want to re-assign the varible, declare the varible as a pointer; 3….

Class not Declared in Scope with .h Included

Summary I come across a compiling error, in which it says class A was not declared. However, I actually include A.h. Details After some research, I find out the bugs. In utils.h, I want to use some static member varible in class A and thus I include A.h. However, since utils.h acts as its name,…

Ping a Specific Port of a Host

Summary In this post, I will introduce how to “ping” a specific port in a host. Details Usually to test the connectivity, we will use the command ping. However, ping will typically send ICMP ECHO_REQUEST packets to network hosts. In ICMP, there are no ports. But in many cases (especially when we have a server…

Moving a Return Value is Easy

Summary In this post, we will discuss rvalue references and move semantics in C++11. Details Rvalue references and move semantics can be very complex, see this article. But the truth is that: the simplest is the best. It starts with three examples of returning values from functions. Read them and ask yourself which of them…

Search Files Under a Directory in C++

Summary In this post, I will introduce how to get a vector of files in a directory in C++. Instead of filesystem in boost or C++ 17, this method is purely based on C++ 11 and Unix platforms. Details The following codes are self-explained. // safe path join in unix-based system string path_join(const string& p1,…

Redirect or Return Views

Summary In this post, I will discuss how to choose one from the other between redirection and returning views. Details I come across a confusion that when a user click a button, should I use redirection or returning views. After researching, I figure out the difference between the two. Return Views Return a view in…

Implement Java String APIs in C++

Summary It is another post related to String in C++. I will introduce some useful codes which implement APIs in Java String. Content String.startsWith() String.endsWith() String.toLowerCase() String.toUpperCase() String.trim() Detials 1. String.startsWith() We implement a.startsWith(b) of java using C++. bool starts_with(const string & a, const string & b) { return b == a.substr(0, b.size()); } int…

Lvalues and Rvalues in C++

Summary In this post, I will introduce lvalues and rvalues in C/C++. But I leave rvalue references and move semantics to the next post. Details Definition Lvalues: (locator value) represents an object that occupies some unique location in memory (has an address in some scope). Rvalues: not lvalues. Important points and examples 1. Rvalues can’t…

Delegating and Inheriting Constructors

Summary In this post, I will show delegating constructors introduced in C++ 11. Details I come across the codes below, and get confused about the systax. class Iterator { struct Data; Data* data; public: Iterator(const vector<int>& nums); Iterator(const Iterator& iter); virtual ~Iterator(); int next(); bool hasNext() const; }; class PeekingIterator : public Iterator { private:…

Change Object While Iterating It

Summary In this post we will discuss how to change object while iterating it. Details Say we have a map. unordered_map<char, int> char_counter ({{‘1’, 1}, {‘a’, 1}, {‘b’, 2}, {‘c’, 3}}); Now, we want to delete all elements whose value is 1. To delete an element using iterator we will use erase() function. But the…