2012-04-18 111 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