4

Is std::string a container class in standard c++ library, restricted to hold only char elements?

4 Answers 4

13

It's a typedef of std::basic_string<char>, actually. std::basic_string is a container class specifically designed for string operations. This container can be used for wide characters (wchar_t) as well; for that case its typedef would be wstring.

Sign up to request clarification or add additional context in comments.

Comments

5

A std::basic_string<> is a class that is very much like a sequence container. Note that std::basic_string can contain any POD type, not just elements of type char (which is what a std::string is) or wchar_t (std::wstring).

I believe that a basic_string supports all the operations of a sequence container. However, note that by definition a container type can hold any assignable and copy-constructable types - not just POD types. So a basic_string is very much like a container, but strictly speaking it's not a container.

In other words there are types that can be used with a container that cannot be used with a basic_string. But for those types that can be used with a basic_string, the std::basic_string provides the full interface of a sequence container (I think) plus additional functionality.

4 Comments

Michael, please correct in if I'm wrong. Your answer seems contradictory with these statements. - Note that std::basic_string can contain any POD type, not just elements of type char. - there are types that can be used with a container that cannot be used with a basic_string. So the questions now is whether std::basic_string can hold any type, primitive or otherwise. If yes, only then std::basic_string behaves like a true STL container.
@Ankur - I'm not exactly sure what you're asking. basic_string<> cannot hold non-POD types which a container like vector<> could hold. But for POD types (like a plain-old struct) you can have a std::basic_string<mystruct_t>. To be honest, I'm just going by what the standard says - it looks like after a bit of experimentation, some compilers (GCC 3.4.5 and Comeau) will accept at least simple non-POD types as a type used for basic_string<> and some compilers won't accept a simple aggregate POD type (Digital Mars, VC6). VC9 behaves as the standard seems to call for.
@Michael. Okay, I think I got it now. BTW, what does POD type mean. Does it mean primitive data types(such as int, char) or more?
@Anku: Plain Old Data - a C++98 concept that indicates simple types. POD types cannot have virtual functions, and can be safely memcpy'd, for instance.
4

std::string as a typedef for basic_string<char, std::char_traits<char>, std::allocator<char> > is pretty much limited to the char type.

However, I don't think basic_string itself is necessarily limited to only character types (though, as the name suggests, it might be intended to be used for string data).

#include <string> #include <cassert> int main() { std::basic_string<int> numbers_1, numbers_2; numbers_1 += 1; numbers_2 += 2; std::basic_string<int> numbers_3 = numbers_1 + numbers_2 + 3; unsigned pos = numbers_3.find(10); assert(pos == std::basic_string<int>::npos); } 

Comments

3

std::string is a basic_string. It's not necessarily a char, but it has to follow the char traits http://www.cplusplus.com/reference/string/char_traits/

1 Comment

No, std::string is a std::basic_string<char, std::char_traits<char>, std::allocator<char> >, which is absolutely restricted to char.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.