C ++ Is a container std :: string?

This may be a simple question for some of you. But I was wondering if the std::string container is a container. By container, I mean containers, for example, std::vector , std::list and std::deque .

Since std::basic_string<> accepts types other than integer characters, it is also optimized when working with character arrays. It’s not clear to me in which category it falls.

This will compile:

 #include <string> #include <iostream> int main() { std::basic_string<int> int_str; int_str.push_back(14); return 0; } 

But adding this line:

 std::cout << int_str << std::endl; 

This is not true. Thus, from these facts, I can conclude that std :: basic_string was not intended to work with types other than characters.

This may be a strange question for you. The reason I need to know this is because I am working on a framework and I still can’t determine in which category the string will fall.

+7
source share
3 answers

Yes, the std::basic_string meets all the requirements of the Container concept. However, I think it has more stringent requirements for the contained type. Just trying to figure out what exactly.

(These are not Bjarne Concepts. Only a bit of the " 23.2.1 General container requirements " standard.)

+9
source

According to standards (2003,2011) std :: basic_string is a container for POD types only. That is, fundamental types or simple structures / classes without constructors, destructors, or virtual functions. But gnu stdlib allows the use of non-POD types with std :: basic_string. Here is an example of working with the types basic_string and non-POD.

And if you want your example to work, you must define a statement

 std::ostream& operator<<(::std::ostream& out, std::basic_string<int> &dat) { out << dat[0]; return out; } 

Or something like that.

+2
source

Well it can be said that this is not a container, at least not in the way you think of a std container.

The simplest example is that you cannot insert any thing into it.

But it does have some sort of classification of the container, like the fact that you can put some of the basic types into it, even if they are not characters, and perhaps the most surprising thing is that you can get an iterator to it, like if it were a regular container.

So what is this deterrence? I would say yes, but! this is not all brilliant.

+1
source

All Articles