2016-03-03 71 views
-31

在Python中,有一个名为string的类型,C++中python的string的确切等价物是什么?C++中字符串的等效代码是什么?

+5

我会在''中说'std :: string'。 – Jarod42

+2

在Python 3中,默认的'str'支持unicode,这使得它与'std :: string'完全不同。 –

+0

@VincentSavard'std :: wstring'呢? –

回答

7

相当于在<string>头文件中声明的std::stringstd::wstring

虽然你应该注意到python可能有不同的处理自动转换为UNICODE字符串的内在行为,如@Vincent Savard's comment中所述。

为了克服这些问题,我们在C++中使用了额外的库,如libiconv。它可用于多种平台。


你应该认真注意,询问在堆栈溢出之前做一些更好的研究,或者更明确地问你的问题。 std::stringis ubiquitous

3

你的意思是std::string家族?

#include <string> 

int main() { 
    const std::string example = "test"; 
    std::string exclaim = example + "!"; 

    std::cout << exclaim << std::endl; 

    return 0; 
} 
2

您既可以使用的std :: string(在这里看到可用的接口:std::string) 或使用char数组来表示,可能作为一个原始的字符串函数字符的基本组合。

+0

字符串比char数组更安全,char数组不是个好主意。 – theo2003