C ++ std :: string是容器吗? [英] C++ Is a std::string a container?

查看:163
本文介绍了C ++ std :: string是容器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些人来说,这可能是一个简单的问题。但我想知道 std :: string 是否是容器。容器是指类似 std :: vector std :: list std :: deque 。

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

因为 std :: basic_string<> 接受除了整数字符以外的其他类型,但也可以通过使用字符数组进行优化。对我来说还不是很清楚。

Since std::basic_string<> accepts other types than integral characters, but also is being optimized by working with character arrays. It isn't really clear to me in which category it falls.

它将编译为:

#include <string>
#include <iostream>

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

但是通过添加以下内容:

But by adding this line:

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

不会。因此,根据这些事实,我可以得出结论:std :: basic_string不能用于字符以外的其他类型。

It won't. So by these facts I could conclude that an std::basic_string was not intended to work with other types than characters.

对您来说这可能是一个奇怪的问题。我需要知道这一点的原因是因为我正在开发一个框架,但仍然无法确定字符串将落入哪个类别。

It might be a strange question for you. The reason I need to know this is because I'm working on a framework and I'm still not able to determine in which category a "string" would fall.

推荐答案

是的, std :: basic_string 模板满足 Container 概念。但是,我认为它对包含类型有更严格的要求。

Yes, the std::basic_string template fulfills all the requirements of the Container concept. However, I think it has stronger requirements on the contained type. Just trying to dig out exactly what.

(这不是Bjarne的概念。只有标有 23.2.1的常规容器要求 。)

(This is not Bjarne's Concepts. Just the bit of The Standard labeled "23.2.1 General container requirements".)

这篇关于C ++ std :: string是容器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆