1

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?

3
  • This is probably better for programmers.stackexchange.com, since it's an open-ended question. That said, in my opinion, the vector is fine for general use, use its member functions like push_back to 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. Commented Oct 10, 2014 at 23:39
  • Thank you so much for your reply. The reason I asked about this is because a few months ago when I interviewed a job, the interviewer questioned me about why I did not initialize the empty vector, so I am curious about how to initialize that in C++. Commented Oct 10, 2014 at 23:45
  • Don't confuse it with 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 write vector<int> vec; that's stack-allocation which happens automatically. Commented Oct 11, 2014 at 0:06

2 Answers 2

4

std:string and std:vector are classes, not basic types: That mean that unlike an Integer which as a pseudo-random value at the declaration moment, string and vectors has a well defined initial value.

The default value for std::string is "".

The default content for std::vector is {}.

You may use what you prefer, but the initialization is not necessary, and even not very optimal.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much!, so if I want zero value C++ basic types, I should always assign a value?msdn.microsoft.com/en-us/library/cc953fe1.aspx
Yes, you should always initialize them unless you have a good reason not to do so.
@leemes: any reason for that? why to initialize a string to ""?
@AdrianMaire he was talking about "basic types" (or at least my response "Yes" was about them only... I could not simply answer "Yes" because of the minimum number of characters ;) )
As @leemes said, for basic types, you should always initialize them: int myvar = 0;
1
  1. std::string str(""); It does a direct initialization and uses string(const char *) constructor.

  2. std::string str=""; It does a copy initialization.

  3. std::string str; It creates empty string.

Option 3 is just like others and less overhead. Use it Read more about the difference from here What's the motivation behind having copy and direct initialization behave differently?

2 Comments

So if I use the option 3, will it be accepted in projects?
Yes, it is the best choice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.