C++字符串声明

C++ String Declaring

本文关键字:声明 字符串 C++      更新时间:2023-10-16

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

例如在 VB 中:

Dim Something As String = "Some text"

Dim Something As String = ListBox1.SelectedItem

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

任何帮助,不胜感激。

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

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

使用标准<string>标头

std::string Something = "Some Text";

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

在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;
}

C++ 中的首选字符串类型是 string ,在命名空间 std 中定义,在标头 <string> 中,您可以像这样初始化它:

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

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

在C++中,字符串不是原始数据类型。因此,我们必须 #include 字符串类或使用 std 命名空间。下面是使用命名空间的示例。

#include <iostream>
using namespace std;
int main()
 {
  cout<<"Hello.n"<<"What is your name?n";
  string name;
  cin>>name;
  cout<<"Hello "<<name<<"!";
 }