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?

user2840454
  • 89
  • 1
  • 3
  • 12
  • 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. – IllusiveBrian Oct 10 '14 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++. – user2840454 Oct 10 '14 at 23:45
  • Don't confuse it with `vector *vec = new vector()`, 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 vec;` that's stack-allocation which happens automatically. – leemes Oct 11 '14 at 00:06

2 Answers2

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.

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
  • Thank you so much!, so if I want zero value C++ basic types, I should always assign a value?http://msdn.microsoft.com/en-us/library/cc953fe1.aspx – user2840454 Oct 10 '14 at 23:50
  • Yes, you should always initialize them unless you have a good reason not to do so. – leemes Oct 11 '14 at 00:03
  • @leemes: any reason for that? why to initialize a string to ""? – Adrian Maire Oct 11 '14 at 00:08
  • @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 ;) ) – leemes Oct 11 '14 at 00:10
  • As @leemes said, for basic types, you should always initialize them: int myvar = 0; – Adrian Maire Oct 11 '14 at 00:16
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?

Community
  • 1
  • 1
dev0z
  • 2,275
  • 1
  • 15
  • 16