Hilton D
Active Coder
Consider the following code:
Because each concatenation generates a new copy of the string, the overall complexity is O(n2). Fortunately, in Java, we could handle this using a StringBuffer, which has O(1) difficulty for each add, resulting in the overall complexity of O. (n).
In C++, std::string::append() has an O(n) complexity, and I'm not sure what the complexity of stringstream is.
Are there methods in C++ that are as sophisticated as those in StringBuffer?
StringBuilder Class in Java
Code:
public String joinWords(String[] words) {
String sentence = "";
for(String w : words) {
sentence = sentence + w;
}
return sentence;
}
Because each concatenation generates a new copy of the string, the overall complexity is O(n2). Fortunately, in Java, we could handle this using a StringBuffer, which has O(1) difficulty for each add, resulting in the overall complexity of O. (n).
In C++, std::string::append() has an O(n) complexity, and I'm not sure what the complexity of stringstream is.
Are there methods in C++ that are as sophisticated as those in StringBuffer?
StringBuilder Class in Java