advantage of QString over std::string

Vihaan Verma picture Vihaan Verma · Jun 14, 2012 · Viewed 19k times · Source

What advantages does QString offer over std::string? Can a normal std::string store unicode characters ? I m trying to write a program that will open various song files . The name of these files can be in different languages. I have heard that using a normal string in such cases will not work properly. I want to keep the application independent of QT and reuse the code for example in android . What do you suggest .

Answer

Oleh Prypin picture Oleh Prypin · Jun 14, 2012

QString allows you to work with Unicode, has more useful methods and integrates with Qt well. It also has better performance, as cbamber85 noted.

std::string just stores the bytes as you give them to it, it doesn't know anything about encodings. It uses one byte per character, but it's not enough for all the languages in the world. The best way to store your texts would probably be UTF-8 encoding, in which characters can take up different number of bytes, and std::string just can't handle that properly! This, for example, means that length method would return the number of bytes, not characters. And this is just the tip of an iceberg...