C++ 字符串声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10218714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:45:22  来源:igfitidea点击:

C++ String Declaring

c++string

提问by NetInfo

I have been working with VB for a while now. Now I'm giving C++ a shot, i have came across strings, i cant seem to find a way to declare a string.

我已经使用 VB 一段时间了。现在我给 C++ 一个机会,我遇到了字符串,我似乎找不到声明字符串的方法。

For example in VB:

例如在 VB 中:

Dim Something As String = "Some text"

Or

或者

Dim Something As String = ListBox1.SelectedItem

Whats the equivalent to the code above in C++ ?

什么等同于 C++ 中的上述代码?

Any help is appreciated.

任何帮助表示赞赏。

回答by dasblinkenlight

C++ supplies a stringclass that can be used like this:

C++ 提供了一个string可以这样使用的类:

#include <string>
#include <iostream>

int main() {
    std::string Something = "Some text";
    std::cout << Something << std::endl;
}

回答by Ben Cottrell

using the standard <string>header

使用标准<string>标题

std::string Something = "Some Text";

http://www.dreamincode.net/forums/topic/42209-c-strings/

http://www.dreamincode.net/forums/topic/42209-c-strings/

回答by argue2000

In C++ you can declare a string like this:

在 C++ 中,您可以像这样声明一个字符串:

#include <string>

using namespace std;

int main()
{
    string str1("argue2000"); //define a string and Initialize str1 with "argue2000"    
    string str2 = "argue2000"; // define a string and assign str2 with "argue2000"
    string str3;  //just declare a string, it has no value
    return 1;
}

回答by Bojan Komazec

Preferred string type in C++ is string, defined in namespace std, in header <string>and you can initialize it like this for example:

C++ 中首选的字符串类型是string,在 namespacestd中定义,在 header 中<string>,您可以像这样初始化它,例如:

#include <string>

int main()
{
   std::string str1("Some text");
   std::string str2 = "Some text";
}

More about it you can find hereand here.

您可以在此处此处找到有关它的更多信息。