Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between std::string and std::basic_string? And why are both needed?

Tags:

c++

stl

What's the difference between std::string and std::basic_string? And why are both needed?

like image 779
Benj Avatar asked Nov 02 '09 15:11

Benj


People also ask

What is the difference between std::string and string?

There is no functionality difference between string and std::string because they're the same type.

What is the difference between std::string and std :: vector?

std::string offers a very different and much expanded interface compared to std::vector<> . While the latter is just a boring old sequence of elements, the former is actually designed to represent a string and therefore offers an assortment of string-related convenience functions.

Why do we use 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.

Do you need STD for string C++?

You don't need to use using namespace std; . You can qualify the namespace directly with the type. Just write std::string s; . @Martin: string is not a template class, basic_string is.


2 Answers

std::basic_string is a class template for making strings out of character types, std::string is a typedef for a specialization of that class template for char.

like image 89
CB Bailey Avatar answered Oct 03 '22 11:10

CB Bailey


std::string is an instantiation of std::basic_string<T>:

typedef std::basic_string<char> string 

std::basic_string is necessary to have a similar interface for all type of strings (wstring for example).

like image 40
pmr Avatar answered Oct 03 '22 12:10

pmr