在这里总结一下string的用法

String是可变长字符串,使用的时候要包含string头文件。

要想使用标准C++中string类,必须要包含

#include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件
using  std::string;
using  std::wstring;

using namespace std;

下面你就可以使用string/wstring了,它们两分别对应着char和wchar_t。


初始化:

string s1;     //是一个空的string
         strings2(s1);
         strings3="hello";  //拷贝初始化
         strings4=s3;        // 将s3的内容拷贝给s4
         strings5(10,'c');   //直接初始化,string中有十个c
         strings6("hello");  //s6中内容是hello

对string读写操作:

 (2)    string a;  //读写string对象

while(cin>>a)
    {
       cout<<a<<endl;
    } 
   while(getline(cin,a))
             cout<<a<<endl;
  cout<<a<<endl;对于此类的操作,string对象对此操作也是返回运算符左侧的运算对象作为其结果。即,可以实现级联操作。
相当于,         cout<<s1<<s2<<s3<<endl;
           cin<<s1<<s2<<s3<<endl;

(2)当我们遇到不知道具体数量的string时

stringword;
             while(cin>>word)
                   cout<<word<<endl;

 只有当cin插入流遇到文件结束或者非法输入时才结束。(例如空白字符,或者文件结束标志)

(3)

关于string的对象的操作:

string s;

         s.empty();   //当string对象为空时,返回真,否则返回假

         s.size();     //返回S中字符的个数(但返回的类型并不是int 类型,而是size_t类型,一种无符号的类型)

使用两个等号的比较运算符时‘==’,当且仅当两个string对象中字符一对一相等并且长度相等。

 

 

String对象可以直接相加
string S1=”hello”;
string S2=” C++”;
String s3=s1+s2;   //S3的内容是 “hello C++”
 
 
String 对象也能可字面值(字符串)进行相加
String s1=”hello”;
S1=s1+” C++”     //s1 的结果仍然是hello C++

(4)关于string的一些操作函数

Isspace(c) 当c是空白字符时(空格回车 制表),返回真
tolower(c)  大写转小写
toupper(c)  小写转大写
对于处理string中的字符
我们有如下方法,我们以大小写转换为例
 
 stringstr3="a,b,c";
         decltype(str3.size())i;      我们使用decltype()来获取string中字符下标的类型
         for(i=0;i<str3.size();i++)
str3[i]=toupper(str3[i]);
         cout<<str3<<endl;       string的 内容变为大写 A B C
    
 
我们还可以通过使用迭代器来遍历字符,我们使用auto关键字,auto会自动根据初始值的类型来设定我们定义的变量
//stringstr("zheng");
//for(autoi=str.begin();i!=str.end();i++)     i的类型就是size_t类型
//      *i=toupper(*i);
//      cout<<str<<endl;   输出的结果是大写  ZHANG

string的特性描述:

intcapacity()const;    //返回当前容量(即string中不必增加内存即可存放的元素个数)
intmax_size()const;    //返回string对象中可存放的最大字符串的长度
intsize()const;        //返回当前字符串的大小
intlength()const;       //返回当前字符串的长度
boolempty()const;        //当前字符串是否为空
void resize(intlen,char c);//把字符串当前大小置为len,并用字符c填充不足的部



最后要介绍如何在Win32 应用程序中引用MFC中的部分类,例如CString。

1.在工程目录下右键选择"Properties”--->"Configuration Properties”--->“General”--->"Use of MFC"--->"Use MFC in a Static Library",

   默认的是:"Use Standard Windows Libraries",