I have questions about C++ initialization.
In Java: if I want a empty StringBuffer, I should use
StringBuffer sb = new StringBuffer();
to initialize a empty string buffer.
In C++: If I want to use a empty string, I can just declare
std::string str;
or std::string str = "";
In real world projects should I always write like the second form?
How about declare an empty vectors?
vector<int> vec;
Is this OK? or should I give some null values to this vec?
push_backto add members and let it handle the rest. As for the string, if you are going to add to an empty string I would explicitly assign it for readability, but otherwise it isn't really necessary.vector<int> *vec = new vector<int>(), which is more or less analogous to your Java example. Here, you initialize the pointer to the location of an allocated ("newed") vector. If you only writevector<int> vec;that's stack-allocation which happens automatically.