把网上可以找到的一些string的函数、用法用代码的形式敲了一遍
没什么难点,看过了基本就会了
#include<iostream>
#include<string>
#include<cstring>
#include<limits.h>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
string a="abcdefgh";
cout<<a<<endl<<endl;
cout<<"a.capacity() is "<<a.capacity()<<endl<<endl;
a.erase(2,3);
cout<<a<<endl<<endl;
a.erase(2);
cout<<a<<endl<<endl;
cout<<"a.capacity() is "<<a.capacity()<<endl<<endl;
a.replace(2,1,"hahaha! ");
cout<<a<<endl<<endl;
cout<<a.length()<<' '<<a.size()<<endl<<endl;
cout<<"2*a.max_size() is "<<2*a.max_size()<<endl<<endl;
cout<<"INT_MAX is "<<INT_MAX<<endl<<endl;
reverse(a.begin(),a.end());
cout<<a<<endl<<endl;
a.reserve(1000);
cout<<"after reserve , the capcity is "<<a.capacity()<<endl<<endl;
a.assign(10,'K');a.insert(5," best acmer ");
cout<<a<<endl<<endl;
a.assign(20,'/');
cout<<a<<endl<<endl;
a="look for non-alphabetic characters...";
cout<<a<<endl<<endl;
int found;
found=a.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
if (found!=string::npos)cout << "First non-alphabetic character is " << a[found]<<" at position " << found<< endl<<endl;
found=a.find_last_not_of("abcdefghijklmnopqrstuvwxyz ");
if (found!=string::npos)cout << "Last non-alphabetic character is " << a[found]<<" at position " << found<< endl<<endl;
found=a.find_first_of("abcdefghijklmnopqrstuvwxyz");
if (found!=string::npos)cout << "First alphabetic character is " << a[found]<<" at position " << found<< endl<<endl;
found=a.find_last_of("abcdefghijklmnopqrstuvwxyz");
if (found!=string::npos)cout << "Last alphabetic character is " << a[found]<<" at position " << found<< endl<<endl;
found=a.find('a');
if (found!=string::npos)cout << "First a is at position " << found<< endl<<endl;
found=a.rfind('a');
if (found!=string::npos)cout << "Last a is at position " << found<< endl<<endl;
a.assign(20,'/'); cout<<a<<endl<<endl;
a="AAAAA";
const char *x=a.data(),*y=a.c_str();//??????????
cout<<x<<" "<<y<<endl;
if(x[5]=='\0')cout<<"@@@@@"<<" ";
if(y[5]=='\0')cout<<"$$$$$"<<endl;/////string ?????'\0'??β
char xx[100];strcpy(xx,a.c_str());
cout<<xx<<endl<<endl;
string b;b.assign(20,'/'); cout<<b<<endl<<endl;
string c(20,'/'); cout<<c<<endl<<endl;
a="abcdefg";
cout<<"after assign , the capcity is "<<a.capacity()<<endl<<endl;
cout<<a[1]<<' '<<a.at(1)<<endl<<endl;
c+="\n\n";cout<<c;
c="\n\n"+c;
a="abcdefg";a.append(13,'/');a.push_back('7');cout<<a<<c;
}