Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should one use std::string over c-style strings in C++?

"One should always use std::string over c-style strings(char *)" is advice that comes up for almost every source code posted here. While the advice is no doubt good, the actual questions being addressed do not permit to elaborate on the why? aspect of the advice in detail. This question is to serve as a placeholder for the same.

A good answer should cover the following aspects(in detail):

  1. Why should one use std::string over c-style strings in C++?
  2. What are the disadvantages (if any) of the practice mentioned in #1?
  3. What are the scenarios where the opposite of the advice mentioned in #1 is a good practice?
like image 868
Alok Save Avatar asked Apr 05 '12 03:04

Alok Save


People also ask

What is a difference between the std::string and C style strings?

std::string is compatible with STL algorithms and other containers. C strings are not char * or const char * ; they are just null-terminated character arrays. Even string literals are just character arrays.

Why do we need std::string?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

Are C strings faster than std::string?

C-strings are usually faster, because they do not call malloc/new. But there are cases where std::string is faster. Function strlen() is O(N), but std::string::size() is O(1). Also when you search for substring, in C strings you need to check for '\0' on every cycle, in std::string - you don't.

What are the advantages and disadvantages of C++ strings over C strings?

C doesn't have strings. It has arrays of characters and all the null pointers, memory leaks, and buffer overflows that go along with that. C++ has a string class which overcomes many of the deficiencies of C's arrays -- but at a run-time performance cost.


2 Answers

  1. std::string manages its own memory, so you can copy, create, destroy them easily.
  2. You can't use your own buffer as a std::string.
  3. You need to pass a c string / buffer to something that expects to take ownership of the buffer - such as a 3rd party C library.
like image 74
Michael Anderson Avatar answered Sep 16 '22 14:09

Michael Anderson


Well, if you just need an array of chars, std::string provides little advantage. But face it, how often is that the case? By wrapping a char array with additional functionality like std::string does, you gain both power and efficiency for some operations.

For example, determining the length of an array of characters requires "counting" the characters in the array. In contrast, an std::string provides an efficient operation for this particular task. (see https://stackoverflow.com/a/1467497/129622)

  1. For power, efficiency and sanity
  2. Larger memory footprint than "just" a char array
  3. When you just need an array of chars
like image 32
ybakos Avatar answered Sep 20 '22 14:09

ybakos