C++字符串的使用学习笔记01

来源:互联网 发布:索福瑞实时数据 编辑:程序博客网 时间:2024/05/29 03:10

C++标准程序库中提供了String类的定义存储字符串的对象,使用是在程序中必须包含这个类的头文件,使用语句:#include <string>

简单的一个小例子:

#include <iostream>#include <string>/* run this program using the console pauser or add your own getch, system("pause") or input loop */using namespace std;int main(int argc, char** argv) {string str1="What your name!";string str2("My name is zhangsan!");cout <<str1 <<str1[0]<<endl;cout <<str2 <<str2[0]<<endl;cout <<"Please input your name:";cin >>str1;cout <<str1 <<"---->>"<<str1.size()<<endl;//遍历打印字符串中 for(int i=0;i<str1.size();i++){cout <<str1[i]<<endl;}cin.get();return 0;}
原创粉丝点击