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

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:

主站蜘蛛池模板: 鹿邑县| 栾川县| 凉城县| 临西县| 麻城市| 务川| 雷州市| 留坝县| 盐城市| 昆明市| 铅山县| 西峡县| 曲周县| 来凤县| 衡东县| 保康县| 屏边| 昌平区| 莎车县| 宜川县| 建瓯市| 万载县| 赣榆县| 景东| 咸阳市| 古田县| 肇源县| 平泉县| 衡东县| 古交市| 吴堡县| 常宁市| 兴和县| 师宗县| 柳河县| 桦川县| 甘洛县| 晋中市| 贵阳市| 阿拉尔市| 宾阳县|