2012-04-18 112 views
7

我一直在使用VB一段时间。现在我给C++一个镜头,我遇到了字符串,我似乎找不到一种方法来声明一个字符串。C++字符串声明

例如在VB:

Dim Something As String = "Some text" 

或者

Dim Something As String = ListBox1.SelectedItem 

请告诉我等价于C++上面的代码?

任何帮助表示赞赏。

回答

17

C++供给string类,它可以像这样使用:

#include <string> 
#include <iostream> 

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

在C++中优选的串类型是string,在限定命名空间std,在标题<string>,你可以初始化像这样的例子:

#include <string> 

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

更多关于它,你可以找到herehere

2

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

嘿!什么字符。数组? char str [30]; – 2018-02-14 07:31:45