string类 其他功能与注意

c_str() 返回一个c风格字符串
***********************************************************
string filename;
cout<<"enter a file name: ";
cin>>filename;
ofstream fout;

fout.open(filename.c_str());

 

 

 

 

 

 

 

 

 

////////////////////////////////////////////////////////////////////////////////////
capacity()  查看string对象当前内存块大小

reserve(int n)  指定内存块最小长度


******************************************************************
////////////////////////////////////////////
//
//string.cpp
//
////////////////////////////////////////////
#include <iostream>
#include <string>
int main()
{
 using namespace std;
 string empty;
 string small="bit";
 string larger="elephants are a girl's best fried";
 cout<<"Size.\n";
 cout<<"\tempty: "<<empty.size()<<endl;
 cout<<"\tsmall: "<<small.size()<<endl;
 cout<<"\tlarger: "<<larger.size()<<endl;
 cout<<"Capacities:\n";
    cout<<"\tempty: "<<empty.capacity()<<endl;
 cout<<"\tsmall: "<<small.capacity()<<endl;
 cout<<"\tlarger: "<<larger.capacity()<<endl;
    empty.reserve(100);
 cout<<"empty.reserve(100): "
  <<empty.capacity()<<endl;
 return 0;
}

posted @ 2007-03-14 16:01  Edward Xie  阅读(144)  评论(0)    收藏  举报