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

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:

主站蜘蛛池模板: 文成县| 南漳县| 饶阳县| 邢台市| 任丘市| 壤塘县| 赣州市| 扎兰屯市| 广南县| 承德市| 宝兴县| 辽源市| 达日县| 松滋市| 莎车县| 乌拉特中旗| 固阳县| 奉节县| 宣威市| 铁力市| 修武县| 平山县| 锡林郭勒盟| 高密市| 余干县| 栖霞市| 太和县| 武宣县| 黄山市| 兰西县| 崇明县| 二连浩特市| 磐安县| 梨树县| 朝阳县| 交城县| 申扎县| 织金县| 剑阁县| 阳春市| 金溪县|