C++字符串声明
C++ String Declaring
我已经使用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<<"!";
}
相关文章:
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 声明要在 qml 和 cpp 文件中使用的全局字符串
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- 用于从 ANSI 字符串转换为 std::basic_string <TCHAR>的正确函数声明
- 为什么必须将 const 添加到 constexpr 中才能进行字符串文字声明?
- 如何声明接受字符串、返回void的闭包的类型
- 我在 c++ 中声明字符串类型时遇到问题
- 在此范围C 中未声明字符串和char变量
- 无法声明字符串变量
- C++可变内存中声明字符串文本
- 如何在 c++ 中的类中声明字符串成员
- 在arduino中声明字符串的问题
- 声明字符串流ss之间的差异;和std::字符串流ss;在C++中
- 如何让用户输入声明字符串数组的大小
- 用常量字符串和字符串常量声明字符串有什么区别
- 可以使用特定名称声明字符串[]
- 在类中声明字符串类型(c++)
- 在GNU c++中声明字符串数组
- c++无法在派生类中声明字符串
- 使用声明字符串的 cin.getline