官术网_书友最值得收藏!

Strings

Variables that are non-numerical are either a single character or a series of characters called strings. In C++, a series of characters can be stored in a special variable called a string. A string is provided through a standard string class.

To declare and use string objects we have to include the string header file. After #include <conio.h>, also add #include <string> at the top of the file.

A string variable is declared in the same way as other variable types, except before the string type you have to use the std namespace.

If you don't like adding the std:: namespace prefix, you can also add the line using the std namespace. After the #include. This way, you won't have to add the std:: prefix, as the program will understand. However, it can be printed out just like other variables:

#include <iostream> 
#include <conio.h> 
#include <string> 
 
// Program prints out values to screen 
 
int main() 
{ 
 
   std::string name = "The Dude"; 
 
   std::cout << "My name is: " << name << std::endl; 
 
   _getch(); 
   return 0; 
} 

Here is the output:

主站蜘蛛池模板: 乡城县| 新宁县| 蓬莱市| 阳谷县| 祁门县| 潼关县| 广德县| 成武县| 贺兰县| 邵武市| 都安| 高唐县| 林芝县| 平邑县| 平利县| 武安市| 崇仁县| 喜德县| 长子县| 乌什县| 天津市| 大连市| 济南市| 奉化市| 嘉兴市| 静海县| 嘉黎县| 始兴县| 渑池县| 晋江市| 驻马店市| 太白县| 桃源县| 华安县| 台山市| 巢湖市| 扎鲁特旗| 北辰区| 无棣县| 井陉县| 隆昌县|